| Module | Ym4r::YahooMaps::BuildingBlock::LocalSearch |
| In: |
lib/ym4r/yahoo_maps/building_block/local_search.rb
|
Send a request to the local search REST API V3.
# File lib/ym4r/yahoo_maps/building_block/local_search.rb, line 11
11: def self.get(param)
12:
13: unless param.has_key?(:street) or
14: param.has_key?(:city) or
15: param.has_key?(:state) or
16: param.has_key?(:zip) or
17: param.has_key?(:location) or
18: (param.has_key?(:longitude) and param.has_key?(:latitude))
19: raise MissingParameterException.new("Missing location data for the Yahoo! Maps Local Search service")
20: end
21:
22: unless param.has_key?(:query) or
23: param.has_key?(:listing_id)
24: raise MissingParameterException.new("Missing query data for the Yahoo! Maps Local Search service")
25: end
26:
27: url = "http://api.local.yahoo.com/LocalSearchService/V3/localSearch?appid=#{Ym4r::YahooMaps::APP_ID}&"
28: url << "query=#{param[:query]}&" if param.has_key?(:query)
29: url << "listing_id=#{param[:query]}&" if param.has_key?(:listing_id)
30: url << "results=#{param[:results]}&" if param.has_key?(:results)
31: url << "start=#{param[:start]}&" if param.has_key?(:start)
32: url << "sort=#{param[:sort]}&" if param.has_key?(:sort)
33: url << "radius=#{param[:radius]}&" if param.has_key?(:radius)
34: url << "street=#{param[:street]}&" if param.has_key?(:street)
35: url << "city=#{param[:city]}&" if param.has_key?(:city)
36: url << "state=#{param[:state]}&" if param.has_key?(:state)
37: url << "zip=#{param[:zip]}&" if param.has_key?(:zip)
38: url << "location=#{param[:location]}&" if param.has_key?(:location)
39: url << "latitude=#{param[:latitude]}&" if param.has_key?(:latitude)
40: url << "longitude=#{param[:longitude]}&" if param.has_key?(:longitude)
41: url << "category=#{param[:category]}&" if param.has_key?(:category)
42: url << "omit_category=#{param[:omit_category]}&" if param.has_key?(:omit_category)
43: url << "minimum_rating=#{param[:minimum_rating]}&" if param.has_key?(:minimum_rating)
44: url << "output=json"
45:
46: begin
47: json = open(URI.encode(url)).read
48: rescue OpenURI::HTTPError => error
49: raise BadRequestException.new(error.to_s)
50: rescue
51: raise ConnectionException.new("Unable to connect to Yahoo! Maps REST service")
52: end
53:
54: #see http://rubyforge.org/snippet/detail.php?type=snippet&id=29. Safe?
55: json_obj = eval(json.gsub(/(["'])\s*:\s*(['"0-9tfn\[{])/){"#{$1}=>#{$2}"})
56:
57: if json_obj.has_key?("Error")
58: raise RateLimitExceededException.new("Rate limit exceeded for Yahoo! Maps Traffic REST service")
59: else
60: json_result_set = json_obj['ResultSet']
61:
62: result_set = LocalSearch::ResultSet.new(json_result_set['ResultSetMapUrl'],
63: json_result_set['totalResultsAvailable'].to_i,
64: json_result_set['totalResultsReturned'].to_i,
65: json_result_set['firstResultPosition'].to_i)
66:
67: unless json_result_set['Result'].nil?
68: json_results = [json_result_set['Result']].flatten #uniform processing in case there is only one result
69:
70: json_results.each do |json_result|
71:
72: #get the rating
73: json_rating = json_result['Rating']
74: rating = LocalSearch::Rating.new(json_rating['AverageRating'].to_f, #when NaN, converted to 0 but can be tested (since TotalRating is 0 in this case) with is_rated? on the rating object
75: json_rating['TotalRatings'].to_i,
76: json_rating['TotalReviews'].to_i,
77: Time.at(json_rating['LastReviewDate'].to_i),
78: json_rating['LastReviewIntro'])
79:
80: #get the categories
81: categories = []
82: unless json_result['Categories']['Category'].nil? #no category present in the result
83: json_categories = [json_result['Categories']['Category']].flatten #uniform processing in case there is only one category
84: json_categories.each do |json_category|
85: categories << LocalSearch::Category.new(json_category['id'].to_i,
86: json_category['content'])
87: end
88: end
89:
90: result_set << LocalSearch::Result.new(json_result['id'].to_i,
91: json_result['Title'],
92: json_result['Address'],
93: json_result['City'],
94: json_result['State'],
95: json_result['Phone'],
96: json_result['Latitude'].to_f,
97: json_result['Longitude'].to_f,
98: rating,
99: json_result['Distance'].to_f,
100: json_result['Url'],
101: json_result['ClickUrl'],
102: json_result['MapUrl'],
103: json_result['BusinessUrl'],
104: json_result['BusinessClickUrl'],
105: categories)
106:
107: end
108: end #unless json_result_set['Result'].nil?
109:
110: result_set
111:
112: end
113: end