Class: GoogleApiCustomization::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/google_api_customization/request.rb

Constant Summary collapse

PLACES_URL =
"https://maps.googleapis.com/maps/api/place/details/json"
RADAR_SEARCH_URL =
"https://maps.googleapis.com/maps/api/place/radarsearch/json"
TEXT_SEARCH_URL =
"https://maps.googleapis.com/maps/api/place/textsearch/json"
NEARBY_SEARCH_URL =
"https://maps.googleapis.com/maps/api/place/nearbysearch/json"
PHOTO_URL =
"https://maps.googleapis.com/maps/api/place/photo"
PAGETOKEN_URL =
"https://maps.googleapis.com/maps/api/place/search/json"
AUTOCOMPLETE_URL =
"https://maps.googleapis.com/maps/api/place/autocomplete/json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options, follow_redirects = true) ⇒ Request

Returns a new instance of Request.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/google_api_customization/request.rb', line 4

def options
  @options
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/google_api_customization/request.rb', line 3

def response
  @response
end

Class Method Details

.place(options = {}) ⇒ Object



26
27
28
29
# File 'lib/google_api_customization/request.rb', line 26

def self.place(options = {})
  request = new(PLACES_URL, options)
  request.parsed_response
end

Instance Method Details

#parsed_responseObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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