Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions getstream-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Gem::Specification.new do |spec|
spec.add_dependency 'faraday-gzip', '~> 3.0'
spec.add_dependency 'faraday-multipart', '~> 1.0'
spec.add_dependency 'faraday-net_http_persistent', '~> 2.3'
spec.add_dependency 'json', '~> 2.0'
spec.add_dependency 'jwt', '~> 2.0'
spec.add_dependency 'json', '>= 2.0', '< 4'
spec.add_dependency 'jwt', '>= 2.0', '< 4'
spec.add_dependency 'net-http-persistent', '~> 4.0'

# spec.metadata['rubygems_mfa_required'] = 'true'
Expand Down
12 changes: 6 additions & 6 deletions lib/getstream_ruby/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ def assign_timeouts_and_pool(options, use_env:)

if use_env
env_request_timeout = ENV.fetch('STREAM_REQUEST_TIMEOUT', nil) || ENV.fetch('STREAM_TIMEOUT', nil)
@request_timeout = (request_timeout || timeout || env_request_timeout || 30).to_i
@request_timeout = (request_timeout || timeout || env_request_timeout || 30).to_f
@max_conns_per_host = (max_conns_per_host || ENV.fetch('STREAM_MAX_CONNS_PER_HOST', nil) || 100).to_i
# 25s sits under GCP SSL-proxy idle (~30s) and AWS ALB idle (60s) so
# net_http_persistent drops the conn before the LB does (CHA-4943).
@idle_timeout = (idle_timeout || ENV.fetch('STREAM_IDLE_TIMEOUT', nil) || 25).to_i
@connect_timeout = (connect_timeout || ENV.fetch('STREAM_CONNECT_TIMEOUT', nil) || 10).to_i
@idle_timeout = (idle_timeout || ENV.fetch('STREAM_IDLE_TIMEOUT', nil) || 25).to_f
@connect_timeout = (connect_timeout || ENV.fetch('STREAM_CONNECT_TIMEOUT', nil) || 10).to_f
else
@request_timeout = (request_timeout || timeout || 30).to_i
@request_timeout = (request_timeout || timeout || 30).to_f
@max_conns_per_host = (max_conns_per_host || 100).to_i
@idle_timeout = (idle_timeout || 25).to_i
@connect_timeout = (connect_timeout || 10).to_i
@idle_timeout = (idle_timeout || 25).to_f
@connect_timeout = (connect_timeout || 10).to_f
end
end

Expand Down
65 changes: 44 additions & 21 deletions lib/getstream_ruby/stream_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,36 @@ def initialize(data)
@data = data
end

# An unknown plain name reads as nil because the API omits empty optional fields.
# A name with a ?/!/= suffix is a real NoMethodError.
def method_missing(method_name, *_args)
key = method_name.to_s
return super unless method_name.to_s.match?(/\A\w+\z/)

if @data.is_a?(Hash) && @data.key?(key)
value = @data[key]
# Recursively wrap nested hashes
if value.is_a?(Hash)
StreamResponse.new(value)
elsif value.is_a?(Array)
value.map { |item| item.is_a?(Hash) ? StreamResponse.new(item) : item }
else
value
end
elsif @data.is_a?(Hash) && @data.key?(key.to_sym)
value = @data[key.to_sym]
if value.is_a?(Hash)
StreamResponse.new(value)
elsif value.is_a?(Array)
value.map { |item| item.is_a?(Hash) ? StreamResponse.new(item) : item }
else
value
end
end
self[method_name]
end

def [](key)
wrap(raw(key))
end

def key?(key)
@data.is_a?(Hash) && (@data.key?(key.to_s) || @data.key?(key.to_s.to_sym))
end

def fetch(key, *default)
return self[key] if key?(key)
return yield(key) if block_given?
return default.first unless default.empty?

raise KeyError, "key not found: #{key.inspect}"
end

def dig(key, *rest)
value = self[key]
Comment thread
getstream-metis[bot] marked this conversation as resolved.
Resolved
return value if rest.empty? || value.nil?
raise TypeError, "#{value.class} does not have #dig method" unless value.respond_to?(:dig)

value.dig(*rest)
end

def respond_to_missing?(method_name, include_private = false)
Expand All @@ -50,6 +57,22 @@ def inspect
"#<GetStreamRuby::StreamResponse:0x#{object_id.to_s(16)} @data=#{@data.inspect}>"
end

private

def raw(key)
return unless @data.is_a?(Hash)

@data.key?(key.to_s) ? @data[key.to_s] : @data[key.to_s.to_sym]
end

def wrap(value)
case value
when Hash then StreamResponse.new(value)
when Array then value.map { |item| item.is_a?(Hash) ? StreamResponse.new(item) : item }
else value
end
end

end

end
10 changes: 10 additions & 0 deletions spec/connection_pooling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ def capture_adapter_call

end

it 'keeps fractional timeouts' do

client = GetStreamRuby.manual(api_key: 'k', api_secret: 's',
request_timeout: 2.5, connect_timeout: 0.5, idle_timeout: 1.5)
expect(client.configuration.request_timeout).to eq(2.5)
expect(client.configuration.connect_timeout).to eq(0.5)
expect(client.configuration.idle_timeout).to eq(1.5)

end

it 'keeps timeout: as a backwards-compat alias for request_timeout:' do

client = GetStreamRuby.manual(api_key: 'k', api_secret: 's', timeout: 11)
Expand Down
38 changes: 38 additions & 0 deletions spec/stream_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,42 @@

end

describe 'hash-style access' do

let(:response) { described_class.new('user' => { 'id' => 'u1', 'teams' => [{ 'name' => 'red' }] }, 'count' => 0) }

it 'reads keys with [], key?, fetch and dig' do

expect(response['count']).to eq(0)
expect(response[:user].id).to eq('u1')
expect(response.key?('user')).to be(true)
expect(response.key?(:missing)).to be(false)
expect(response.fetch('count')).to eq(0)
expect(response.fetch('missing', 'fallback')).to eq('fallback')
expect { response.fetch('missing') }.to raise_error(KeyError)
expect(response.dig('user', 'teams', 0, 'name')).to eq('red')
expect(response.dig('missing', 'id')).to be_nil
expect(response[0]).to be_nil
expect(response.key?(0)).to be(false)
expect { response.dig('count', 'id') }.to raise_error(TypeError)

end

it 'reads plain names as nil when the body is not a Hash' do

expect(described_class.new(nil).duration).to be_nil
expect(described_class.new('').duration).to be_nil

end

it 'raises NoMethodError for names that cannot be a field' do

expect(response.missing_field).to be_nil
expect { response.missing_field? }.to raise_error(NoMethodError)
expect { response.missing_field! }.to raise_error(NoMethodError)

end

end

end
Loading