Skip to content

Instantly share code, notes, and snippets.

@developish
Created May 22, 2010 23:26
Show Gist options
  • Save developish/410458 to your computer and use it in GitHub Desktop.
Save developish/410458 to your computer and use it in GitHub Desktop.
Simple postal code search using the Geonames web service
require 'rubygems'
require 'geonames' # ppe-ruby-geonames
class PostalCodeSearch
include Geonames
def initialize(postal_code, country_code = "US")
@postal_code = postal_code
@country_code = country_code
@response = nil
end
def hash
return hash_from_response(@response) if @response
search = PostalCodeSearchCriteria.new
search.postal_code = @postal_code
search.country_code = @country_code
@response = WebService.postal_code_search(search)
@response.empty? ? nil : hash_from_response(@response)
end
def latlng
[hash[:latitude], hash[:longitude]] if hash
end
def hash_from_response(response)
{
:latitude => response.first.latitude,
:longitude => response.first.longitude
}
end
end
# PostalCodeSearch.new("92064").hash
# PostalCodeSearch.new("V6Z 2R9","CA").latlng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment