traveltime-api-ruby 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/traveltime_api.rb +190 -0
- metadata +79 -0
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
# Class for using TravelTime API service. Learn more about TravelTime at
|
5
|
+
# www.traveltimeapp.com
|
6
|
+
#
|
7
|
+
# ## Defined types ##
|
8
|
+
#
|
9
|
+
# * TransportationMode - string with one of following values:
|
10
|
+
# "walking", "driving", "walking_train", "walking_bus", "public_transport"
|
11
|
+
#
|
12
|
+
# * EdgeType - describes type of the edge. String with one of following values:
|
13
|
+
# "car", "walk", "train", "bus", "cable_car", "plane", "ship"
|
14
|
+
#
|
15
|
+
# ## Thrown errors ##
|
16
|
+
#
|
17
|
+
# Each method that does a call to the server can raise TravelTimeAPI::BadRequest
|
18
|
+
# or TravelTimeAPI::ServerError errors.
|
19
|
+
#
|
20
|
+
class TravelTimeAPI
|
21
|
+
DEFAULT_URL = "http://api.traveltimeapp.com"
|
22
|
+
|
23
|
+
# Raised if API user made bad request.
|
24
|
+
class BadRequest < RuntimeError
|
25
|
+
# Server provided String code for this error.
|
26
|
+
attr_reader :code
|
27
|
+
# Server provided String explanation of error.
|
28
|
+
attr_reader :details
|
29
|
+
|
30
|
+
def initialize(code, details)
|
31
|
+
@code = code
|
32
|
+
@details = details
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_s
|
36
|
+
"TravelTimeAPI::BadRequest[#{@code}: #{details}]"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Raised if API server has encountered an error.
|
41
|
+
class ServerError < RuntimeError
|
42
|
+
# Human readable error message.
|
43
|
+
attr_reader :error
|
44
|
+
|
45
|
+
def initialize(error)
|
46
|
+
@error = error
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_s
|
50
|
+
"TravelTimeAPI::ServerError[#{@error}]"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Represents that API does not have data for this point/region.
|
55
|
+
class NoData < RuntimeError; end
|
56
|
+
|
57
|
+
# Result of #time_filter method.
|
58
|
+
#
|
59
|
+
# @param points [Hash] {"id1" => time_in_seconds, ...}
|
60
|
+
# @param accuracy [Symbol] :exact or :approx. Decrease travel time to switch
|
61
|
+
# from approximate to exact accuracy.
|
62
|
+
class TimeFilterResult < Struct.new(:points, :accuracy); end
|
63
|
+
|
64
|
+
# Result of #time_map method.
|
65
|
+
#
|
66
|
+
# @param shape [Array] array of polylines that represent areas reachable by given time.
|
67
|
+
# [Cluster, Cluster, ...], where Cluster is [[lat, lon], [lat, lon], ...]
|
68
|
+
# @param accuracy [Symbol] :exact or :approx. Decrease travel time to switch
|
69
|
+
# from approximate to exact accuracy.
|
70
|
+
class TimeMapResult < Struct.new(:shape, :accuracy); end
|
71
|
+
|
72
|
+
# Result of #routes method.
|
73
|
+
#
|
74
|
+
# * RouteDescriptionPart is a structure describing one part of the route:
|
75
|
+
# {
|
76
|
+
# "mode": EdgeType,
|
77
|
+
# # Array of coordinates for this route part. Can be used to draw a polyline
|
78
|
+
# # on a map.
|
79
|
+
# "coords": [[lat, lon], [lat, lon], [lat, lon], ...],
|
80
|
+
# "directions: String, # textual description in english of this route part
|
81
|
+
# "distance": Int, # distance covered in metres for this route part
|
82
|
+
# "time": Int # time taken to travel through this route part in seconds
|
83
|
+
# }
|
84
|
+
#
|
85
|
+
# @param routes [Hash] {"id1" -> [RouteDescriptionPart, RouteDescriptionPart, ...], ...}
|
86
|
+
class RoutesResult < Struct.new(:routes); end
|
87
|
+
|
88
|
+
# Initializes API. You need to specify your API key here.
|
89
|
+
def initialize(api_key, url=DEFAULT_URL)
|
90
|
+
@api_key = api_key
|
91
|
+
@url = url
|
92
|
+
end
|
93
|
+
|
94
|
+
# Takes input parameters and returns how long does it take to reach each point
|
95
|
+
# in chosen mode of transportation (in seconds).
|
96
|
+
#
|
97
|
+
# Points whose travel times which exceed _travel_time_ are not included in the
|
98
|
+
# result.
|
99
|
+
#
|
100
|
+
# @param start_time [Time] Time moment when we start our search.
|
101
|
+
# @param travel_time [Int] Max time in seconds to the point.
|
102
|
+
# @param mode [String] Transportation mode. See class documentation for info.
|
103
|
+
# @param origin [Array] [latitude, longtitude]
|
104
|
+
# @param points [Hash] Points th{"id1" => [latitude, longtitude], ...}
|
105
|
+
# @return [TravelTimeAPI::TimeFilterResult]
|
106
|
+
def time_filter(start_time, travel_time, mode, origin, points)
|
107
|
+
request = {
|
108
|
+
:start_time => format_time(start_time),
|
109
|
+
:travel_time => travel_time,
|
110
|
+
:mode => mode,
|
111
|
+
:origin => origin,
|
112
|
+
:points => points
|
113
|
+
}
|
114
|
+
|
115
|
+
response = post("/v2/time_filter", request)
|
116
|
+
TimeFilterResult.new(response["times"], response["accuracy"].to_sym)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Takes input parameters and returns polylines for each cluster that you can
|
120
|
+
# reach by given time.
|
121
|
+
#
|
122
|
+
# @param start_time [Time] Time moment when we start our search.
|
123
|
+
# @param travel_time [Int] Max time in seconds to the point.
|
124
|
+
# @param mode [String] Transportation mode. See class documentation for info.
|
125
|
+
# @param origin [Array] [latitude, longtitude]
|
126
|
+
# @param smooth [Boolean] Shall the returned shaped be smoothed?
|
127
|
+
# @return [TravelTimeAPI::TimeMapResult]
|
128
|
+
def time_map(start_time, travel_time, mode, origin, smooth)
|
129
|
+
request = {
|
130
|
+
:start_time => format_time(start_time),
|
131
|
+
:travel_time => travel_time,
|
132
|
+
:mode => mode,
|
133
|
+
:origin => origin,
|
134
|
+
:smooth => smooth
|
135
|
+
}
|
136
|
+
|
137
|
+
response = post("/v2/time_map", request)
|
138
|
+
TimeMapResult.new(response["shape"], response["accuracy"].to_sym)
|
139
|
+
end
|
140
|
+
|
141
|
+
# Takes input parameters and returns routes to each of the points within
|
142
|
+
# travel_time.
|
143
|
+
#
|
144
|
+
# Input parameters are the same as #travel_time.
|
145
|
+
# @return [TravelTimeAPI::RoutesResult]
|
146
|
+
def routes(start_time, travel_time, mode, origin, points)
|
147
|
+
request = {
|
148
|
+
:start_time => format_time(start_time),
|
149
|
+
:travel_time => travel_time,
|
150
|
+
:mode => mode,
|
151
|
+
:origin => origin,
|
152
|
+
:points => points
|
153
|
+
}
|
154
|
+
|
155
|
+
response = post("/v2/routes", request)
|
156
|
+
RoutesResult.new(response)
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
def format_time(time)
|
162
|
+
time.strftime("%FT%T%:z")
|
163
|
+
end
|
164
|
+
|
165
|
+
def post(action, request)
|
166
|
+
response = RestClient.post(
|
167
|
+
@url + action,
|
168
|
+
request.merge(:api_key => @api_key).to_json,
|
169
|
+
:content_type => :json
|
170
|
+
)
|
171
|
+
json = JSON.parse(response)
|
172
|
+
case json["code"]
|
173
|
+
when "no-data"
|
174
|
+
raise NoData
|
175
|
+
when nil
|
176
|
+
json
|
177
|
+
else
|
178
|
+
raise ServerError.new(
|
179
|
+
"code: #{json["code"]}, details: #{json["details"]}"
|
180
|
+
)
|
181
|
+
end
|
182
|
+
rescue RestClient::ExceptionWithResponse => e
|
183
|
+
raise ServerError.new(
|
184
|
+
"server error, http_status: #{e.http_code}, http_body: #{e.http_body}"
|
185
|
+
)
|
186
|
+
rescue JSON::ParserError => e
|
187
|
+
raise ServerError.new("Cannot parse server response as JSON", response)
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: traveltime-api-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Artūras Šlajus
|
9
|
+
- IGeolise
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-01-25 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rest-client
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.6.7
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 1.6.7
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: json
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 1.7.0
|
39
|
+
type: :runtime
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.7.0
|
47
|
+
description: ''
|
48
|
+
email:
|
49
|
+
- arturas@igeolise.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- lib/traveltime_api.rb
|
55
|
+
homepage: http://traveltimeapp.com
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.24
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Simple wrapper for TravelTime REST API.
|
79
|
+
test_files: []
|