diff --git a/CHANGELOG.md b/CHANGELOG.md index cc21c12..b2ad0ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,13 @@ # Changelog ## Version 1 +### Version 1.1.0 +* Fix error method causing NoMethodError. +* Better documentation for all methods, including adding the missing documentation for the private error method. +* Case/When statement instead of if conditional in request_successful? +* Better code in all the getter methods, improving how values are returned and how error is called. +* New get_error_description for getting the description to go with the ambiguous error message. +* Better error code, which is now dependent on request_successful?, instead of having getter methods being dependent on request_successful?. This greatly reduces the length of the methods. +* HTTPClient dependency is no longer open-eneded. + ### Version 1.0.0 * Initial release version. Contains methods for most everything available with IP-API.com. The only API that I am not utilizing is the DNS API. That should come in a future release. diff --git a/lib/simple_geolocator.rb b/lib/simple_geolocator.rb index 4aa66b7..4ca711e 100644 --- a/lib/simple_geolocator.rb +++ b/lib/simple_geolocator.rb @@ -2,7 +2,7 @@ require 'json' module SimpleGeolocator - module_function + extend self @client = HTTPClient.new @@ -18,14 +18,15 @@ def get_full_response(ip) end # Gets whether the request failed or not. - # @param response [String] The response body (gotten by #get_full_response) - # to check. + # @param response [JSON] The parsed response body (#get_full_response) to + # check. # @return [Boolean] True if successful, false if errored. def request_successful?(response) - if response['status'] == 'success' - return true - else - return false + case response['status'] + when 'success' + true + when 'fail' + false end end @@ -33,116 +34,136 @@ def request_successful?(response) # @param ip [String] See #get_full_response # @return [Hash] A hash containing data formatted as # { :name => 'United States', :code => 'US' } + # @return [String] A string containing the error message. def country(ip) response = get_full_response(ip) - if request_successful?(response) - ret = { - name: response['country'], - code: response['countryCode'] - } - return ret - else - return error(response) - end + err = error(response) + return err unless err.nil? + ret = { + name: response['country'], + code: response['countryCode'] + } + ret end # Gets the region data for the IP. # @param ip [String] See #get_full_response # @return [Hash] A hash containing data formatted as # { :name => 'Oregon', :code => 'OR'} + # @return [String] A string containing the error message. def region(ip) response = get_full_response(ip) - if request_successful?(response) - ret = { - name: response['regionName'], - code: response['region'] - } - return ret - else - return error(response) - end + err = error(response) + return err unless err.nil? + ret = { + name: response['regionName'], + code: response['region'] + } + ret end # Gets the city name for the IP. # @param ip [String] See #get_full_response # @return [String] The name of the city that the IP is located in. + # @return [String] A string containing the error message. def city(ip) response = get_full_response(ip) - if request_successful?(response) - return response['city'] - else - return error(response) - end + err = error(response) + return err unless err.nil? + + response['city'] end # Gets the zip code for the IP. # @param ip [String] See #get_full_response # @return [Int] The zip code that the IP is located in. + # @return [String] A string containing the error message. def zip(ip) response = get_full_response(ip) - if request_successful?(response) - return response['zip'].to_i - else - return error(response) - end + err = error(response) + return err unless err.nil? + + response['zip'].to_i end # Gets the latitude, longitude for the IP. # @param ip [String] See #get_full_response # @return [Array] An array of Floats formatted as lat, lon + # @return [String] A string containing the error message. def ll(ip) response = get_full_response(ip) - if request_successful?(response) - ret = [ - response['lat'], - response['lon'] - ] - return ret - else - return error(response) - end + err = error(response) + return err unless err.nil? + + ret = [response['lat'], response['lon']] + ret end # Gets the timezone for the IP. # @param ip [String] See #get_full_response # @return [String] The timezone (UTC, PST, etc.) that the IP is in. + # @return [String] A string containing the error message. def timezone(ip) response = get_full_response(ip) - if request_successful?(response) - return response['timezone'] - else - return error(response) - end + err = error(response) + return err unless err.nil? + + response['timezone'] end # Gets the name of the IP's Internet Service Provider. # @param ip [String] See #get_full_response # @return [String] The ISP name, such as Comcast Cable. + # @return [String] A string containing the error message. def isp_name(ip) response = get_full_response(ip) - if request_successful?(response) - return response['isp'] - else - return error(response) - end + err = error(response) + return err unless err.nil? + + response['isp'] end # Gets the name of the IP's organization. For most people, this is identical # to their ISP name. # @param ip [String] See #get_full_response # @return [String] The organization name, such as Google. + # @return [String] A string containing the error message. def organization_name(ip) response = get_full_response(ip) - if request_successful?(response) - return response['org'] + err = error(response) + return err unless err.nil? + + response['org'] + end + + # Gets the according description for the semi-ambiguous error returned by the + # API. + # @param error [String] The error message returned by #error + # @return [String] The error description. + # @return [Nil] If you provided an invalid error message. + def get_error_description(error) + case error + when 'private range' + return 'The IP address is part of a private range.' + when 'reserved range' + return 'The IP address is part of a reserved range.' + when 'invalid query' + return 'The IP address or domain name is invalid.' + when 'quota' + return 'You have reached the quota.' else - return error(response) + return nil end end private + # Gets the error message from a response. + # @param response [JSON] See #request_successful? + # @return [String] The error message. + # @return [Nil] If there was no error message to begin with. def error(response) - response['message'] + return response['message'] unless request_successful?(response) + nil end end diff --git a/simple_geolocator.gemspec b/simple_geolocator.gemspec index dea9e8e..10aacc0 100644 --- a/simple_geolocator.gemspec +++ b/simple_geolocator.gemspec @@ -3,7 +3,7 @@ Gem::Specification.new do |s| s.name = 'simple_geolocator' s.summary = 'A Ruby gem for easily using the IP-API.com API to perform ' \ 'IP geolocation.' - s.version = '1.0.0' + s.version = '1.1.0' s.license = 'CC-BY-NC-ND-4.0' s.description = 'Accessing the IP API through HTTPClient. I found that many' \ ', if not all, Geolocation gems were very annoying and ' \ @@ -21,5 +21,5 @@ Gem::Specification.new do |s| 'CHANGELOG.md', 'lib/simple_geolocator.rb' ] - s.add_runtime_dependency('httpclient') + s.add_runtime_dependency('httpclient', '~> 2.6', '>= 2.6.0.1') end