traxo_api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +127 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/traxo/auth.rb +63 -0
- data/lib/traxo/client.rb +193 -0
- data/lib/traxo/endpoints/accounts.rb +24 -0
- data/lib/traxo/endpoints/member.rb +16 -0
- data/lib/traxo/endpoints/trips.rb +127 -0
- data/lib/traxo/version.rb +3 -0
- data/lib/traxo_api.rb +14 -0
- data/traxo_api.gemspec +34 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04b3987a9b2f6560ff147539d2198b375ab2cb9d
|
4
|
+
data.tar.gz: 0ee51e9725becd3de4c8b1c839aa3488d5d13dcd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb2acc3e7442ec0481a47fdafe37c8c019eb241012b957e4acaeefeee9053c13667c573e6200424d8effb420fdeee10a5610ebeadd6d1a4ca122a8b17a5ebdda
|
7
|
+
data.tar.gz: c3b04a2d332ef8167cadbde2de1c9edf4b582f876fd855fa1aed60bf53d1c4e3046c733e750e31437f46351bb1b402d785cd41549fe14b046c939259fc08f03e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Wil Chandler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
#traxo_api
|
2
|
+
|
3
|
+
The 'traxo_api' gem is a Ruby wrapper meant to simplify the processes of both authorizing applications and creating interactions with the [Traxo API](https://developer.traxo.com).
|
4
|
+
|
5
|
+
Currently, methods for the _member_, _accounts_, and _trips_ endpoints of Traxo's API have been implemented. More sections are hopefully soon to come.
|
6
|
+
|
7
|
+
###README Contents:
|
8
|
+
- [Installation](#toc-installation)
|
9
|
+
- [Usage](#toc-usage)
|
10
|
+
- [Authorization](#toc-auth)
|
11
|
+
- [CRUD](#toc-crud)
|
12
|
+
- [Documentation](#toc-docs)
|
13
|
+
- [License](#toc-license)
|
14
|
+
- [Contributing](#toc-contributing)
|
15
|
+
- [Links](#toc-links)
|
16
|
+
- [Contact](#toc-contact)
|
17
|
+
|
18
|
+
<a name="toc-installation"></a>
|
19
|
+
##Installation
|
20
|
+
|
21
|
+
_Note: this gem is currently in pre-release and should have its first non-pre-release within a few days_.
|
22
|
+
|
23
|
+
Add this line to your application's Gemfile:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'traxo_api', '~> 0.1.0pre'
|
27
|
+
```
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
$ gem install traxo_api --pre
|
36
|
+
|
37
|
+
<a name="toc-usage"></a>
|
38
|
+
##Usage
|
39
|
+
|
40
|
+
<a name="toc-auth"></a>
|
41
|
+
###Authorization
|
42
|
+
Traxo's API uses the OAuth 2.0 standard for authorization. Additionally, Traxo enforces two later security additions: a _redirect URL_ and a _state parameter_.
|
43
|
+
|
44
|
+
To gain authorization from a Traxo user, you will need to [register your application](https://developer.traxo.com/signup) with Traxo. Once registered, you will need to retrieve your your _client ID_ and _client secret_ from the API's website where you will also need to register a _redirect url_ for the application.
|
45
|
+
|
46
|
+
####Example of authorization controller flow
|
47
|
+
```ruby
|
48
|
+
class TraxoController < ApplicationController
|
49
|
+
|
50
|
+
def auth
|
51
|
+
t = Traxo::Auth.new('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URL')
|
52
|
+
state = 'SOME_STRING'
|
53
|
+
redirect_to t.request_code_url(state)
|
54
|
+
end
|
55
|
+
|
56
|
+
def auth_success
|
57
|
+
t = Traxo::Auth.new('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URL')
|
58
|
+
|
59
|
+
# this conditional is not required, but uses CSRF protection made possible
|
60
|
+
# by Traxo's enforcement of a state parameter in authorization requests
|
61
|
+
if params[:state] == 'SOME_STRING'
|
62
|
+
code = params[:code]
|
63
|
+
response = t.exchange_request_code(code)
|
64
|
+
|
65
|
+
access_token = response[:access_token] # used to authorize requests
|
66
|
+
lifetime = response[:expires_in] # seconds until access_token expires
|
67
|
+
refresh_token = response[:refresh_token] # used to request new tokens
|
68
|
+
|
69
|
+
# store tokens (and use elsewhere for CRUD requests)...
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
```
|
77
|
+
|
78
|
+
<a name="toc-crud"></a>
|
79
|
+
###CRUD
|
80
|
+
Once a user has authorized your application and you have a valid access token, you can start making CRUD (create, read, update, delete) requests to the Traxo API on their behalf.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
t = Traxo::Client.new('ACCESS_TOKEN', 'CLIENT_ID', 'CLIENT_SECRET')
|
84
|
+
t.get_member # => Hash of properties for user's Traxo account
|
85
|
+
|
86
|
+
args = {
|
87
|
+
begin_datetime: '2015-06-01', # can be String or Date or Datetime
|
88
|
+
end_datetime: '2015-06-08', # can be String or Date or Datetime
|
89
|
+
destination: 'Little Rock, AR',
|
90
|
+
headline: 'Good times in the Rock',
|
91
|
+
personal: true
|
92
|
+
}
|
93
|
+
t.create_trip(args) # => Hash of properties for the user's freshly created trip
|
94
|
+
```
|
95
|
+
|
96
|
+
<a name="toc-docs"></a>
|
97
|
+
## Documentation
|
98
|
+
[View the documentation](https://github.com/wilchandler/traxo_api/wiki)
|
99
|
+
Covers more in-depth usage and the public methods available within this gem.
|
100
|
+
|
101
|
+
<a name="toc-license"></a>
|
102
|
+
##License
|
103
|
+
[View MIT License](LICENSE.txt)
|
104
|
+
|
105
|
+
<a name="toc-contributing"></a>
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
If you find a bug, please feel free to open an issue. I aim to actively maintain this project for now as I continue to work towards implementing the remaining endpoint sections.
|
109
|
+
|
110
|
+
If you would like to contribute code of your own (whether to fix a bug or to add a feature), please feel free to do the following:
|
111
|
+
|
112
|
+
1. Fork it ( https://github.com/wilchandler/traxo_api/fork )
|
113
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
114
|
+
3. Make sure previous tests are passing (some are currently pending from my removing features). Additional tests for new code would also be appreciated.
|
115
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
116
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
117
|
+
5. Create a new Pull Request
|
118
|
+
|
119
|
+
<a name="toc-links"></a>
|
120
|
+
## Links
|
121
|
+
* [Traxo's user website](https://www.traxo.com/)
|
122
|
+
* [Traxo's developer website](https://developer.traxo.com/)
|
123
|
+
|
124
|
+
<a name="toc-contact"></a>
|
125
|
+
## Contact Author
|
126
|
+
[Website](http://www.wilchandler.me) | [Email](mailto:wilchandler2@gmail.com) | [Twitter](https://twitter.com/wil_chandler) | [LinkedIn](http://www.linkedin.com/in/wilchandler) | [GitHub](https://github.com/wilchandler)
|
127
|
+
_Note: the creator of this gem is not affiliated with Traxo._
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "traxo_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/traxo/auth.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module Traxo
|
2
|
+
|
3
|
+
class Auth
|
4
|
+
|
5
|
+
OAUTH_URL = "https://www.traxo.com/oauth/"
|
6
|
+
TOKEN_URL = "#{OAUTH_URL}token/"
|
7
|
+
|
8
|
+
def initialize(client_id, client_secret, oauth_redirect_url)
|
9
|
+
@client_id = client_id
|
10
|
+
@client_secret = client_secret
|
11
|
+
@oauth_redirect_url = oauth_redirect_url
|
12
|
+
end
|
13
|
+
|
14
|
+
def request_code_url(state)
|
15
|
+
check_state_parameter(state)
|
16
|
+
"#{OAUTH_URL}authenticate?client_id=#{@client_id}&response_type=code&redirect_uri=#{@oauth_redirect_url}&state=#{state}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def exchange_request_code(code)
|
20
|
+
data = token_request_data(code, @oauth_redirect_url)
|
21
|
+
response = Net::HTTP.post_form(token_uri, data)
|
22
|
+
JSON.parse(response.body, symbolize_names: true)
|
23
|
+
end
|
24
|
+
|
25
|
+
def exchange_refresh_token(token)
|
26
|
+
data = token_refresh_data(token)
|
27
|
+
response = Net::HTTP.post_form(token_uri, data)
|
28
|
+
JSON.parse(response.body, symbolize_names: true)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def token_uri
|
34
|
+
URI.parse TOKEN_URL
|
35
|
+
end
|
36
|
+
|
37
|
+
def token_request_data(code, oauth_redirect_url)
|
38
|
+
{
|
39
|
+
client_id: @client_id,
|
40
|
+
client_secret: @client_secret,
|
41
|
+
grant_type: 'authorization_code',
|
42
|
+
redirect_uri: @oauth_redirect_url,
|
43
|
+
code: code
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def token_refresh_data(token)
|
48
|
+
{
|
49
|
+
client_id: @client_id,
|
50
|
+
client_secret: @client_secret,
|
51
|
+
grant_type: 'refresh_token',
|
52
|
+
refresh_token: token
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def check_state_parameter(state)
|
57
|
+
if !state.is_a?(String) || state.empty?
|
58
|
+
raise ArgumentError.new('State parameter must be a (non-empty) string')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/lib/traxo/client.rb
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
module Traxo
|
2
|
+
|
3
|
+
class Client
|
4
|
+
attr_accessor :access_token
|
5
|
+
attr_reader :response_format, :raise_http_errors
|
6
|
+
|
7
|
+
API_URL = "https://api.traxo.com/v2/"
|
8
|
+
|
9
|
+
def initialize(access_token, client_id, client_secret, options = {})
|
10
|
+
@access_token = access_token
|
11
|
+
@client_secret = client_secret
|
12
|
+
@client_id = client_id
|
13
|
+
assign_options(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def return_body!
|
17
|
+
@response_format = :body
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def return_body_string!
|
22
|
+
@response_format = :body_string
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def return_headers!
|
27
|
+
@response_format = :headers
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def return_headers_string!
|
32
|
+
@response_format = :headers_string
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def return_code!
|
37
|
+
@response_format = :code
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def return_http_object!
|
42
|
+
@response_format = :http
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def ignore_http_errors!
|
47
|
+
@raise_http_errors = false
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def raise_http_errors!
|
52
|
+
@raise_http_errors = true
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def assign_options(options)
|
59
|
+
assign_response_format(options[:response_format])
|
60
|
+
assign_error_handling(options[:errors])
|
61
|
+
end
|
62
|
+
|
63
|
+
def assign_response_format(format)
|
64
|
+
if format
|
65
|
+
accepted = [:body, :body_string, :headers, :headers_string, :code, :http]
|
66
|
+
if accepted.include? format
|
67
|
+
@response_format = format
|
68
|
+
else
|
69
|
+
str = accepted.join(', :').insert(0, ':')
|
70
|
+
raise ArgumentError.new(":response_format must be one of the following: #{str}")
|
71
|
+
end
|
72
|
+
else
|
73
|
+
return_body!
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def assign_error_handling(action)
|
78
|
+
if action
|
79
|
+
if [:raise, :ignore].include? action
|
80
|
+
raise_http_errors! if action == :raise
|
81
|
+
ignore_http_errors! if action == :ignore
|
82
|
+
else
|
83
|
+
raise ArgumentError.new(':errors parameter must be either :raise or :ignore')
|
84
|
+
end
|
85
|
+
else
|
86
|
+
raise_http_errors!
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def make_http_request(uri)
|
91
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == "https") do |http|
|
92
|
+
yield(http, uri)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def get_request_with_token(url)
|
97
|
+
uri = URI.parse(url)
|
98
|
+
request = Net::HTTP::Get.new(uri)
|
99
|
+
attach_token(request)
|
100
|
+
response = make_http_request(uri) { |http| http.request(request) }
|
101
|
+
format_response(response)
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_request_with_token_and_client(url)
|
105
|
+
# TO BE IMPLEMENTED
|
106
|
+
# Some endpoints require client id and secret (most do not)
|
107
|
+
end
|
108
|
+
|
109
|
+
def post_request_with_token(url, data)
|
110
|
+
uri = URI.parse(url)
|
111
|
+
request = Net::HTTP::Post.new(uri)
|
112
|
+
attach_token(request)
|
113
|
+
attach_data(request, data)
|
114
|
+
response = make_http_request(uri) { |http| http.request(request) }
|
115
|
+
format_response(response)
|
116
|
+
end
|
117
|
+
|
118
|
+
def put_request_with_token(url, data)
|
119
|
+
uri = URI.parse(url)
|
120
|
+
request = Net::HTTP::Put.new(uri)
|
121
|
+
attach_token(request)
|
122
|
+
attach_data(request, data)
|
123
|
+
response = make_http_request(uri) { |http| http.request(request) }
|
124
|
+
format_response(response)
|
125
|
+
end
|
126
|
+
|
127
|
+
def delete_request_with_token(url)
|
128
|
+
uri = URI.parse(url)
|
129
|
+
request = Net::HTTP::Delete.new(uri)
|
130
|
+
attach_token(request)
|
131
|
+
response = make_http_request(uri) { |http| http.request(request) }
|
132
|
+
check_code(response) ? true : false
|
133
|
+
end
|
134
|
+
|
135
|
+
def query_string(data = {})
|
136
|
+
data.keep_if { |key, value| value }
|
137
|
+
(data.empty?) ? '' : "?#{ URI.encode_www_form(data)}"
|
138
|
+
end
|
139
|
+
|
140
|
+
def attach_token(request)
|
141
|
+
request['Authorization'] = "Bearer #{@access_token}"
|
142
|
+
end
|
143
|
+
|
144
|
+
def attach_data(request, data)
|
145
|
+
request.set_form_data(data)
|
146
|
+
end
|
147
|
+
|
148
|
+
def convert_time(time)
|
149
|
+
time = time.dup
|
150
|
+
if time.is_a? String
|
151
|
+
begin
|
152
|
+
time = Time.parse(time)
|
153
|
+
rescue
|
154
|
+
return nil
|
155
|
+
end
|
156
|
+
elsif !(time.is_a?(Date) || time.is_a?(Time))
|
157
|
+
return nil
|
158
|
+
end
|
159
|
+
time.iso8601
|
160
|
+
end
|
161
|
+
|
162
|
+
def format_response(response)
|
163
|
+
return false unless check_code(response)
|
164
|
+
|
165
|
+
case @response_format
|
166
|
+
when :http
|
167
|
+
response
|
168
|
+
when :body
|
169
|
+
body = response.body
|
170
|
+
body.to_s.empty? ? {} : JSON.parse(body, symbolize_names: true)
|
171
|
+
when :body_string
|
172
|
+
response.body
|
173
|
+
when :headers
|
174
|
+
headers = {}
|
175
|
+
response.header.each { |key| headers[key.to_sym] = response[key] }
|
176
|
+
headers
|
177
|
+
when :headers_string
|
178
|
+
response.to_json
|
179
|
+
when :code
|
180
|
+
response.code.to_i
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def check_code(response)
|
185
|
+
if @raise_http_errors
|
186
|
+
response.value || response
|
187
|
+
else
|
188
|
+
response.code <= '300'
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Traxo
|
2
|
+
|
3
|
+
class Client
|
4
|
+
def get_account(id)
|
5
|
+
url = "#{ API_URL}accounts/#{id}"
|
6
|
+
get_request_with_token(url)
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_accounts(options = {})
|
10
|
+
data = get_accounts_options(options)
|
11
|
+
url = "#{ API_URL}accounts#{ query_string(data) }"
|
12
|
+
get_request_with_token(url)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def get_accounts_options(args)
|
18
|
+
options = [:status, :classification, :offset, :limit]
|
19
|
+
args.select! { |a| options.include? a }
|
20
|
+
args
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Traxo
|
2
|
+
|
3
|
+
class Client
|
4
|
+
def get_member
|
5
|
+
url = "#{ API_URL }me"
|
6
|
+
get_request_with_token(url)
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_stream(args = {})
|
10
|
+
args.select! { |key| [:offset, :limit, :count].include? key }
|
11
|
+
url = "#{ API_URL }stream#{ query_string(args) }"
|
12
|
+
get_request_with_token(url)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
module Traxo
|
2
|
+
class Client
|
3
|
+
|
4
|
+
def get_trips(options = {})
|
5
|
+
data = get_trips_options(options)
|
6
|
+
url = "#{ API_URL }trips#{ query_string(data) }"
|
7
|
+
get_request_with_token(url)
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_trip(trip_id, options = {})
|
11
|
+
data = get_trip_options(options)
|
12
|
+
url = "#{ API_URL }trips/#{ trip_id }#{ query_string(data) }"
|
13
|
+
get_request_with_token(url)
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_current_trip(options = {})
|
17
|
+
data = get_trip_options(options)
|
18
|
+
url = "#{ API_URL }trips/current#{ query_string(data) }"
|
19
|
+
get_request_with_token(url)
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_upcoming_trips(options = {})
|
23
|
+
data = get_past_or_upcoming_trips_options(options)
|
24
|
+
url = "#{ API_URL }trips/upcoming#{ query_string(data) }"
|
25
|
+
get_request_with_token(url)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_past_trips(options = {})
|
29
|
+
data = get_past_or_upcoming_trips_options(options)
|
30
|
+
url = "#{ API_URL }trips/past#{ query_string(data) }"
|
31
|
+
get_request_with_token(url)
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_trip_oembed(trip_id)
|
35
|
+
url = "#{ API_URL }trips/oembed/#{trip_id}"
|
36
|
+
get_request_with_token(url)
|
37
|
+
end
|
38
|
+
|
39
|
+
def create_trip(arg)
|
40
|
+
data = create_trip_data(arg)
|
41
|
+
create_trip_check_required_params(data)
|
42
|
+
url = "#{ API_URL}trips"
|
43
|
+
post_request_with_token(url, data)
|
44
|
+
end
|
45
|
+
|
46
|
+
def update_trip(trip_id, args)
|
47
|
+
raise ArgumentError.new('Must provide an integer trip ID') unless trip_id.is_a?(Fixnum)
|
48
|
+
raise ArgumentError.new('Must provide a Hash of attributes') unless args.is_a? Hash
|
49
|
+
return false if args.empty?
|
50
|
+
data = create_trip_data_from_hash(args)
|
51
|
+
url = "#{ API_URL }trips/#{ trip_id }"
|
52
|
+
put_request_with_token(url, data)
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete_trip(trip_id)
|
56
|
+
raise ArgumentError.new('Must provide an integer trip ID') unless trip_id.is_a?(Fixnum)
|
57
|
+
url = "#{ API_URL }trips/#{ trip_id }"
|
58
|
+
delete_request_with_token(url)
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def get_trips_options(args)
|
64
|
+
# not implemented: count (via X-Total-Count header)
|
65
|
+
# until: Filter results changed until this UTC date/time (ISO8601)
|
66
|
+
# since: Filter results changed since this UTC date/time (ISO8601)
|
67
|
+
# recursive: Include changed sub-objects (requires: since or until)
|
68
|
+
# start: listed as defaulting to "today" on Traxo API Explorer (as of 5/12/15)
|
69
|
+
args = args.dup
|
70
|
+
|
71
|
+
unless args.empty?
|
72
|
+
options = [:segments, :start, :end, :since, :until, :status, :privacy,
|
73
|
+
:purpose, :count, :offset, :limit, :recursive]
|
74
|
+
args.select! { |a| options.include? a }
|
75
|
+
args.delete(:recursive) unless (args[:since] || args[:until])
|
76
|
+
end
|
77
|
+
args[:segments] = 1 if args[:segments]
|
78
|
+
args[:recursive] = 1 if args[:recursive]
|
79
|
+
{start: 'today'}.merge(args)
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_trip_options(args)
|
83
|
+
([1, true].include? args[:segments]) ? { segments: 1 } : {}
|
84
|
+
end
|
85
|
+
|
86
|
+
def get_past_or_upcoming_trips_options(args)
|
87
|
+
args = args.dup
|
88
|
+
args[:segments] = 1 if [true, 1, '1'].include? args[:segments]
|
89
|
+
options = [:segments, :privacy, :purpose, :limit, :offset]
|
90
|
+
args.select! { |a| options.include? a }
|
91
|
+
args
|
92
|
+
end
|
93
|
+
|
94
|
+
def create_trip_data(arg)
|
95
|
+
if arg.is_a? Hash
|
96
|
+
create_trip_data_from_hash(arg)
|
97
|
+
else
|
98
|
+
raise ArgumentError.new('Argument must be a Hash')
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def create_trip_data_from_hash(arg)
|
103
|
+
arg = arg.dup
|
104
|
+
options = [:destination, :begin_datetime, :end_datetime, :personal,
|
105
|
+
:business, :privacy, :headline, :first_name, :last_name]
|
106
|
+
arg.select! { |a| options.include? a }
|
107
|
+
|
108
|
+
negative = [false, -1, 0, 'no', 'No', 'n', 'N']
|
109
|
+
(arg[:business] = negative.include?(arg[:business]) ? 'N' : 'Y') if arg[:business]
|
110
|
+
(arg[:business] = negative.include?(arg[:business]) ? 'N' : 'Y') if arg[:personal]
|
111
|
+
arg[:begin_datetime] = convert_time(arg[:begin_datetime]) if arg[:begin_datetime]
|
112
|
+
arg[:end_datetime] = convert_time(arg[:end_datetime]) if arg[:end_datetime]
|
113
|
+
arg
|
114
|
+
end
|
115
|
+
|
116
|
+
def create_trip_check_required_params(arg)
|
117
|
+
if arg.is_a? Hash
|
118
|
+
valid = arg[:destination] && arg[:end_datetime] && arg[:begin_datetime]
|
119
|
+
end
|
120
|
+
unless valid
|
121
|
+
message = 'Argument must include destination, end_datetime, and begin_datetime'
|
122
|
+
raise ArgumentError.new(message)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
end
|
data/lib/traxo_api.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'traxo/version'
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'time'
|
5
|
+
require 'date'
|
6
|
+
require 'net/http'
|
7
|
+
require 'uri'
|
8
|
+
|
9
|
+
require 'traxo/auth'
|
10
|
+
require 'traxo/client'
|
11
|
+
|
12
|
+
require 'traxo/endpoints/member'
|
13
|
+
require 'traxo/endpoints/accounts'
|
14
|
+
require 'traxo/endpoints/trips'
|
data/traxo_api.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'traxo/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "traxo_api"
|
8
|
+
spec.version = Traxo::VERSION
|
9
|
+
spec.authors = ["Wil Chandler"]
|
10
|
+
spec.email = ["wilchandler2@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby interface for interacting with the Traxo API}
|
13
|
+
spec.description = %q{The traxo_api gem provides both 'authentication' and 'client' modules to simplify the process of gaining a user's authentication and making CRUD requests to the Traxo API on their behalf.}
|
14
|
+
spec.homepage = "https://github.com/wilchandler/traxo_api"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# # delete this section to allow pushing this gem to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
33
|
+
spec.add_development_dependency 'webmock', '~> 1.21'
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: traxo_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wil Chandler
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.21'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.21'
|
69
|
+
description: The traxo_api gem provides both 'authentication' and 'client' modules
|
70
|
+
to simplify the process of gaining a user's authentication and making CRUD requests
|
71
|
+
to the Traxo API on their behalf.
|
72
|
+
email:
|
73
|
+
- wilchandler2@gmail.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- ".travis.yml"
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/traxo/auth.rb
|
88
|
+
- lib/traxo/client.rb
|
89
|
+
- lib/traxo/endpoints/accounts.rb
|
90
|
+
- lib/traxo/endpoints/member.rb
|
91
|
+
- lib/traxo/endpoints/trips.rb
|
92
|
+
- lib/traxo/version.rb
|
93
|
+
- lib/traxo_api.rb
|
94
|
+
- traxo_api.gemspec
|
95
|
+
homepage: https://github.com/wilchandler/traxo_api
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.4.6
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Ruby interface for interacting with the Traxo API
|
119
|
+
test_files: []
|