class Google::Directions::Request

Constants

BASE_URL
DRIVING_MODE
GET_PATH

Attributes

params[R]
response[R]

Public Class Methods

new() click to toggle source
# File lib/google/directions/request.rb, line 13
def initialize
  session.base_url        = ((sign? || private_key) ? 'https://' : 'http://') + BASE_URL
  session.timeout         = Google::Directions.config.timeout
  session.connect_timeout = Google::Directions.config.connect_timeout
end

Public Instance Methods

get(params) click to toggle source
# File lib/google/directions/request.rb, line 19
def get(params)
  @params   = params.with_indifferent_access

  url = sign_url_if_needed(GET_PATH + '?' + parse_params.to_query)
  @response = session.get url

  response ? ::JSON.parse(response.body) : nil
end

Private Instance Methods

client_id() click to toggle source
# File lib/google/directions/request.rb, line 79
def client_id
  Google::Directions.config.client_id
end
missing(name) click to toggle source
# File lib/google/directions/request.rb, line 71
def missing(name)
  raise Error, "Missing parameter: #{name}"
end
parse_params() click to toggle source
# File lib/google/directions/request.rb, line 30
def parse_params
  parsed_params = {
    sensor:      params[:sensor] || false,
    origin:      params[:origin] || missing(:origin),
    destination: params[:destination] || missing(:destination)
  }

  parsed_params[:waypoints] = parse_waypoints if params[:waypoints]
  parsed_params[:mode] = params[:mode] || DRIVING_MODE

  set_auth_params!(parsed_params)

  parsed_params
end
parse_waypoints() click to toggle source
# File lib/google/directions/request.rb, line 49
def parse_waypoints
  optimize = !!params[:optimize] || false
  waypoints = params[:waypoints].join('|')

  "optimize:#{optimize}|#{waypoints}"
end
private_key() click to toggle source
# File lib/google/directions/request.rb, line 75
def private_key
  Google::Directions.config.private_key
end
session() click to toggle source
# File lib/google/directions/request.rb, line 67
def session
  @session ||= ::Patron::Session.new
end
set_auth_params!(parsed_params) click to toggle source
# File lib/google/directions/request.rb, line 56
def set_auth_params!(parsed_params)
  if sign?
    missing('client_id') unless client_id
    missing('private_key') unless private_key

    parsed_params[:client] = client_id
  elsif private_key
    parsed_params[:key] = private_key
  end
end
sign?() click to toggle source
# File lib/google/directions/request.rb, line 83
def sign?
  Google::Directions.config.sign
end
sign_url_if_needed(uri_with_params) click to toggle source
# File lib/google/directions/request.rb, line 45
def sign_url_if_needed(uri_with_params)
  sign? ? Encoder.new(uri_with_params, private_key).encode : uri_with_params
end