From 5437240132cd00cc7091eaab67884c509c6fd754 Mon Sep 17 00:00:00 2001 From: Christian Aust <git@kontakt.software-consultant.net> Date: Fri, 8 Feb 2013 17:48:11 +0100 Subject: [PATCH] Added test case for Ovi, restored Ruby 1.8 hash syntax --- lib/geocoder/lookups/ovi.rb | 30 ++++++-- lib/geocoder/results/ovi.rb | 22 ++++-- test/fixtures/ovi_madison_square_garden.json | 72 ++++++++++++++++++++ test/fixtures/ovi_no_results.json | 8 +++ test/test_helper.rb | 13 ++++ 5 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/ovi_madison_square_garden.json create mode 100644 test/fixtures/ovi_no_results.json diff --git a/lib/geocoder/lookups/ovi.rb b/lib/geocoder/lookups/ovi.rb index d02f7f62..8831bc88 100644 --- a/lib/geocoder/lookups/ovi.rb +++ b/lib/geocoder/lookups/ovi.rb @@ -1,7 +1,7 @@ # ovi.com store require 'geocoder/lookups/base' -require "geocoder/results/ovi" +require 'geocoder/results/ovi' module Geocoder::Lookup class Ovi < Base @@ -10,19 +10,35 @@ module Geocoder::Lookup def results(query) return [] unless doc = fetch_data(query) - return [] unless doc['Response'] - doc['Response']['View'].first['Result'] + return [] unless doc['Response'] && doc['Response']['View'] + if r=doc['Response']['View'] + return [] if r.nil? || !r.is_a?(Array) || r.empty? + return r.first['Result'] + end + [] end def query_url_params(query) super.merge( - searchtext:query.sanitized_text, - gen:1, - app_id:Geocoder::Configuration.api_key[0], - app_code:Geocoder::Configuration.api_key[1] + :searchtext=>query.sanitized_text, + :gen=>1, + :app_id=>api_key, + :app_code=>api_code ) end + def api_key + if a=Geocoder::Configuration.api_key + return a.first if a.is_a?(Array) + end + end + + def api_code + if a=Geocoder::Configuration.api_key + return a.last if a.is_a?(Array) + end + end + def query_url(query) "http://lbs.ovi.com/search/6.2/geocode.json?" + url_query_string(query) end diff --git a/lib/geocoder/results/ovi.rb b/lib/geocoder/results/ovi.rb index 1af8bb89..40dfd298 100644 --- a/lib/geocoder/results/ovi.rb +++ b/lib/geocoder/results/ovi.rb @@ -21,7 +21,7 @@ module Geocoder::Result def state fail unless d = @data['Location']['Address'] - d['State'] + d['County'] end def province @@ -29,17 +29,31 @@ module Geocoder::Result d['County'] end + def postal_code + fail unless d = @data['Location']['Address'] + d['PostalCode'] + end + + def city + fail unless d = @data['Location']['Address'] + d['City'] + end + def state_code - fail + fail unless d = @data['Location']['Address'] + d['State'] end def province_code - fail + fail unless d = @data['Location']['Address'] + d['State'] end def country fail unless d = @data['Location']['Address']['AdditionalData'] - d.find{|ad| ad['value'] if ad['key']=='CountryName'} + if v = d.find{|ad| ad['key']=='CountryName'} + return v['value'] + end end def country_code diff --git a/test/fixtures/ovi_madison_square_garden.json b/test/fixtures/ovi_madison_square_garden.json new file mode 100644 index 00000000..464b8f85 --- /dev/null +++ b/test/fixtures/ovi_madison_square_garden.json @@ -0,0 +1,72 @@ +{ + "Response": { + "MetaInfo": { + "Timestamp": "2013-02-08T16:26:39.382+0000" + }, + "View": [ + { + "_type": "SearchResultsViewType", + "ViewId": 0, + "Result": [ + { + "Relevance": 1.0, + "MatchLevel": "houseNumber", + "MatchQuality": { + "State": 1.0, + "City": 1.0, + "Street": [ + 1.0 + ], + "HouseNumber": 1.0 + }, + "MatchType": "pointAddress", + "Location": { + "LocationId": "NT_ArsGdYbpo6dqjPQel9gTID_4", + "LocationType": "point", + "DisplayPosition": { + "Latitude": 40.7504692, + "Longitude": -73.9933777 + }, + "NavigationPosition": [ + { + "Latitude": 40.7500305, + "Longitude": -73.9942398 + } + ], + "MapView": { + "TopLeft": { + "Latitude": 40.7515934, + "Longitude": -73.9948616 + }, + "BottomRight": { + "Latitude": 40.7493451, + "Longitude": -73.9918938 + } + }, + "Address": { + "Label": "4 Penn Plz, New York, NY 10001, United States", + "Country": "USA", + "State": "NY", + "County": "New York", + "City": "New York", + "Street": "Penn Plz", + "HouseNumber": "4", + "PostalCode": "10001", + "AdditionalData": [ + { + "value": "United States", + "key": "CountryName" + }, + { + "value": "New York", + "key": "StateName" + } + ] + } + } + } + ] + } + ] + } +} diff --git a/test/fixtures/ovi_no_results.json b/test/fixtures/ovi_no_results.json new file mode 100644 index 00000000..26fdaf93 --- /dev/null +++ b/test/fixtures/ovi_no_results.json @@ -0,0 +1,8 @@ +{ + "Response": { + "MetaInfo": { + "Timestamp": "2013-02-08T16:41:16.723+0000" + }, + "View": [] + } +} diff --git a/test/test_helper.rb b/test/test_helper.rb index b246afdd..f64e1b02 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -163,6 +163,19 @@ module Geocoder end end + class Ovi < Base + private #----------------------------------------------------------------- + def fetch_raw_data(query) + raise TimeoutError if query.text == "timeout" + raise SocketError if query.text == "socket_error" + file = case query.text + when "no results"; :no_results + else :madison_square_garden + end + read_fixture "ovi_#{file}.json" + end + end + class Nominatim < Base private #----------------------------------------------------------------- def fetch_raw_data(query) -- GitLab