Class: GooglePlaces::Request
- Inherits:
-
Object
- Object
- GooglePlaces::Request
- Includes:
- HTTParty
- Defined in:
- lib/google_places/request.rb
Overview
This class performs the queries on the API
Constant Summary collapse
- NEARBY_SEARCH_URL =
'https://maps.googleapis.com/maps/api/place/nearbysearch/json'
- DETAILS_URL =
'https://maps.googleapis.com/maps/api/place/details/json'
- TEXT_SEARCH_URL =
'https://maps.googleapis.com/maps/api/place/textsearch/json'
- PAGETOKEN_URL =
'https://maps.googleapis.com/maps/api/place/search/json'
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#response ⇒ HTTParty::Response
The retrieved response from the API.
Class Method Summary collapse
-
.spot(options = {}) ⇒ Spot
Search for a Spot with a reference key.
-
.spots(options = {}) ⇒ Array<Spot>
Search for Spots at the provided location.
-
.spots_by_pagetoken(options = {}) ⇒ Array<Spot>
Search for Spots with a page token.
-
.spots_by_query(options = {}) ⇒ Array<Spot>
Search for Spots with a query.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(url, options) ⇒ Request
constructor
Create a new Request for a given uri and the provided params.
-
#parsed_response ⇒ String
Parse errors from the server respons, if any.
Constructor Details
#initialize(url, options) ⇒ Request
Create a new Request for a given uri and the provided params
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/google_places/request.rb', line 186 def initialize(url, ) = .delete(:retry_options) || {} [:status] ||= [] [:max] ||= 0 [:delay] ||= 5 [:status] = [[:status]] unless [:status].is_a?(Array) @response = self.class.get(url, :query => ) # puts "@response.request.last_uri.to_s" # puts @response.request.last_uri.to_s return unless [:max] > 0 && [:status].include?(@response.parsed_response['status']) retry_request = proc do for i in (1..[:max]) sleep([:delay]) @response = self.class.get(url, :query => ) break unless [:status].include?(@response.parsed_response['status']) end end if [:timeout] begin Timeout::timeout([:timeout]) do retry_request.call end rescue Timeout::Error raise RetryTimeoutError.new(@response) end else retry_request.call raise RetryError.new(@response) if [:status].include?(@response.parsed_response['status']) end end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/google_places/request.rb', line 6 def @options end |
#response ⇒ HTTParty::Response
Returns the retrieved response from the API.
5 6 7 |
# File 'lib/google_places/request.rb', line 5 def response @response end |
Class Method Details
.spot(options = {}) ⇒ Spot
Search for a Spot with a reference key
82 83 84 85 |
# File 'lib/google_places/request.rb', line 82 def self.spot( = {}) request = new(DETAILS_URL, ) request.parsed_response end |
.spots(options = {}) ⇒ Array<Spot>
Search for Spots at the provided location
56 57 58 59 |
# File 'lib/google_places/request.rb', line 56 def self.spots( = {}) request = new(NEARBY_SEARCH_URL, ) request.parsed_response end |
.spots_by_pagetoken(options = {}) ⇒ Array<Spot>
Search for Spots with a page token
140 141 142 143 |
# File 'lib/google_places/request.rb', line 140 def self.spots_by_pagetoken( = {}) request = new(PAGETOKEN_URL, ) request.parsed_response end |
.spots_by_query(options = {}) ⇒ Array<Spot>
Search for Spots with a query
120 121 122 123 |
# File 'lib/google_places/request.rb', line 120 def self.spots_by_query( = {}) request = new(TEXT_SEARCH_URL, ) request.parsed_response end |
Instance Method Details
#execute ⇒ Object
226 227 228 |
# File 'lib/google_places/request.rb', line 226 def execute @response = self.class.get(url, :query => ) end |
#parsed_response ⇒ String
Parse errors from the server respons, if any
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/google_places/request.rb', line 237 def parsed_response case @response.parsed_response['status'] when 'OK', 'ZERO_RESULTS' @response.parsed_response when 'OVER_QUERY_LIMIT' raise OverQueryLimitError.new(@response) when 'REQUEST_DENIED' raise RequestDeniedError.new(@response) when 'INVALID_REQUEST' raise InvalidRequestError.new(@response) when 'UNKNOWN_ERROR' raise UnknownError.new(@response) when 'NOT_FOUND' raise NotFoundError.new(@response) end end |