From 748788b0f52847e917f6d4222c3452b8b7b731bc Mon Sep 17 00:00:00 2001 From: Kieran Topping Date: Wed, 2 Apr 2014 23:11:00 +0100 Subject: [PATCH] Added ability to specify postcodes and countries in the geocoding api. --- lib/mapquest/services/geocoding.rb | 18 +++++++++++------- spec/fixtures/geocoding/location_country.json | 1 + spec/geocoding_spec.rb | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 spec/fixtures/geocoding/location_country.json diff --git a/lib/mapquest/services/geocoding.rb b/lib/mapquest/services/geocoding.rb index 3219b0a..e64b01d 100644 --- a/lib/mapquest/services/geocoding.rb +++ b/lib/mapquest/services/geocoding.rb @@ -4,7 +4,7 @@ module Services class Geocoding < Core API_LOCATION = :geocoding - VALID_OPTIONS = [:location,:maxResults,:thumbMaps] + VALID_OPTIONS = [:location,:maxResults,:thumbMaps,:country,:postalCode] class TooManyLocations < StandardError; end @@ -12,14 +12,18 @@ class TooManyLocations < StandardError; end # # Example: .address :location => "London, UK" # - # ==Required parameters - # * location [String] The location for which you wish to get data - # ==Optional parameters + # ==Optional parameter + # * location [String] The location for which you wish to get data. If you omit this, you must include one or more of the following options. + # ==Options parameters + # * :location [String] A single-line address to lookup, as detailed here: http://www.mapquestapi.com/common/locations.html#singlelinelocations + # * :postalCode [String] A postcode (or partial postcode) to lookup. + # * :country [String] The name of a country to restrict the results to. E.g. "GB", "France" # * :maxResults [Integer] The number of results to limit the response to. Defaults to -1 (-1 indicates no limit) # * :thumbMaps [Boolean] Return a URL to a static map thumbnail image for a location. Defaults to true - def address(location, options = {}) - raise ArgumentError, 'Method must receive a location (string)' unless location - options[:location] = location + def address(*args) + options = args.last.is_a?(Hash) ? args.last : {} + options[:location] = args.first if args.first.is_a? String + raise ArgumentError, 'Method must receive a location (string) or options (hash)' unless options.keys.count > 0 call_api self, 1, 'address', options end diff --git a/spec/fixtures/geocoding/location_country.json b/spec/fixtures/geocoding/location_country.json new file mode 100644 index 0000000..cb3661c --- /dev/null +++ b/spec/fixtures/geocoding/location_country.json @@ -0,0 +1 @@ +{"info":{"statuscode":0,"copyright":{"text":"\u00A9 2014 MapQuest, Inc.","imageUrl":"http://api.mqcdn.com/res/mqlogo.gif","imageAltText":"\u00A9 2014 MapQuest, Inc."},"messages":[]},"options":{"maxResults":-1,"thumbMaps":true,"ignoreLatLngInput":false},"results":[{"providedLocation":{"postalCode":"GU34","country":"GB"},"locations":[{"street":"","adminArea5":"Alton","adminArea5Type":"City","adminArea4":"","adminArea4Type":"County","adminArea3":"","adminArea3Type":"State","adminArea1":"GB","adminArea1Type":"Country","postalCode":"GU34","geocodeQualityCode":"Z1XAA","geocodeQuality":"ZIP","dragPoint":false,"sideOfStreet":"N","linkId":"286345969","unknownInput":"","type":"s","latLng":{"lat":51.13947,"lng":-0.985224},"displayLatLng":{"lat":51.13947,"lng":-0.985224},"mapUrl":"http://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2d6ynu,7a=o5-9u22gr&type=map&size=225,160&pois=purple-1,51.13947,-0.985224,0,0,|¢er=51.13947,-0.985224&zoom=15&rand=1732261632"}]}]} diff --git a/spec/geocoding_spec.rb b/spec/geocoding_spec.rb index 6d08fc1..92bf000 100644 --- a/spec/geocoding_spec.rb +++ b/spec/geocoding_spec.rb @@ -49,6 +49,23 @@ it { geocoding.options[:thumbMaps].should == false } end + context 'location is not provided, but a country and postcode are' do + subject(:geocoding) { mapquest.geocoding.address postalCode: "GU34", :country => "GB" } + + it 'should receive a location and country' do + fixture = fixture 'geocoding/location_country' + query = { + :key => 'xxx', + :postalCode => 'GU34', + :country => "GB" + } + stub_request(:get, 'www.mapquestapi.com/geocoding/v1/address').with(:query => query).to_return(:body => fixture) + end + + its(:providedLocation) { should == {postalCode: "GU34", country: "GB"} } + + end + context 'location and :maxResults are provided' do subject(:geocoding) { mapquest.geocoding.address 'London, UK', :maxResults => 2 }