traxo_api 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04b3987a9b2f6560ff147539d2198b375ab2cb9d
4
- data.tar.gz: 0ee51e9725becd3de4c8b1c839aa3488d5d13dcd
3
+ metadata.gz: 908a877bad2c37d81e3118ca0d6003a4038e2e81
4
+ data.tar.gz: bce5e411c6ff950ef7a3580f1685a9128d1e0a89
5
5
  SHA512:
6
- metadata.gz: bb2acc3e7442ec0481a47fdafe37c8c019eb241012b957e4acaeefeee9053c13667c573e6200424d8effb420fdeee10a5610ebeadd6d1a4ca122a8b17a5ebdda
7
- data.tar.gz: c3b04a2d332ef8167cadbde2de1c9edf4b582f876fd855fa1aed60bf53d1c4e3046c733e750e31437f46351bb1b402d785cd41549fe14b046c939259fc08f03e
6
+ metadata.gz: c3f60c2f38a6c85d1f709815f1a83711c4927b16f8e732b7bcfd81b10593b26b1d2d1b2ef9ab8f2cd8374e5e0af9214d8ba7b244750b1c89052b6fe346cbe687
7
+ data.tar.gz: a411f7746280f1142861a8de5fdbfa464d3b8a650ef5bf07016dd136794b22feee6d033d4bbe71fbbdc4e6c7621071faaf50e790d06da95beede76f767769090
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  #traxo_api
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/traxo_api.svg)](http://badge.fury.io/rb/traxo_api)
4
+
3
5
  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
6
 
5
- Currently, methods for the _member_, _accounts_, and _trips_ endpoints of Traxo's API have been implemented. More sections are hopefully soon to come.
7
+ Currently, methods for the __member__, __accounts__, __trips__, and __air segments__ endpoints of Traxo's API have been implemented. More sections are hopefully soon to come.
6
8
 
7
9
  ###README Contents:
8
10
  - [Installation](#toc-installation)
@@ -18,12 +20,10 @@ Currently, methods for the _member_, _accounts_, and _trips_ endpoints of Traxo'
18
20
  <a name="toc-installation"></a>
19
21
  ##Installation
20
22
 
21
- _Note: this gem is currently in pre-release and should have its first non-pre-release within a few days_.
22
-
23
23
  Add this line to your application's Gemfile:
24
24
 
25
25
  ```ruby
26
- gem 'traxo_api', '~> 0.1.0pre'
26
+ gem 'traxo_api'
27
27
  ```
28
28
 
29
29
  And then execute:
@@ -32,7 +32,7 @@ And then execute:
32
32
 
33
33
  Or install it yourself as:
34
34
 
35
- $ gem install traxo_api --pre
35
+ $ gem install traxo_api
36
36
 
37
37
  <a name="toc-usage"></a>
38
38
  ##Usage
@@ -43,6 +43,8 @@ Traxo's API uses the OAuth 2.0 standard for authorization. Additionally, Traxo e
43
43
 
44
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
45
 
46
+ See the [__authorization documentation__](https://github.com/wilchandler/traxo_api/wiki/Authorization) for a list and detailing of available authorization methods.
47
+
46
48
  ####Example of authorization controller flow
47
49
  ```ruby
48
50
  class TraxoController < ApplicationController
@@ -77,8 +79,11 @@ end
77
79
 
78
80
  <a name="toc-crud"></a>
79
81
  ###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.
82
+ 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. There are a multiple response formats available, and http errors/failures can be configured to be _raise exceptions_ (default), _ignored_, or to _return false_ for the CRUD method being called.
83
+
84
+ See the [__endpoint documentation__](https://github.com/wilchandler/traxo_api/wiki/Client-for-Endpoints) for response formatting and error handling. Individual endpoint methods are also detailed in the documentation.
81
85
 
86
+ ####Example of getting user's Traxo details and creating a trip
82
87
  ```ruby
83
88
  t = Traxo::Client.new('ACCESS_TOKEN', 'CLIENT_ID', 'CLIENT_SECRET')
84
89
  t.get_member # => Hash of properties for user's Traxo account
data/lib/traxo/client.rb CHANGED
@@ -2,7 +2,7 @@ module Traxo
2
2
 
3
3
  class Client
4
4
  attr_accessor :access_token
5
- attr_reader :response_format, :raise_http_errors
5
+ attr_reader :response_format, :error_handling
6
6
 
7
7
  API_URL = "https://api.traxo.com/v2/"
8
8
 
@@ -44,12 +44,17 @@ module Traxo
44
44
  end
45
45
 
46
46
  def ignore_http_errors!
47
- @raise_http_errors = false
47
+ @error_handling = :ignore
48
48
  self
49
49
  end
50
50
 
51
51
  def raise_http_errors!
52
- @raise_http_errors = true
52
+ @error_handling = :raise
53
+ self
54
+ end
55
+
56
+ def return_false_if_http_errors!
57
+ @error_handling = :bool
53
58
  self
54
59
  end
55
60
 
@@ -76,14 +81,15 @@ module Traxo
76
81
 
77
82
  def assign_error_handling(action)
78
83
  if action
79
- if [:raise, :ignore].include? action
84
+ if [:raise, :ignore, :bool].include? action
80
85
  raise_http_errors! if action == :raise
81
86
  ignore_http_errors! if action == :ignore
87
+ return_false_if_http_errors! if action == :bool
82
88
  else
83
89
  raise ArgumentError.new(':errors parameter must be either :raise or :ignore')
84
90
  end
85
91
  else
86
- raise_http_errors!
92
+ ignore_http_errors!
87
93
  end
88
94
  end
89
95
 
@@ -129,7 +135,7 @@ module Traxo
129
135
  request = Net::HTTP::Delete.new(uri)
130
136
  attach_token(request)
131
137
  response = make_http_request(uri) { |http| http.request(request) }
132
- check_code(response) ? true : false
138
+ format_response(response)
133
139
  end
134
140
 
135
141
  def query_string(data = {})
@@ -146,7 +152,6 @@ module Traxo
146
152
  end
147
153
 
148
154
  def convert_time(time)
149
- time = time.dup
150
155
  if time.is_a? String
151
156
  begin
152
157
  time = Time.parse(time)
@@ -160,7 +165,7 @@ module Traxo
160
165
  end
161
166
 
162
167
  def format_response(response)
163
- return false unless check_code(response)
168
+ return false unless check_success(response)
164
169
 
165
170
  case @response_format
166
171
  when :http
@@ -175,18 +180,21 @@ module Traxo
175
180
  response.header.each { |key| headers[key.to_sym] = response[key] }
176
181
  headers
177
182
  when :headers_string
178
- response.to_json
183
+ headers = {}
184
+ response.header.each { |key| headers[key.to_sym] = response[key] }
185
+ headers.to_json
179
186
  when :code
180
187
  response.code.to_i
181
188
  end
182
189
  end
183
190
 
184
- def check_code(response)
185
- if @raise_http_errors
186
- response.value || response
187
- else
188
- response.code <= '300'
191
+ def check_success(response)
192
+ if @error_handling == :raise
193
+ return response.value || response
194
+ elsif @error_handling == :ignore
195
+ return response
189
196
  end
197
+ response.code <= '300'
190
198
  end
191
199
  end
192
200
 
@@ -0,0 +1,71 @@
1
+ module Traxo
2
+
3
+ class Client
4
+ def get_air_segments(args = {})
5
+ query = get_air_segments_options(args)
6
+ url = "#{API_URL}segments/air#{ query_string(query) }"
7
+ get_request_with_token(url)
8
+ end
9
+
10
+ def get_air_segment(id)
11
+ url = "#{ API_URL}segments/air/#{id}"
12
+ get_request_with_token(url)
13
+ end
14
+
15
+ def create_air_segment(args)
16
+ url = "#{ API_URL}segments/air"
17
+ data = create_air_segment_options(args)
18
+ post_request_with_token(url, data)
19
+ end
20
+
21
+ def update_air_segment(id, args)
22
+ url = "#{ API_URL}segments/air/#{id}"
23
+ data = create_air_segment_options(args, false)
24
+ data.delete_if { |key, val| !val } # previous datetime filtering is skipped
25
+ raise ArgumentError.new('Must provide options to update segment') if data.empty?
26
+ put_request_with_token(url, data)
27
+ end
28
+
29
+ def delete_air_segment(id)
30
+ url = "#{ API_URL}segments/air/#{id}"
31
+ delete_request_with_token(url)
32
+ end
33
+
34
+ private
35
+
36
+ def get_air_segments_options(args)
37
+ unless args.empty?
38
+ args = args.dup
39
+ options = [:start, :status, :end]
40
+ args.select! { |key| options.include? key }
41
+ end
42
+ args = {start: 'today'}.merge(args)
43
+ args[:start] = convert_time(args[:start]) unless args[:start] == 'today'
44
+ if args[:end] && args[:end] != 'today'
45
+ args[:end] = convert_time(args[:end])
46
+ end
47
+ args
48
+ end
49
+
50
+ def create_air_segment_options(args, enforce_required = true)
51
+ args = args.dup
52
+ args[:departure_datetime] = convert_time args[:departure_datetime]
53
+ args[:arrival_datetime] = convert_time args[:arrival_datetime]
54
+ create_air_segment_required_params(args) if enforce_required
55
+ options = [ :trip_id, :origin, :destination, :departure_datetime,
56
+ :arrival_datetime, :airline, :flight_num, :seat_assignment,
57
+ :confirmation_no, :number_of_pax, :price, :currency, :phone,
58
+ :first_name, :last_name ]
59
+ args.keep_if { |key| options.include? key }
60
+ end
61
+
62
+ def create_air_segment_required_params(args)
63
+ required = [:trip_id, :origin, :destination, :departure_datetime,
64
+ :arrival_datetime, :airline, :flight_num]
65
+ required.each do |arg|
66
+ raise ArgumentError.new("#{arg} is a required argument key") unless args[arg]
67
+ end
68
+ end
69
+ end
70
+
71
+ end
data/lib/traxo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Traxo
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/traxo_api.rb CHANGED
@@ -11,4 +11,5 @@ require 'traxo/client'
11
11
 
12
12
  require 'traxo/endpoints/member'
13
13
  require 'traxo/endpoints/accounts'
14
- require 'traxo/endpoints/trips'
14
+ require 'traxo/endpoints/trips'
15
+ require 'traxo/endpoints/segments/air.rb'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traxo_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wil Chandler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-04 00:00:00.000000000 Z
11
+ date: 2015-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ files:
88
88
  - lib/traxo/client.rb
89
89
  - lib/traxo/endpoints/accounts.rb
90
90
  - lib/traxo/endpoints/member.rb
91
+ - lib/traxo/endpoints/segments/air.rb
91
92
  - lib/traxo/endpoints/trips.rb
92
93
  - lib/traxo/version.rb
93
94
  - lib/traxo_api.rb