Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix returning invalid ip address #3

Merged
merged 7 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Extract timeout error to the service
  • Loading branch information
pedrocarrico committed Mar 3, 2018
commit 867edb505b680c4aab2dbde7e2f1467bcf3bdc75
5 changes: 2 additions & 3 deletions lib/public_ip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module PublicIp
TIMEOUT_IN_SECS = 3

class UnknownService < StandardError; end
class TimedOut < StandardError; end

module_function

Expand All @@ -24,11 +23,11 @@ def get_ip(service: :random)
else
PublicIp::Service::Registry[service].ip
end
rescue Timeout::Error
rescue PublicIp::Service::TimedOut
tries -= 1
retry if tries.positive? && service == :random

raise PublicIp::TimedOut, 'Took too long to get your public ip address, try with another service'
raise
end

def list_services
Expand Down
3 changes: 2 additions & 1 deletion lib/public_ip/service/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module PublicIp
module Service
class TimedOut < StandardError; end
class Simple
attr_reader :uri
attr_reader :headers
Expand All @@ -27,7 +28,7 @@ def self.symbol
end

def self.perform_request
Timeout.timeout(PublicIp::TIMEOUT_IN_SECS) do
Timeout.timeout(PublicIp::TIMEOUT_IN_SECS, PublicIp::Service::TimedOut) do
request = Net::HTTP::Get.new(uri, headers)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
Expand Down