-
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
Showing
9 changed files
with
325 additions
and
1 deletion.
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,46 @@ | ||
# encoding: utf-8 | ||
|
||
require 'geocoder/lookups/base' | ||
require 'geocoder/results/abstract_api' | ||
|
||
module Geocoder::Lookup | ||
class AbstractApi < Base | ||
|
||
def name | ||
"Abstract API" | ||
end | ||
|
||
def required_api_key_parts | ||
['api_key'] | ||
end | ||
|
||
def supported_protocols | ||
[:https] | ||
end | ||
|
||
private # --------------------------------------------------------------- | ||
|
||
def base_query_url(query) | ||
"#{protocol}://ipgeolocation.abstractapi.com/v1/?" | ||
end | ||
|
||
def query_url_params(query) | ||
params = {api_key: configuration.api_key} | ||
|
||
ip_address = query.sanitized_text | ||
if ip_address.is_a?(String) && ip_address.length > 0 | ||
params[:ip_address] = ip_address | ||
end | ||
|
||
params.merge(super) | ||
end | ||
|
||
def results(query, reverse = false) | ||
if doc = fetch_data(query) | ||
[doc] | ||
else | ||
[] | ||
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,146 @@ | ||
require 'geocoder/results/base' | ||
|
||
module Geocoder | ||
module Result | ||
class AbstractApi < Base | ||
|
||
## | ||
# Geolocation | ||
|
||
def state | ||
@data['region'] | ||
end | ||
|
||
def state_code | ||
@data['region_iso_code'] | ||
end | ||
|
||
def city | ||
@data["city"] | ||
end | ||
|
||
def city_geoname_id | ||
@data["city_geoname_id"] | ||
end | ||
|
||
def region_geoname_id | ||
@data["region_geoname_id"] | ||
end | ||
|
||
def postal_code | ||
@data["postal_code"] | ||
end | ||
|
||
def country | ||
@data["country"] | ||
end | ||
|
||
def country_code | ||
@data["country_code"] | ||
end | ||
|
||
def country_geoname_id | ||
@data["country_geoname_id"] | ||
end | ||
|
||
def country_is_eu | ||
@data["country_is_eu"] | ||
end | ||
|
||
def continent | ||
@data["continent"] | ||
end | ||
|
||
def continent_code | ||
@data["continent_code"] | ||
end | ||
|
||
def continent_geoname_id | ||
@data["continent_geoname_id"] | ||
end | ||
|
||
## | ||
# Security | ||
|
||
def is_vpn? | ||
@data.dig "security", "is_vpn" | ||
end | ||
|
||
## | ||
# Timezone | ||
|
||
def timezone_name | ||
@data.dig "timezone", "name" | ||
end | ||
|
||
def timezone_abbreviation | ||
@data.dig "timezone", "abbreviation" | ||
end | ||
|
||
def timezone_gmt_offset | ||
@data.dig "timezone", "gmt_offset" | ||
end | ||
|
||
def timezone_current_time | ||
@data.dig "timezone", "current_time" | ||
end | ||
|
||
def timezone_is_dst | ||
@data.dig "timezone", "is_dst" | ||
end | ||
|
||
## | ||
# Flag | ||
|
||
def flag_emoji | ||
@data.dig "flag", "emoji" | ||
end | ||
|
||
def flag_unicode | ||
@data.dig "flag", "unicode" | ||
end | ||
|
||
def flag_png | ||
@data.dig "flag", "png" | ||
end | ||
|
||
def flag_svg | ||
@data.dig "flag", "svg" | ||
end | ||
|
||
## | ||
# Currency | ||
|
||
def currency_currency_name | ||
@data.dig "currency", "currency_name" | ||
end | ||
|
||
def currency_currency_code | ||
@data.dig "currency", "currency_code" | ||
end | ||
|
||
## | ||
# Connection | ||
|
||
def connection_autonomous_system_number | ||
@data.dig "connection", "autonomous_system_number" | ||
end | ||
|
||
def connection_autonomous_system_organization | ||
@data.dig "connection", "autonomous_system_organization" | ||
end | ||
|
||
def connection_connection_type | ||
@data.dig "connection", "connection_type" | ||
end | ||
|
||
def connection_isp_name | ||
@data.dig "connection", "isp_name" | ||
end | ||
|
||
def connection_organization_name | ||
@data.dig "connection", "organization_name" | ||
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,45 @@ | ||
{ | ||
"ip_address": "2.19.128.50", | ||
"city": "Seattle", | ||
"city_geoname_id": 5809844, | ||
"region": "Washington", | ||
"region_iso_code": "WA", | ||
"region_geoname_id": 5815135, | ||
"postal_code": "98111", | ||
"country": "United States", | ||
"country_code": "US", | ||
"country_geoname_id": 6252001, | ||
"country_is_eu": false, | ||
"continent": "North America", | ||
"continent_code": "NA", | ||
"continent_geoname_id": 6255149, | ||
"longitude": -122.3412, | ||
"latitude": 47.6032, | ||
"security": { | ||
"is_vpn": false | ||
}, | ||
"timezone": { | ||
"name": "America/Los_Angeles", | ||
"abbreviation": "PST", | ||
"gmt_offset": -8, | ||
"current_time": "06:06:14", | ||
"is_dst": false | ||
}, | ||
"flag": { | ||
"emoji": "🇺🇸", | ||
"unicode": "U+1F1FA U+1F1F8", | ||
"png": "https://static.abstractapi.com/country-flags/US_flag.png", | ||
"svg": "https://static.abstractapi.com/country-flags/US_flag.svg" | ||
}, | ||
"currency": { | ||
"currency_name": "USD", | ||
"currency_code": "USD" | ||
}, | ||
"connection": { | ||
"autonomous_system_number": 16625, | ||
"autonomous_system_organization": "AKAMAI-AS", | ||
"connection_type": "Corporate", | ||
"isp_name": "Akamai Technologies", | ||
"organization_name": null | ||
} | ||
} |
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,45 @@ | ||
{ | ||
"ip_address": "2.19.128.50", | ||
"city": "Seattle", | ||
"city_geoname_id": 5809844, | ||
"region": "Washington", | ||
"region_iso_code": "WA", | ||
"region_geoname_id": 5815135, | ||
"postal_code": "98111", | ||
"country": "United States", | ||
"country_code": "US", | ||
"country_geoname_id": 6252001, | ||
"country_is_eu": false, | ||
"continent": "North America", | ||
"continent_code": "NA", | ||
"continent_geoname_id": 6255149, | ||
"longitude": -122.3412, | ||
"latitude": 47.6032, | ||
"security": { | ||
"is_vpn": false | ||
}, | ||
"timezone": { | ||
"name": "America/Los_Angeles", | ||
"abbreviation": "PST", | ||
"gmt_offset": -8, | ||
"current_time": "06:06:14", | ||
"is_dst": false | ||
}, | ||
"flag": { | ||
"emoji": "🇺🇸", | ||
"unicode": "U+1F1FA U+1F1F8", | ||
"png": "https://static.abstractapi.com/country-flags/US_flag.png", | ||
"svg": "https://static.abstractapi.com/country-flags/US_flag.svg" | ||
}, | ||
"currency": { | ||
"currency_name": "USD", | ||
"currency_code": "USD" | ||
}, | ||
"connection": { | ||
"autonomous_system_number": 16625, | ||
"autonomous_system_organization": "AKAMAI-AS", | ||
"connection_type": "Corporate", | ||
"isp_name": "Akamai Technologies", | ||
"organization_name": null | ||
} | ||
} |
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,23 @@ | ||
# encoding: utf-8 | ||
require 'test_helper' | ||
|
||
class AbstractApiTest < GeocoderTestCase | ||
def setup | ||
Geocoder.configure(ip_lookup: :abstract_api) | ||
set_api_key!(:abstract_api) | ||
end | ||
|
||
def test_result_attributes | ||
result = Geocoder.search('2.19.128.50').first | ||
assert_equal 'Seattle, WA 98111, United States', result.address | ||
assert_equal 'Seattle', result.city | ||
assert_equal 'WA', result.state_code | ||
assert_equal 'Washington', result.state | ||
assert_equal 'United States', result.country | ||
assert_equal 'US', result.country_code | ||
assert_equal '98111', result.postal_code | ||
assert_equal 47.6032, result.latitude | ||
assert_equal(-122.3412, result.longitude) | ||
assert_equal [47.6032, -122.3412], result.coordinates | ||
end | ||
end |