@sources.push "geonames_reverse" if defined?(GEONAMES_USERNAME)
elsif @params[:query]
if @params[:query] =~ /^\d{5}(-\d{4})?$/
- @sources.push "us_postcode"
@sources.push "osm_nominatim"
elsif @params[:query] =~ /^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\s*[0-9][ABD-HJLNP-UW-Z]{2})$/i
@sources.push "uk_postcode"
end
end
- def search_us_postcode
- # get query parameters
- query = params[:query]
-
- # create result array
- @results = []
-
- # ask geocoder.us (they have a non-commercial use api)
- response = fetch_text("http://rpc.geocoder.us/service/csv?zip=#{escape_query(query)}")
-
- # parse the response
- unless response =~ /couldn't find this zip/
- data = response.split(/\s*,\s+/) # lat,long,town,state,zip
- @results.push(:lat => data[0], :lon => data[1],
- :zoom => POSTCODE_ZOOM,
- :prefix => "#{data[2]}, #{data[3]},",
- :name => data[4])
- end
-
- render :action => "results"
- rescue StandardError => ex
- @error = "Error contacting rpc.geocoder.us: #{ex}"
- render :action => "error"
- end
-
def search_uk_postcode
# get query parameters
query = params[:query]
search:
title:
latlon: 'Results from <a href="http://openstreetmap.org/">Internal</a>'
- us_postcode: 'Results from <a href="http://geocoder.us/">Geocoder.us</a>'
uk_postcode: 'Results from <a href="http://www.npemap.org.uk/">NPEMap / FreeThe Postcode</a>'
ca_postcode: 'Results from <a href="http://geocoder.ca/">Geocoder.CA</a>'
osm_nominatim: 'Results from <a href="http://nominatim.openstreetmap.org/">OpenStreetMap Nominatim</a>'
# geocoder
match "/search" => "geocoder#search", :via => :get, :as => :search
match "/geocoder/search_latlon" => "geocoder#search_latlon", :via => :get
- match "/geocoder/search_us_postcode" => "geocoder#search_us_postcode", :via => :get
match "/geocoder/search_uk_postcode" => "geocoder#search_uk_postcode", :via => :get
match "/geocoder/search_ca_postcode" => "geocoder#search_ca_postcode", :via => :get
match "/geocoder/search_osm_nominatim" => "geocoder#search_osm_nominatim", :via => :get
{ :path => "/geocoder/search_latlon", :method => :get },
{ :controller => "geocoder", :action => "search_latlon" }
)
- assert_routing(
- { :path => "/geocoder/search_us_postcode", :method => :get },
- { :controller => "geocoder", :action => "search_us_postcode" }
- )
assert_routing(
{ :path => "/geocoder/search_uk_postcode", :method => :get },
{ :controller => "geocoder", :action => "search_uk_postcode" }
].each do |code|
post :search, :params => { :query => code }
assert_response :success
- assert_equal %w[us_postcode osm_nominatim], assigns(:sources)
+ assert_equal %w[osm_nominatim], assigns(:sources)
end
end
results_check_error "Longitude 180.23 out of range"
end
- ##
- # Test the US postcode search
- def test_search_us_postcode
- with_http_stubs "geocoder_us" do
- get :search_us_postcode, :xhr => true,
- :params => { :query => "90210", :zoom => 10,
- :minlon => -0.559, :minlat => 51.217,
- :maxlon => 0.836, :maxlat => 51.766 }
- results_check :prefix => "Beverly Hills, CA,", :name => "90210",
- :lat => 34.088808, :lon => -118.40612
-
- get :search_us_postcode, :xhr => true,
- :params => { :query => "00000", :zoom => 10,
- :minlon => -0.559, :minlat => 51.217,
- :maxlon => 0.836, :maxlat => 51.766 }
- results_check
- end
- end
-
##
# Test the UK postcode search
def test_search_uk_postcode
+++ /dev/null
-/service/csv?zip=90210:
- code: 200
- body: "34.088808, -118.40612, Beverly Hills, CA, 90210"
-/service/csv?zip=00000:
- code: 200
- body: "1: couldn't find this zip code: 00000! sorry"