diff --git a/README.md b/README.md index 5dd78e2f9477c1c540d489b242ccb0ce4399fa88..647960b95635c59b0803df32e253c838c45222b8 100644 --- a/README.md +++ b/README.md @@ -457,6 +457,17 @@ The [Google Places Details API](https://developers.google.com/places/documentati * **Terms of Service**: http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy * **Limitations**: Please limit request rate to 1 per second and include your contact information in User-Agent headers (eg: `Geocoder.configure(:http_headers => { "User-Agent" => "your contact info" })`). [Data licensed under Open Database License (ODbL) (you must provide attribution).](http://www.openstreetmap.org/copyright) +#### LocationIQ (`:location_iq`) + +* **API key**: required +* **Quota**: 6 request/second (30k req/day), then ability to purchase more +* **Region**: world +* **SSL support**: yes +* **Languages**: ? +* **Documentation**: http://locationiq.org/#docs +* **Terms of Service**: https://unwiredlabs.com/tos +* **Limitations**: [Data licensed under Open Database License (ODbL) (you must provide attribution).](http://www.openstreetmap.org/copyright) + #### OpenCageData (`:opencagedata`) * **API key**: required diff --git a/lib/geocoder/lookup.rb b/lib/geocoder/lookup.rb index fdc4a0705b09020079320b1e25fb3ffea3ffb3db..b704d7cd1d50540a3525d7629a8294e96590af6a 100644 --- a/lib/geocoder/lookup.rb +++ b/lib/geocoder/lookup.rb @@ -23,6 +23,7 @@ module Geocoder # def street_services @street_services ||= [ + :location_iq, :dstk, :esri, :google, diff --git a/lib/geocoder/lookups/location_iq.rb b/lib/geocoder/lookups/location_iq.rb new file mode 100644 index 0000000000000000000000000000000000000000..d362c1b35735b8b1d43e43d819141e1d14220a2c --- /dev/null +++ b/lib/geocoder/lookups/location_iq.rb @@ -0,0 +1,32 @@ +require 'geocoder/lookups/nominatim' +require "geocoder/results/location_iq" + +module Geocoder::Lookup + class LocationIq < Nominatim + def name + "LocationIq" + end + + def required_api_key_parts + ["api_key"] + end + + def query_url(query) + method = query.reverse_geocode? ? "reverse.php" : "search.php" + host = configuration[:host] || "locationiq.org/v1" + "#{protocol}://#{host}/#{method}?key=#{configuration.api_key}&" + url_query_string(query) + end + + private + + def results(query) + return [] unless doc = fetch_data(query) + + if !doc.is_a?(Array) && doc['error'] =~ /Invalid\skey/ + raise_error(Geocoder::InvalidApiKey, doc['error']) + end + + doc.is_a?(Array) ? doc : [doc] + end + end +end diff --git a/lib/geocoder/results/location_iq.rb b/lib/geocoder/results/location_iq.rb new file mode 100644 index 0000000000000000000000000000000000000000..7265212c95b96be55bb9d56614c3c429e6a8253c --- /dev/null +++ b/lib/geocoder/results/location_iq.rb @@ -0,0 +1,6 @@ +require 'geocoder/results/nominatim' + +module Geocoder::Result + class LocationIq < Nominatim + end +end \ No newline at end of file diff --git a/test/fixtures/location_iq_invalid_api_key b/test/fixtures/location_iq_invalid_api_key new file mode 100644 index 0000000000000000000000000000000000000000..636cff25ac7a882ca54d235b08c2b74904f1a1f5 --- /dev/null +++ b/test/fixtures/location_iq_invalid_api_key @@ -0,0 +1,3 @@ +{ + "error": "Invalid key" +} diff --git a/test/fixtures/location_iq_madison_square_garden b/test/fixtures/location_iq_madison_square_garden new file mode 100644 index 0000000000000000000000000000000000000000..b1c03ffd45cb3c1a34274d64158b0d13acf06474 --- /dev/null +++ b/test/fixtures/location_iq_madison_square_garden @@ -0,0 +1,150 @@ +[ + + { + "place_id": "30632629", + "licence": "Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.", + "osm_type": "way", + "osm_id": "24801588", + "boundingbox": [ + "40.749828338623", + "40.7511596679688", + "-73.9943389892578", + "-73.9926528930664" + ], + "polygonpoints": [ + [ + "-73.9943346", + "40.7503638" + ], + [ + "-73.9942745", + "40.7504158" + ], + [ + "-73.9942593", + "40.750629" + ], + [ + "-73.9941343", + "40.7508432" + ], + [ + "-73.9939794", + "40.7509703" + ], + [ + "-73.9938042", + "40.7510532" + ], + [ + "-73.9938025", + "40.7511311" + ], + [ + "-73.9936051", + "40.7511571" + ], + [ + "-73.9935673", + "40.751105" + ], + [ + "-73.9934095", + "40.7511089" + ], + [ + "-73.9931235", + "40.7510548" + ], + [ + "-73.9928863", + "40.7509311" + ], + [ + "-73.9928068", + "40.750949" + ], + [ + "-73.992721", + "40.7508515" + ], + [ + "-73.9927444", + "40.7507889" + ], + [ + "-73.9926693", + "40.7506457" + ], + [ + "-73.9926597", + "40.7503657" + ], + [ + "-73.9928305", + "40.7500953" + ], + [ + "-73.9929757", + "40.7499911" + ], + [ + "-73.9931281", + "40.7499238" + ], + [ + "-73.993133", + "40.7498631" + ], + [ + "-73.9932961", + "40.7498306" + ], + [ + "-73.9933664", + "40.7498742" + ], + [ + "-73.993471", + "40.7498701" + ], + [ + "-73.9938023", + "40.7499263" + ], + [ + "-73.9940703", + "40.7500756" + ], + [ + "-73.9941876", + "40.7502038" + ], + [ + "-73.9942831", + "40.7502142" + ], + [ + "-73.9943346", + "40.7503638" + ] + ], + "lat": "40.7504928941818", + "lon": "-73.993466492276", + "display_name": "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America", + "class": "leisure", + "type": "stadium", + "address": { + "stadium": "Madison Square Garden", + "road": "West 31st Street", + "suburb": "Long Island City", + "city": "New York City", + "county": "New York", + "state": "New York", + "postcode": "10001", + "country": "United States of America", + "country_code": "us" + } + } + +] diff --git a/test/fixtures/location_iq_no_results b/test/fixtures/location_iq_no_results new file mode 100644 index 0000000000000000000000000000000000000000..1e3ec7217afba05e6dcd456b0e89ec7f17bf7b55 --- /dev/null +++ b/test/fixtures/location_iq_no_results @@ -0,0 +1 @@ +[ ] diff --git a/test/fixtures/location_iq_over_limit b/test/fixtures/location_iq_over_limit new file mode 100644 index 0000000000000000000000000000000000000000..9b9f64d101860b2e098573d63a815d06b8ce2cb7 --- /dev/null +++ b/test/fixtures/location_iq_over_limit @@ -0,0 +1 @@ +<html>\n<head>\n<title>Bandwidth limit exceeded</title>\n</head>\n<body>\n<h1>Bandwidth limit exceeded</h1>\n\n<p>You have been temporarily blocked because you have been overusing OSM's geocoding service or because you have not provided sufficient identification of your application. This block will be automatically lifted after a while. Please take the time and adapt your scripts to reduce the number of requests and make sure that you send a valid UserAgent or Referer.</p>\n\n<p>For more information, consult the <a href=\"http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy\">usage policy</a> for the OSM Nominatim server.\n</body>\n</head>\n diff --git a/test/test_helper.rb b/test/test_helper.rb index dda4330bb1204138f3a9e193d18df38a72fe1d6a..54a580dc3b7df20df85412da9bb204d51e8b78e6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -181,6 +181,14 @@ module Geocoder end end + require 'geocoder/lookups/location_iq' + class LocationIq + private + def fixture_prefix + "location_iq" + end + end + require 'geocoder/lookups/yandex' class Yandex private diff --git a/test/unit/lookups/location_iq_test.rb b/test/unit/lookups/location_iq_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..893975d17b7674252b8f16a57e4190a88ebbe04c --- /dev/null +++ b/test/unit/lookups/location_iq_test.rb @@ -0,0 +1,24 @@ +# encoding: utf-8 +require 'unit/lookups/nominatim_test' +require 'test_helper' + +class LocationIq < NominatimTest + + def setup + Geocoder.configure(lookup: :location_iq) + set_api_key!(:location_iq) + end + + def test_url_contains_api_key + Geocoder.configure(location_iq: {api_key: "abc123"}) + query = Geocoder::Query.new("Leadville, CO") + assert_equal "http://locationiq.org/v1/search.php?key=abc123&accept-language=en&addressdetails=1&format=json&q=Leadville%2C+CO", query.url + end + + def test_raises_exception_with_invalid_api_key + Geocoder.configure(always_raise: [Geocoder::InvalidApiKey]) + assert_raises Geocoder::InvalidApiKey do + Geocoder.search("invalid api key") + end + end +end