Created
May 22, 2010 23:26
-
-
Save developish/410458 to your computer and use it in GitHub Desktop.
Simple postal code search using the Geonames web service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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