Created
March 13, 2011 23:02
-
-
Save jpmckinney/868528 to your computer and use it in GitHub Desktop.
Access Yellow API with HTTParty
This file contains 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
# snippet from HackMTL 2010 | |
require 'httparty' | |
YELLOW_API_KEY = 'demo' | |
class String | |
# Transliterates French accents | |
def transliterate | |
{ /[àâ]/ => 'a', | |
/[éèêë]/ => 'e', | |
/[îï]/ => 'i', | |
/ô/ => 'o', | |
/[ùûü]/ => 'u', | |
}.reduce(self) do |s,map| | |
s.gsub map[0], map[1] | |
end | |
end | |
end | |
class Yellow | |
include HTTParty | |
base_uri 'api.sandbox.yellowapi.com' | |
def self.find_business(query) | |
get '/FindBusiness/', { | |
:query => { | |
:pgLen => '5', | |
:fmt => 'JSON', | |
:apikey => YELLOW_API_KEY, | |
:UID => '127.0.0.1', | |
}.merge(query), | |
} | |
end | |
def self.get_business_details(query) | |
get '/GetBusinessDetails/', { | |
:query => { | |
:fmt => 'JSON', | |
:apikey => YELLOW_API_KEY, | |
:UID => '127.0.0.1', | |
}.merge(query), | |
} | |
end | |
def self.find_business_details(query) | |
find_business(query).parsed_response['listings'].map{|x| | |
{ | |
'listingId' => x['id'], | |
'bus-name' => x['name'].transliterate.gsub(/\W/, '-').squeeze('-'), | |
'city' => x['address']['city'].transliterate, | |
'prov' => { | |
'AB' => 'Alberta', | |
'BC' => 'British-Columbia', | |
'MB' => 'Manitoba', | |
'NB' => 'New-Brunswick', | |
'NL' => 'Newfoundland-and-Labrador', | |
'NS' => 'Nova-Scotia', | |
'NT' => 'Northwest-Territories', | |
'NU' => 'Nunavut', | |
'PE' => 'Prince-Edward-Island', | |
'ON' => 'Ontario', | |
'QC' => 'Quebec', | |
'SK' => 'Saskatchewan', | |
'YT' => 'Yukon', | |
}[x['address']['prov']], | |
} | |
}.map{|x| Yellow.get_business_details(x) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment