-
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.
Add Melissa Data to API Options (#1521)
Add :melissa_street lookup.
- Loading branch information
Showing
11 changed files
with
308 additions
and
3 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/melissa_street" | ||
|
||
module Geocoder::Lookup | ||
class MelissaStreet < Base | ||
|
||
def name | ||
"MelissaStreet" | ||
end | ||
|
||
def results(query) | ||
return [] unless doc = fetch_data(query) | ||
|
||
if doc["TransmissionResults"] == "GE05" | ||
raise_error(Geocoder::InvalidApiKey) || | ||
Geocoder.log(:warn, "Melissa service error: invalid API key.") | ||
end | ||
|
||
return doc["Records"] | ||
end | ||
|
||
private # --------------------------------------------------------------- | ||
|
||
def base_query_url(query) | ||
"#{protocol}://address.melissadata.net/v3/WEB/GlobalAddress/doGlobalAddress?" | ||
end | ||
|
||
def query_url_params(query) | ||
params = { | ||
id: configuration.api_key, | ||
format: "JSON", | ||
a1: query.sanitized_text, | ||
loc: query.options[:city], | ||
admarea: query.options[:state], | ||
postal: query.options[:postal], | ||
ctry: query.options[:country] | ||
} | ||
params.merge(super) | ||
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,46 @@ | ||
require 'geocoder/results/base' | ||
|
||
module Geocoder::Result | ||
class MelissaStreet < Base | ||
def address(format = :full) | ||
@data['FormattedAddress'] | ||
end | ||
|
||
def street_address | ||
@data['AddressLine1'] | ||
end | ||
|
||
def suffix | ||
@data['ThoroughfareTrailingType'] | ||
end | ||
|
||
def number | ||
@data['PremisesNumber'] | ||
end | ||
|
||
def city | ||
@data['Locality'] | ||
end | ||
|
||
def state_code | ||
@data['AdministrativeArea'] | ||
end | ||
alias_method :state, :state_code | ||
|
||
def country | ||
@data['CountryName'] | ||
end | ||
|
||
def country_code | ||
@data['CountryISO3166_1_Alpha2'] | ||
end | ||
|
||
def postal_code | ||
@data['PostalCode'] | ||
end | ||
|
||
def coordinates | ||
[@data['Latitude'].to_f, @data['Longitude'].to_f] | ||
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,6 @@ | ||
{ | ||
"Version": "3.0.1.167", | ||
"TransmissionReference": "", | ||
"TransmissionResults": "GE05", | ||
"TotalRecords": "0" | ||
} |
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,73 @@ | ||
{ | ||
"Version": "3.0.1.167", | ||
"TransmissionReference": "", | ||
"TransmissionResults": "", | ||
"TotalRecords": "1", | ||
"Records": [ | ||
{ | ||
"RecordID": "1", | ||
"Results": "AC16,AE11,AV13,GS03", | ||
"FormattedAddress": "Frank H Ogawa Plaza;Oakland, CA 94612", | ||
"Organization": "", | ||
"AddressLine1": "Frank H Ogawa Plaza", | ||
"AddressLine2": "Oakland, CA 94612", | ||
"AddressLine3": "", | ||
"AddressLine4": "", | ||
"AddressLine5": "", | ||
"AddressLine6": "", | ||
"AddressLine7": "", | ||
"AddressLine8": "", | ||
"SubPremises": "", | ||
"DoubleDependentLocality": "", | ||
"DependentLocality": "", | ||
"Locality": "Oakland", | ||
"SubAdministrativeArea": "Alameda", | ||
"AdministrativeArea": "CA", | ||
"PostalCode": "94612", | ||
"PostalCodeType": " ", | ||
"AddressType": " ", | ||
"AddressKey": "94612000000", | ||
"SubNationalArea": "", | ||
"CountryName": "United States of America", | ||
"CountryISO3166_1_Alpha2": "US", | ||
"CountryISO3166_1_Alpha3": "USA", | ||
"CountryISO3166_1_Numeric": "840", | ||
"CountrySubdivisionCode": "US-CA", | ||
"Thoroughfare": "Frank H Ogawa Plz", | ||
"ThoroughfarePreDirection": "", | ||
"ThoroughfareLeadingType": "", | ||
"ThoroughfareName": "Frank H Ogawa", | ||
"ThoroughfareTrailingType": "Plz", | ||
"ThoroughfarePostDirection": "", | ||
"DependentThoroughfare": "", | ||
"DependentThoroughfarePreDirection": "", | ||
"DependentThoroughfareLeadingType": "", | ||
"DependentThoroughfareName": "", | ||
"DependentThoroughfareTrailingType": "", | ||
"DependentThoroughfarePostDirection": "", | ||
"Building": "", | ||
"PremisesType": "", | ||
"PremisesNumber": "", | ||
"SubPremisesType": "", | ||
"SubPremisesNumber": "", | ||
"PostBox": "", | ||
"Latitude": "37.806200", | ||
"Longitude": "-122.268900", | ||
"DeliveryIndicator": "U", | ||
"MelissaAddressKey": "", | ||
"MelissaAddressKeyBase": "", | ||
"PostOfficeLocation": "", | ||
"SubPremiseLevel": "", | ||
"SubPremiseLevelType": "", | ||
"SubPremiseLevelNumber": "", | ||
"SubBuilding": "", | ||
"SubBuildingType": "", | ||
"SubBuildingNumber": "", | ||
"UTC": "UTC-08:00", | ||
"DST": "Y", | ||
"DeliveryPointSuffix": "", | ||
"CensusKey": "060014029001027", | ||
"Extras": {} | ||
} | ||
] | ||
} |
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,73 @@ | ||
{ | ||
"Version": "3.0.1.167", | ||
"TransmissionReference": "", | ||
"TransmissionResults": "", | ||
"TotalRecords": "1", | ||
"Records": [ | ||
{ | ||
"RecordID": "1", | ||
"Results": "AC10,AC14,AC16,AV14,GS05", | ||
"FormattedAddress": "1 Frank H Ogawa Plz Fl 3;Oakland, CA 94612-1932", | ||
"Organization": "", | ||
"AddressLine1": "1 Frank H Ogawa Plz Fl 3", | ||
"AddressLine2": "Oakland, CA 94612-1932", | ||
"AddressLine3": "", | ||
"AddressLine4": "", | ||
"AddressLine5": "", | ||
"AddressLine6": "", | ||
"AddressLine7": "", | ||
"AddressLine8": "", | ||
"SubPremises": "Fl 3", | ||
"DoubleDependentLocality": "", | ||
"DependentLocality": "", | ||
"Locality": "Oakland", | ||
"SubAdministrativeArea": "Alameda", | ||
"AdministrativeArea": "CA", | ||
"PostalCode": "94612-1932", | ||
"PostalCodeType": " ", | ||
"AddressType": "H", | ||
"AddressKey": "94612193299", | ||
"SubNationalArea": "", | ||
"CountryName": "United States of America", | ||
"CountryISO3166_1_Alpha2": "US", | ||
"CountryISO3166_1_Alpha3": "USA", | ||
"CountryISO3166_1_Numeric": "840", | ||
"CountrySubdivisionCode": "US-CA", | ||
"Thoroughfare": "Frank H Ogawa Plz", | ||
"ThoroughfarePreDirection": "", | ||
"ThoroughfareLeadingType": "", | ||
"ThoroughfareName": "Frank H Ogawa", | ||
"ThoroughfareTrailingType": "Plz", | ||
"ThoroughfarePostDirection": "", | ||
"DependentThoroughfare": "", | ||
"DependentThoroughfarePreDirection": "", | ||
"DependentThoroughfareLeadingType": "", | ||
"DependentThoroughfareName": "", | ||
"DependentThoroughfareTrailingType": "", | ||
"DependentThoroughfarePostDirection": "", | ||
"Building": "", | ||
"PremisesType": "", | ||
"PremisesNumber": "1", | ||
"SubPremisesType": "Fl", | ||
"SubPremisesNumber": "3", | ||
"PostBox": "", | ||
"Latitude": "37.805402", | ||
"Longitude": "-122.272797", | ||
"DeliveryIndicator": "B", | ||
"MelissaAddressKey": "9772863955", | ||
"MelissaAddressKeyBase": "", | ||
"PostOfficeLocation": "", | ||
"SubPremiseLevel": "", | ||
"SubPremiseLevelType": "", | ||
"SubPremiseLevelNumber": "", | ||
"SubBuilding": "", | ||
"SubBuildingType": "", | ||
"SubBuildingNumber": "", | ||
"UTC": "UTC-08:00", | ||
"DST": "Y", | ||
"DeliveryPointSuffix": "", | ||
"CensusKey": "060014028002040", | ||
"Extras": {} | ||
} | ||
] | ||
} |
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,35 @@ | ||
# encoding: utf-8 | ||
require 'test_helper' | ||
|
||
class MelissaStreetTest < GeocoderTestCase | ||
|
||
def setup | ||
Geocoder.configure(lookup: :melissa_street) | ||
set_api_key!(:melissa_street) | ||
end | ||
|
||
def test_result_components | ||
result = Geocoder.search("1 Frank H Ogawa Plz Fl 3").first | ||
assert_equal "1", result.number | ||
assert_equal "1 Frank H Ogawa Plz Fl 3", result.street_address | ||
assert_equal "Plz", result.suffix | ||
assert_equal "CA", result.state | ||
assert_equal "94612-1932", result.postal_code | ||
assert_equal "Oakland", result.city | ||
assert_equal "US", result.country_code | ||
assert_equal "United States of America", result.country | ||
assert_equal([37.805402, -122.272797], result.coordinates) | ||
end | ||
|
||
def test_low_accuracy | ||
result = Geocoder.search("low accuracy").first | ||
assert_equal "United States of America", result.country | ||
end | ||
|
||
def test_raises_api_key_exception | ||
Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey]) | ||
assert_raises Geocoder::InvalidApiKey do | ||
Geocoder.search("invalid key") | ||
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