Skip to content

Commit

Permalink
Add country filter to mapquest
Browse files Browse the repository at this point in the history
Only return geo results for a specified country
  • Loading branch information
jcarpenter88 committed Apr 17, 2014
1 parent b18ba3d commit 1c2d395
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/graticule/geocoder/mapquest.rb
Expand Up @@ -11,13 +11,14 @@ module Geocoder #:nodoc:
#
class Mapquest < Base

def initialize(api_key, open = false)
def initialize(api_key, open = false, limited_to_country = nil)
@api_key = api_key
@url = if open
URI.parse('http://open.mapquestapi.com/geocoding/v1/address')
else
URI.parse('http://www.mapquestapi.com/geocoding/v1/address')
end
@country_filter = limited_to_country
end

# Locates +address+ returning a Location
Expand Down Expand Up @@ -101,7 +102,11 @@ def prepare_response(xml)

# Extracts a location from +xml+.
def parse_response(response) #:nodoc:
addr = response.result.locations.addresses.first
if @country_filter
addr = response.result.locations.addresses.select{|address| address.country == @country_filter}.first
else
addr = response.result.locations.addresses.first
end
Location.new(
:latitude => addr.latitude,
:longitude => addr.longitude,
Expand Down
171 changes: 171 additions & 0 deletions test/fixtures/responses/mapquest/multi_country_success.xml
@@ -0,0 +1,171 @@
<response>
<info>
<statusCode>0</statusCode>
<messages/>
<copyright>
<imageUrl>http://api.mqcdn.com/res/mqlogo.gif</imageUrl>
<imageAltText>© 2014 MapQuest, Inc.</imageAltText>
<text>© 2014 MapQuest, Inc.</text>
</copyright>
</info>
<results>
<result>
<providedLocation>
<location>12345 US</location>
</providedLocation>
<locations>
<location>
<street/>
<adminArea5 type="City">Us</adminArea5>
<adminArea3 type="State">Ile-de-France</adminArea3>
<adminArea4 type="County">Pontoise</adminArea4>
<postalCode/>
<adminArea1 type="Country">FR</adminArea1>
<geocodeQuality>CITY</geocodeQuality>
<geocodeQualityCode>A5XXX</geocodeQualityCode>
<dragPoint>false</dragPoint>
<sideOfStreet>N</sideOfStreet>
<displayLatLng>
<latLng>
<lat>49.100945</lat>
<lng>1.967247</lng>
</latLng>
</displayLatLng>
<linkId>0</linkId>
<type>s</type>
<latLng>
<lat>49.100945</lat>
<lng>1.967247</lng>
</latLng>
<mapUrl>
<![CDATA[
http://open.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2hutnq,8w=o5-9utlur&type=map&size=225,160&pois=purple-1,49.1009451,1.9672466,0,0|&center=49.1009451,1.9672466&zoom=12&rand=-1921049987
]]>
</mapUrl>
</location>
<location>
<street/>
<adminArea5 type="City">Us</adminArea5>
<adminArea3 type="State">Bourgogne</adminArea3>
<adminArea4 type="County">Louhans</adminArea4>
<postalCode/>
<adminArea1 type="Country">FR</adminArea1>
<geocodeQuality>CITY</geocodeQuality>
<geocodeQualityCode>A5XXX</geocodeQualityCode>
<dragPoint>false</dragPoint>
<sideOfStreet>N</sideOfStreet>
<displayLatLng>
<latLng>
<lat>46.818792</lat>
<lng>5.281419</lng>
</latLng>
</displayLatLng>
<linkId>0</linkId>
<type>s</type>
<latLng>
<lat>46.818792</lat>
<lng>5.281419</lng>
</latLng>
<mapUrl>
<![CDATA[
http://open.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2hutnq,8w=o5-9utlur&type=map&size=225,160&pois=purple-2,46.8187918,5.2814186,0,0|&center=46.8187918,5.2814186&zoom=12&rand=-1921049987
]]>
</mapUrl>
</location>
<location>
<street/>
<adminArea5 type="City">Us</adminArea5>
<adminArea3 type="State"/>
<adminArea4 type="County"/>
<postalCode/>
<adminArea1 type="Country">ID</adminArea1>
<geocodeQuality>CITY</geocodeQuality>
<geocodeQualityCode>A5XXX</geocodeQualityCode>
<dragPoint>false</dragPoint>
<sideOfStreet>N</sideOfStreet>
<displayLatLng>
<latLng>
<lat>-2.771829</lat>
<lng>132.186285</lng>
</latLng>
</displayLatLng>
<linkId>0</linkId>
<type>s</type>
<latLng>
<lat>-2.771829</lat>
<lng>132.186285</lng>
</latLng>
<mapUrl>
<![CDATA[
http://open.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2hutnq,8w=o5-9utlur&type=map&size=225,160&pois=purple-3,-2.771829,132.1862852,0,0|&center=-2.771829,132.1862852&zoom=12&rand=-1921049987
]]>
</mapUrl>
</location>
<location>
<street/>
<adminArea5 type="City"/>
<adminArea3 type="State">LA</adminArea3>
<adminArea4 type="County">Ascension Parish</adminArea4>
<postalCode>12345</postalCode>
<adminArea1 type="Country">US</adminArea1>
<geocodeQuality>ZIP</geocodeQuality>
<geocodeQualityCode>Z1XXA</geocodeQualityCode>
<dragPoint>false</dragPoint>
<sideOfStreet>N</sideOfStreet>
<displayLatLng>
<latLng>
<lat>30.280046</lat>
<lng>-90.786583</lng>
</latLng>
</displayLatLng>
<linkId>0</linkId>
<type>s</type>
<latLng>
<lat>30.280046</lat>
<lng>-90.786583</lng>
</latLng>
<mapUrl>
<![CDATA[
http://open.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2hutnq,8w=o5-9utlur&type=map&size=225,160&pois=purple-4,30.2800464733256,-90.7865829472683,0,0|&center=30.2800464733256,-90.7865829472683&zoom=12&rand=-1921049987
]]>
</mapUrl>
</location>
<location>
<street/>
<adminArea5 type="City">Uz</adminArea5>
<adminArea3 type="State">Midi-Pyrénées</adminArea3>
<adminArea4 type="County">Argelès-Gazost</adminArea4>
<postalCode/>
<adminArea1 type="Country">FR</adminArea1>
<geocodeQuality>CITY</geocodeQuality>
<geocodeQualityCode>A5XXX</geocodeQualityCode>
<dragPoint>false</dragPoint>
<sideOfStreet>N</sideOfStreet>
<displayLatLng>
<latLng>
<lat>42.966369</lat>
<lng>-0.085232</lng>
</latLng>
</displayLatLng>
<linkId>0</linkId>
<type>s</type>
<latLng>
<lat>42.966369</lat>
<lng>-0.085232</lng>
</latLng>
<mapUrl>
<![CDATA[
http://open.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2hutnq,8w=o5-9utlur&type=map&size=225,160&pois=purple-5,42.9663688,-0.0852324,0,0|&center=42.9663688,-0.0852324&zoom=12&rand=-1921049987
]]>
</mapUrl>
</location>
</locations>
</result>
</results>
<options>
<maxResults>-1</maxResults>
<thumbMaps>true</thumbMaps>
<ignoreLatLngInput>false</ignoreLatLngInput>
<boundingBox/>
</options>
</response>
19 changes: 18 additions & 1 deletion test/graticule/geocoder/mapquest_test.rb
Expand Up @@ -7,10 +7,10 @@ class MapquestTest < Test::Unit::TestCase
def setup
URI::HTTP.responses = []
URI::HTTP.uris = []
@geocoder = Mapquest.new('api_key')
end

def test_success
@geocoder = Mapquest.new('api_key')
prepare_response(:success)
location = Location.new(
:country => "US",
Expand All @@ -26,6 +26,7 @@ def test_success
end

def test_multi_result
@geocoder = Mapquest.new('api_key')
prepare_response(:multi_result)
location = Location.new(
:country => "US",
Expand All @@ -40,6 +41,22 @@ def test_multi_result
assert_equal(location, @geocoder.locate('217 Union St., NY'))
end

def test_multi_country
@geocoder = Mapquest.new('api_key', false, 'US')
prepare_response(:multi_country_success)
location = Location.new(
:country => "US",
:latitude => 30.280046,
:locality => "",
:longitude => -90.786583,
:postal_code => "12345",
:precision => :postal_code,
:region => "LA",
:street => nil
)
assert_equal(location, @geocoder.locate('12345 us'))
end

def test_query_construction
request = Mapquest::Request.new("217 Union St., NY", "api_key")
query = %Q{key=api_key&outFormat=xml&inFormat=kvp&location=217%20Union%20St.,%20NY}
Expand Down

0 comments on commit 1c2d395

Please sign in to comment.