Skip to content

Commit

Permalink
Fail if it’s an invalid IP address (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrocarrico committed Apr 21, 2017
1 parent 5ceed05 commit 24f2b17
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/public_ip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_ip(service: :random)
else
PublicIp::Service::Registry[service].ip
end
rescue PublicIp::Service::TimedOut
rescue PublicIp::Service::TimedOut, PublicIp::Service::InvalidIpAddress
tries -= 1
if tries > 0 && service == :random
retry
Expand Down
4 changes: 1 addition & 3 deletions lib/public_ip/service/json_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ def self.parse_json(_json_data)
fail 'Not implemented'
end

def self.ip
response = perform_request

def self.extract_ip(response)
parse_json(JSON.parse(response.body.strip))
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/public_ip/service/matched_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ class MatchedExpression < Simple

attr_reader :match_regex

def self.ip
response = perform_request

def self.extract_ip(response)
response.body.match(match_regex)[1].strip
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/public_ip/service/parsed_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Service
class ParsedHTML < Simple
extend PublicIp::Service::Registrable

def self.ip
response = perform_request
def self.parse_ip_address(_html)
fail 'Not implemented'
end

def self.extract_ip(response)
parse_ip_address(response.body)
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/public_ip/service/plain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ module Service
class Plain < Simple
extend PublicIp::Service::Registrable

def self.ip
response = perform_request

response.body.strip
def self.extract_ip(response)
response.body.strip.delete('"')
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions lib/public_ip/service/simple.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
require 'net/http'
require 'timeout'

IPV4_REGEX = /(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])/

module PublicIp
module Service
class InvalidIpAddress < StandardError; end
class TimedOut < StandardError; end

class Simple
attr_reader :uri
attr_reader :headers

def self.ip
response = perform_request

ip_address = extract_ip(response)

unless IPV4_REGEX.match(ip_address)
fail PublicIp::Service::InvalidIpAddress, "#{ip_address} is not a valid ip address"
end

ip_address
end

def self.uri
fail 'Not implemented'
end
Expand Down

0 comments on commit 24f2b17

Please sign in to comment.