Skip to content

Commit

Permalink
Refactor code to follow gem's typical style.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreisner committed Jul 27, 2018
1 parent 20a8c01 commit e329eb3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 92 deletions.
8 changes: 4 additions & 4 deletions README_API_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,16 +463,16 @@ IP Address Lookups
* **Terms of Service**: https://ipdata.co/terms.html
* **Limitations**: ?

### IP2Location Web Service (`:ip2location`)
### IP2Location (`:ip2location`)

* **API key**: optional, see: https://www.ip2location.com/web-service
* **Quota**: 20 query/day (up to 100k credits with paid API key)
* **API key**: optional (20 free demo queries per day)
* **Quota**: up to 100k credits with paid API key
* **Region**: world
* **SSL support**: yes
* **Languages**: English
* **Documentation**: https://www.ip2location.com/web-service
* **Terms of Service**: https://www.ip2location.com/web-service
* **Notes**: To use IP2Location Web Service with API Key set `Geocoder.configure(:ip_lookup => :ip2location, :api_key => "IP2LOCATION_WEB_SERVICE_API_KEY")`. Supports the optional param :package with `Geocoder.configure(:ip2location => {:package => "WSX"})` (see API documentation for package offered in details).
* **Notes**: With the non-free version, specify your desired package: `Geocoder.configure(ip2location: {package: "WSX"})` (see API documentation for package details).


Local IP Address Lookups
Expand Down
22 changes: 11 additions & 11 deletions lib/geocoder/lookups/ip2location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@ def name

def query_url(query)
api_key = configuration.api_key ? configuration.api_key : "demo"
url_ = "#{protocol}://api.ip2location.com/?ip=#{query.sanitized_text}&key=#{api_key}&format=json"
url = "#{protocol}://api.ip2location.com/?ip=#{query.sanitized_text}&key=#{api_key}&format=json"

if (params = url_query_string(query)) && !params.empty?
url_ + "&" + params
else
url_
if (params = url_query_string(query)) and !params.empty?
url << "&" + params
end

url
end

def supported_protocols
[:http, :https]
end

private
private # ----------------------------------------------------------------

def results(query)
return [reserved_result(query.text)] if query.loopback_ip_address?

return [] unless doc = fetch_data(query)

if doc["response"] == "INVALID ACCOUNT"
raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "INVALID ACCOUNT")
return []
Expand Down Expand Up @@ -64,9 +62,11 @@ def reserved_result(query)
end

def query_url_params(query)
params = {}
params.merge!(package: configuration[:package]) if configuration.has_key?(:package)
params.merge(super)
params = super
if configuration.has_key?(:package)
params.merge!(package: configuration[:package])
end
params
end

end
Expand Down
85 changes: 8 additions & 77 deletions lib/geocoder/results/ip2location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,16 @@ def address(format = :full)
"#{city_name} #{zip_code}, #{country_name}".sub(/^[ ,]*/, '')
end

def country_code
@data['country_code']
def self.response_attributes
%w[country_code country_name region_name city_name latitude longitude
zip_code time_zone isp domain net_speed idd_code area_code usage_type
weather_station_code weather_station_name mcc mnc mobile_brand elevation]
end

def country_name
@data['country_name']
response_attributes.each do |attr|
define_method attr do
@data[attr] || ""
end
end

def region_name
@data['region_name']
end

def city_name
@data['city_name']
end

def latitude
@data['latitude']
end

def longitude
@data['longitude']
end

def zip_code
@data['zip_code']
end

def time_zone
@data['time_zone']
end

def isp
@data['isp']
end

def domain
@data['domain']
end

def net_speed
@data['net_speed']
end

def idd_code
@data['idd_code']
end

def area_code
@data['area_code']
end

def weather_station_code
@data['weather_station_code']
end

def weather_station_name
@data['weather_station_name']
end

def mcc
@data['mcc']
end

def mnc
@data['mnc']
end

def mobile_brand
@data['mobile_brand']
end

def elevation
@data['elevation']
end

def usage_type
@data['usage_type']
end

end
end

0 comments on commit e329eb3

Please sign in to comment.