Skip to content

Commit

Permalink
Abstract API
Browse files Browse the repository at this point in the history
  • Loading branch information
randoum committed Mar 11, 2021
1 parent 5622d6e commit f7852aa
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README_API_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ Regional Street Address Lookups


IP Address Lookups

### Abstract API (`:abstract_api`)

* **API key**: required
* **Quota**: 20,000/day with free API Key, and un to 20,000,000/day for paid API keys
* **Region**: world
* **SSL support**: yes
* **Languages**: English
* **Documentation**: https://www.abstractapi.com/ip-geolocation-api#docs
* **Terms of Service**: https://www.abstractapi.com/legal
------------------

### Baidu IP (`:baidu_ip`)
Expand Down
1 change: 1 addition & 0 deletions lib/geocoder/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def street_services
def ip_services
@ip_services ||= [
:baidu_ip,
:abstract_api,
:freegeoip,
:geoip2,
:maxmind,
Expand Down
46 changes: 46 additions & 0 deletions lib/geocoder/lookups/abstract_api.rb
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
146 changes: 146 additions & 0 deletions lib/geocoder/results/abstract_api.rb
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
45 changes: 45 additions & 0 deletions test/fixtures/abstract_api
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
}
}
45 changes: 45 additions & 0 deletions test/fixtures/abstract_api_2_19_128_50
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
}
}
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ def default_fixture_filename
end
end

require 'geocoder/lookups/abstract_api'
class AbstractApi
private
def default_fixture_filename
"abstract_api"
end
end

require 'geocoder/lookups/freegeoip'
class Freegeoip
private
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lookup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_responds_to_name_method

def test_search_returns_empty_array_when_no_results
Geocoder::Lookup.all_services_except_test.each do |l|
next if [:ipgeolocation, :nationaal_georegister_nl].include?(l) # lookups that always return a result
next if [:abstract_api, :ipgeolocation, :nationaal_georegister_nl].include?(l) # lookups that always return a result
lookup = Geocoder::Lookup.get(l)
set_api_key!(l)
silence_warnings do
Expand Down
23 changes: 23 additions & 0 deletions test/unit/lookups/abstract_api_test.rb
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

0 comments on commit f7852aa

Please sign in to comment.