Class: GmapsDirections::API

Inherits:
Object
  • Object
show all
Defined in:
lib/gmaps_directions.rb

Constant Summary collapse

DIRECTIONS_API_BASE_URL =
"http://maps.googleapis.com/maps/api/directions/json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ API

Returns a new instance of API.



51
52
53
54
# File 'lib/gmaps_directions.rb', line 51

def initialize(attributes)
  @start_address = attributes[:start_address]
  @end_address   = attributes[:end_address]
end

Instance Attribute Details

#end_addressObject (readonly)

Returns the value of attribute end_address.



49
50
51
# File 'lib/gmaps_directions.rb', line 49

def end_address
  @end_address
end

#parsed_directions_responseObject (readonly)

Returns the value of attribute parsed_directions_response.



49
50
51
# File 'lib/gmaps_directions.rb', line 49

def parsed_directions_response
  @parsed_directions_response
end

#start_addressObject (readonly)

Returns the value of attribute start_address.



49
50
51
# File 'lib/gmaps_directions.rb', line 49

def start_address
  @start_address
end

Class Method Details

.find_directions(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gmaps_directions.rb', line 34

def find_directions(options = {})
  api_driver = new(:start_address => options[:from], :end_address => options[:to])
  api_driver.call_google
  if api_driver.parsed_directions_response.present? && api_driver.parsed_directions_response["routes"].present?
    return api_driver.parsed_directions_response["routes"].inject([]) do |routes, route_hash|
      routes << Route.new(route_hash, api_driver.parsed_directions_response["status"])
    end
  else
    return []
  end
end

Instance Method Details

#call_googleObject



56
57
58
59
# File 'lib/gmaps_directions.rb', line 56

def call_google
  response = Net::HTTP.get_response(directions_api_url).body
  @parsed_directions_response = Yajl::Parser.parse(response)
end