Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/mapquest/services/geocoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ module Services
class Geocoding < Core

API_LOCATION = :geocoding
VALID_OPTIONS = [:location,:maxResults,:thumbMaps]
VALID_OPTIONS = [:location,:maxResults,:thumbMaps,:country,:postalCode]

class TooManyLocations < StandardError; end

# Allows you to search for a single location and returns a response object of the found locations
#
# 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

Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/geocoding/location_country.json
Original file line number Diff line number Diff line change
@@ -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,|&center=51.13947,-0.985224&zoom=15&rand=1732261632"}]}]}
17 changes: 17 additions & 0 deletions spec/geocoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down