Class: GoogleMapsAPI::Directions::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/google_maps_api/directions/request.rb

Constant Summary collapse

BASE_PATH =
"maps.googleapis.com/maps/api/directions/json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination, options = {}) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
14
15
# File 'lib/google_maps_api/directions/request.rb', line 8

def initialize(origin, destination, options = {})
  origin = origin.to_ary.join(",") if origin.respond_to?(:to_ary)
  destination = destination.to_ary.join(",") if destination.respond_to?(:to_ary)
  @origin = origin
  @destination = destination
  @options = default_options.merge(options)
  @http_adapter = nil
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



6
7
8
# File 'lib/google_maps_api/directions/request.rb', line 6

def destination
  @destination
end

#http_adapterObject

Returns the value of attribute http_adapter.



6
7
8
# File 'lib/google_maps_api/directions/request.rb', line 6

def http_adapter
  @http_adapter
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/google_maps_api/directions/request.rb', line 6

def options
  @options
end

#originObject

Returns the value of attribute origin.



6
7
8
# File 'lib/google_maps_api/directions/request.rb', line 6

def origin
  @origin
end

Class Method Details

.build(origin, destination, options = {}) ⇒ Object



17
18
19
# File 'lib/google_maps_api/directions/request.rb', line 17

def self.build(origin, destination, options = {})
  self.new(origin, destination, options)
end

Instance Method Details

#performObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/google_maps_api/directions/request.rb', line 21

def perform
  response = http_adapter.get_response(uri)
  if response.is_a?(Net::HTTPSuccess)
    return GoogleMapsAPI::Directions::Response.from_json(response.body)
  else
    msg = "The response was not successful (200). Call #response for datails."
    exception = GoogleMapsAPI::Directions::ResponseError.new(msg)
    exception.response = response
    raise exception
  end
end

#schemeObject



40
41
42
# File 'lib/google_maps_api/directions/request.rb', line 40

def scheme
  options[:https] ? "https" : "http"
end

#uriObject



33
34
35
36
37
38
# File 'lib/google_maps_api/directions/request.rb', line 33

def uri
  uri = URI("#{scheme}://#{BASE_PATH}")
  query = prepared_options.merge({origin: origin, destination: destination})
  uri.query = URI.encode_www_form(query)
  uri
end