-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_directions_wrapper.rb
More file actions
51 lines (37 loc) · 1.38 KB
/
google_directions_wrapper.rb
File metadata and controls
51 lines (37 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'faraday'
require 'json'
class GoogleDirectionsWrapper
attr_reader :origin, :destination, :mode
attr_writer :response_json
def initialize(origin, destination, mode="driving")
@origin = origin
@destination = destination
@mode = mode
@response_json = JSON.parse(open("json_cached_output1.json").read)
end
def fetch
formated_origin = @origin.split.join('+')
formated_destination = @destination.split.join('+')
url = "http://maps.googleapis.com/maps/api/directions/json?origin=#{formated_origin}&destination=#{formated_destination}&sensor=false&mode=#{mode}"
# puts url
sleep 1
faraday_object = Faraday.get(url)
@response_json = JSON.parse(faraday_object.body)
# @response_json = JSON.parse(open("json_cached_output1.json").read)
# puts faraday_object
# puts @response_json
end
def route_time
@response_json['routes'][0]['legs'][0]['duration']['value']
end
def route_distance
@response_json['routes'][0]['legs'][0]['distance']['text'].split[0].to_f
end
def origin_zipcode
@response_json['routes'][0]['legs'][0]['start_address'].split[-2][0..-2]
end
def origin_lat_longs
@response_json['routes'][0]['legs'][0]['start_location']
end
end
# http://maps.googleapis.com/maps/api/directions/json?origin=#{formated_origin}&destination=#{formated_destination}&sensor=false&mode=#{mode}