Class: GoogleMapsAPI::Directions::Request

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

Constant Summary collapse

BASE_PATH =
"/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 = 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

#business_account?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/google_maps_api/directions/request.rb', line 59

def business_account?
  options.key?(:key) && options.key?(:client)
end

#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



51
52
53
# File 'lib/google_maps_api/directions/request.rb', line 51

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

#uriObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/google_maps_api/directions/request.rb', line 33

def uri
  base_host = GoogleMapsAPI::Core::BASE_HOST
  uri = "#{scheme}://#{base_host}#{BASE_PATH}"
  query_params = prepared_options.merge(
    {origin: origin, destination: destination}
  ).reject { |key, value| [:client, :channel].include?(key) }

  if business_account?
    query_params = query_params.reject { |key, value| [:key].include?(key) }
    uri = "#{uri}?#{to_query(query_params)}"
    uri = sign_uri(uri)
  else
    uri = URI("#{uri}?#{URI.encode_www_form(query_params)}")
  end

  uri
end