Skip to content

Commit

Permalink
Replaced RuntimeErrors with properly namespaced exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
arr-ee committed Nov 27, 2013
1 parent 3670e3b commit 86617f3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/geo_ip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
require 'rest-client'

class GeoIp
class InvalidPrecissionError < ArgumentError; end
class InvalidTimezoneError < ArgumentError; end
class InvalidIpError < ArgumentError; end
class ApiKeyError < ArgumentError; end

SERVICE_URL = 'http://api.ipinfodb.com/v3/ip-'
CITY_API = 'city'
COUNTRY_API = 'country'
Expand Down Expand Up @@ -39,14 +44,14 @@ def fallback_timeout= fallback_timeout
def set_defaults_if_necessary options
options[:precision] ||= :city
options[:timezone] ||= false
raise 'Invalid precision' unless [:country, :city].include?(options[:precision])
raise 'Invalid timezone' unless [true, false].include?(options[:timezone])
raise InvalidPrecisionError unless [:country, :city].include?(options[:precision])
raise InvalidTimezoneError unless [true, false].include?(options[:timezone])
end

def lookup_url ip, options = {}
set_defaults_if_necessary options
raise 'API key must be set first: GeoIp.api_key = \'YOURKEY\'' if self.api_key.nil?
raise 'Invalid IP address' unless ip.to_s =~ IPV4_REGEXP
raise ApiKeyError.new('API key must be set first: GeoIp.api_key = \'YOURKEY\'') if self.api_key.nil?
raise InvalidIpError.new(ip) unless ip.to_s =~ IPV4_REGEXP

"#{SERVICE_URL}#{options[:precision] == :city || options[:timezone] ? CITY_API : COUNTRY_API}?key=#{api_key}&ip=#{ip}&format=json&timezone=#{options[:timezone]}"
end
Expand Down

0 comments on commit 86617f3

Please sign in to comment.