Skip to content

Commit

Permalink
updated international example and test to use send method instead of …
Browse files Browse the repository at this point in the history
…send_lookup (now deprecated). added retry sender functionality to begin at a retry sleep of 1sec instead of 0sec. (#41)
  • Loading branch information
abbeynels committed Aug 11, 2023
1 parent 1d9960e commit c13d680
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/international_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def run
lookup.country = 'Brazil'
lookup.postal_code = '02516-050'

candidates = client.send(lookup) # The candidates are also stored in the lookup's 'result' field.
candidates = client.send_lookup(lookup) # The candidates are also stored in the lookup's 'result' field.

first_candidate = candidates[0]
puts "Input ID: #{first_candidate.input_id}"
Expand Down
2 changes: 1 addition & 1 deletion lib/smartystreets_ruby_sdk/retry_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(max_retries, inner, sleeper, logger)
def send(request)
response = @inner.send(request)

(0..@max_retries-1).each do |i|
(1..@max_retries).each do |i|

break if STATUS_TO_RETRY.include?(response.status_code.to_i) == false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_sending_freeform_lookup
client = Client.new(sender, serializer)
lookup = Lookup.new('1', '2')

client.send(lookup)
client.send_lookup(lookup)

assert_equal('1', sender.request.parameters['freeform'])
assert_equal('2', sender.request.parameters['country'])
Expand All @@ -41,7 +41,7 @@ def test_sending_single_fully_populated_lookup
lookup.administrative_area = '8'
lookup.postal_code = '9'

client.send(lookup)
client.send_lookup(lookup)

assert_equal('1234', sender.request.parameters['input_id'])
assert_equal('0', sender.request.parameters['country'])
Expand All @@ -63,7 +63,7 @@ def test_empty_lookup_rejected
client = Client.new(sender, nil)

assert_raises SmartyStreets::UnprocessableEntityError do
client.send(Lookup.new)
client.send_lookup(Lookup.new)
end
end

Expand All @@ -73,7 +73,7 @@ def test_rejects_lookups_with_only_country
lookup = Lookup.new(nil, '0')

assert_raises SmartyStreets::UnprocessableEntityError do
client.send(lookup)
client.send_lookup(lookup)
end
end

Expand All @@ -84,7 +84,7 @@ def test_rejects_lookups_with_only_country_and_address1
lookup.address1 = '1'

assert_raises SmartyStreets::UnprocessableEntityError do
client.send(lookup)
client.send_lookup(lookup)
end
end

Expand All @@ -96,7 +96,7 @@ def test_rejects_lookups_with_only_country_and_address1_and_locality
lookup.locality = '2'

assert_raises SmartyStreets::UnprocessableEntityError do
client.send(lookup)
client.send_lookup(lookup)
end
end

Expand All @@ -108,7 +108,7 @@ def test_rejects_lookups_with_only_country_and_address1_and_administrative_area
lookup.administrative_area = '2'

assert_raises SmartyStreets::UnprocessableEntityError do
client.send(lookup)
client.send_lookup(lookup)
end
end

Expand All @@ -120,17 +120,17 @@ def test_accepts_lookups_with_enough_info

lookup.country = '0'
lookup.freeform = '1'
client.send(lookup)
client.send_lookup(lookup)

lookup.freeform = nil
lookup.address1 = '1'
lookup.postal_code = '2'
client.send(lookup)
client.send_lookup(lookup)

lookup.postal_code = nil
lookup.locality = '3'
lookup.administrative_area = '4'
client.send(lookup)
client.send_lookup(lookup)
end

def test_deserialize_called_with_response_body
Expand All @@ -140,7 +140,7 @@ def test_deserialize_called_with_response_body
deserializer = FakeDeserializer.new({})
client = Client.new(sender, deserializer)

client.send(Lookup.new('1', '2'))
client.send_lookup(Lookup.new('1', '2'))

assert_equal(response.payload, deserializer.input)
end
Expand All @@ -153,7 +153,7 @@ def test_candidates_correctly_assigned_to_lookup
deserializer = FakeDeserializer.new(raw_candidates)
client = Client.new(sender, deserializer)

client.send(lookup)
client.send_lookup(lookup)

assert_equal(expected_candidates[0].address1, lookup.result[0].address1)
assert_equal(expected_candidates[1].address1, lookup.result[1].address1)
Expand Down
12 changes: 10 additions & 2 deletions test/smartystreets_ruby_sdk/test_retry_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_return_response_if_retry_limit_exceeded

assert(response)
assert_equal(5, inner.current_status_code_index)
assert_equal([0,1,2,3], sleeper.sleep_durations)
assert_equal([1,2,3,4], sleeper.sleep_durations)
assert_equal('500', response.status_code)
end

Expand All @@ -48,7 +48,7 @@ def test_backoff_does_not_exceed_max

send_with_retry(20, inner, sleeper)

assert_equal([0,1,2,3,4,5,6,7,8,9,10,10,10], sleeper.sleep_durations)
assert_equal([1,2,3,4,5,6,7,8,9,10,10,10,10], sleeper.sleep_durations)
end

def test_nil_status_does_not_retry
Expand All @@ -68,6 +68,14 @@ def test_sleep_on_rate_limit
end

def test_rate_limit_error_return
inner = FailingSender.new(%w(429), {'Retry-After' => 12})
sleeper = FakeSleeper.new

send_with_retry(10, inner, sleeper)
assert_equal([12], sleeper.sleep_durations)
end

def test_rate_limit_greater_than_10s
inner = FailingSender.new(%w(429), {'Retry-After' => 7})
sleeper = FakeSleeper.new

Expand Down

0 comments on commit c13d680

Please sign in to comment.