diff --git a/getstream-ruby.gemspec b/getstream-ruby.gemspec index 895ad6e..54e1f0b 100644 --- a/getstream-ruby.gemspec +++ b/getstream-ruby.gemspec @@ -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' diff --git a/lib/getstream_ruby/configuration.rb b/lib/getstream_ruby/configuration.rb index a3b6cad..36f0bff 100644 --- a/lib/getstream_ruby/configuration.rb +++ b/lib/getstream_ruby/configuration.rb @@ -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 diff --git a/lib/getstream_ruby/stream_response.rb b/lib/getstream_ruby/stream_response.rb index 2e8357f..c616c16 100644 --- a/lib/getstream_ruby/stream_response.rb +++ b/lib/getstream_ruby/stream_response.rb @@ -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] + 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) @@ -50,6 +57,22 @@ def 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 diff --git a/spec/connection_pooling_spec.rb b/spec/connection_pooling_spec.rb index 4321273..f4cd630 100644 --- a/spec/connection_pooling_spec.rb +++ b/spec/connection_pooling_spec.rb @@ -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) diff --git a/spec/stream_response_spec.rb b/spec/stream_response_spec.rb index 100a221..34d08a7 100644 --- a/spec/stream_response_spec.rb +++ b/spec/stream_response_spec.rb @@ -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