class GoogleApiCustomization::Request

Constants

AUTOCOMPLETE_URL
NEARBY_SEARCH_URL
PAGETOKEN_URL
PHOTO_URL
PLACES_URL
RADAR_SEARCH_URL
TEXT_SEARCH_URL

Attributes

options[R]
response[RW]

Public Class Methods

new(url, options, follow_redirects = true) click to toggle source
# File lib/google_api_customization/request.rb, line 31
def initialize(url, options, follow_redirects = true)
  retry_options = options.delete(:retry_options) || {}

  retry_options[:status] ||= []
  retry_options[:max]    ||= 0
  retry_options[:delay]  ||= 5

  retry_options[:status] = [retry_options[:status]] unless retry_options[:status].is_a?(Array)
  @response = self.class.get(url, :query => options, :follow_redirects => follow_redirects)

  # puts @response.request.last_uri.to_s

  return unless retry_options[:max] > 0 && retry_options[:status].include?(@response.parsed_response['status'])

  retry_request = proc do
    for i in (1..retry_options[:max])
      sleep(retry_options[:delay])

      @response = self.class.get(url, :query => options, :follow_redirects => follow_redirects)

      break unless retry_options[:status].include?(@response.parsed_response['status'])
    end
  end

  if retry_options[:timeout]
    begin
      Timeout::timeout(retry_options[:timeout]) do
        retry_request.call
      end
    rescue Timeout::Error
      raise RetryTimeoutError.new(@response)
    end
  else
    retry_request.call

    raise RetryError.new(@response) if retry_options[:status].include?(@response.parsed_response['status'])
  end
end
place(options = {}) click to toggle source
# File lib/google_api_customization/request.rb, line 26
def self.place(options = {})
  request = new(PLACES_URL, options)
  request.parsed_response
end

Public Instance Methods

parsed_response() click to toggle source
# File lib/google_api_customization/request.rb, line 71
def parsed_response

  return @response.headers["location"] if @response.code >= 300 and @response.code < 400
  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