-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf000b3
commit 3b40397
Showing
10 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'geocoder/lookups/base' | ||
require 'geocoder/results/ip2location_lite' | ||
|
||
module Geocoder | ||
module Lookup | ||
class Ip2locationLite < Base | ||
attr_reader :gem_name | ||
|
||
def initialize | ||
unless configuration[:file].nil? | ||
begin | ||
@gem_name = 'ip2location_ruby' | ||
require @gem_name | ||
rescue LoadError | ||
raise "Could not load IP2Location DB dependency. To use the IP2LocationLite lookup you must add the #{@gem_name} gem to your Gemfile or have it installed in your system." | ||
end | ||
|
||
@i2l = Ip2location.new.open(configuration[:file].to_s) | ||
end | ||
super | ||
end | ||
|
||
def name | ||
'IP2LocationLite' | ||
end | ||
|
||
def required_api_key_parts | ||
[] | ||
end | ||
|
||
private | ||
|
||
def results(query) | ||
return [] unless configuration[:file] | ||
|
||
result = @i2l.get_all(query.to_s) | ||
result.nil? ? [] : [result] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'geocoder/results/base' | ||
|
||
module Geocoder::Result | ||
class Ip2locationLite < Base | ||
|
||
def coordinates | ||
[@data[:latitude], @data[:longitude]] | ||
end | ||
|
||
def city | ||
@data[:city] | ||
end | ||
|
||
def state | ||
@data[:region] | ||
end | ||
|
||
def state_code | ||
"" # Not available in Maxmind's database | ||
end | ||
|
||
def country | ||
@data[:country_long] | ||
end | ||
|
||
def country_code | ||
@data[:country_short] | ||
end | ||
|
||
def postal_code | ||
@data[:zipcode] | ||
end | ||
|
||
def self.response_attributes | ||
%w[country_short country_long region latitude longitude isp | ||
domain netspeed areacode iddcode timezone zipcode weatherstationname | ||
weatherstationcode mcc mnc mobilebrand elevation usagetype addresstype | ||
category district asn as] | ||
end | ||
|
||
response_attributes.each do |a| | ||
define_method a do | ||
@data[a] || "" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# encoding: utf-8 | ||
require 'test_helper' | ||
|
||
class Ip2locationLiteTest < GeocoderTestCase | ||
def setup | ||
Geocoder.configure(ip_lookup: :ip2location_lite, ip2location_lite: { file: File.join('folder', 'test_file') }) | ||
end | ||
|
||
def test_loopback | ||
result = Geocoder.search('127.0.0.1').first | ||
assert_equal '', result.country_short | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters