sacs_ruby 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.travis.yml +4 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +196 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +7 -0
  12. data/lib/sacs_ruby.rb +56 -0
  13. data/lib/sacs_ruby/api/advanced_calendar_search.rb +13 -0
  14. data/lib/sacs_ruby/api/aircraft_equipment_lookup.rb +14 -0
  15. data/lib/sacs_ruby/api/airline_alliance_lookup.rb +15 -0
  16. data/lib/sacs_ruby/api/airline_lookup.rb +14 -0
  17. data/lib/sacs_ruby/api/airports_at_cities_lookup.rb +19 -0
  18. data/lib/sacs_ruby/api/alternate_airport_shop.rb +15 -0
  19. data/lib/sacs_ruby/api/alternate_date.rb +13 -0
  20. data/lib/sacs_ruby/api/bargain_finder_max.rb +15 -0
  21. data/lib/sacs_ruby/api/car_availability.rb +13 -0
  22. data/lib/sacs_ruby/api/city_pairs_lookup.rb +17 -0
  23. data/lib/sacs_ruby/api/countries_lookup.rb +15 -0
  24. data/lib/sacs_ruby/api/destination_finder.rb +17 -0
  25. data/lib/sacs_ruby/api/fare_range.rb +25 -0
  26. data/lib/sacs_ruby/api/flights_to.rb +17 -0
  27. data/lib/sacs_ruby/api/geo_autocomplete.rb +17 -0
  28. data/lib/sacs_ruby/api/geo_code.rb +12 -0
  29. data/lib/sacs_ruby/api/geo_search.rb +12 -0
  30. data/lib/sacs_ruby/api/insta_flights_search.rb +17 -0
  31. data/lib/sacs_ruby/api/lead_price_calendar.rb +17 -0
  32. data/lib/sacs_ruby/api/low_fare_forecast.rb +19 -0
  33. data/lib/sacs_ruby/api/low_fare_history.rb +17 -0
  34. data/lib/sacs_ruby/api/multi_airport_city_lookup.rb +20 -0
  35. data/lib/sacs_ruby/api/point_of_sale_country_code_lookup.rb +17 -0
  36. data/lib/sacs_ruby/api/seat_map.rb +13 -0
  37. data/lib/sacs_ruby/api/top_destinations.rb +19 -0
  38. data/lib/sacs_ruby/api/travel_seasonality.rb +23 -0
  39. data/lib/sacs_ruby/api/travel_seasonality_airports_lookup.rb +13 -0
  40. data/lib/sacs_ruby/api/travel_theme_lookup.rb +15 -0
  41. data/lib/sacs_ruby/base.rb +47 -0
  42. data/lib/sacs_ruby/base_post.rb +27 -0
  43. data/lib/sacs_ruby/client.rb +124 -0
  44. data/lib/sacs_ruby/configuration.rb +23 -0
  45. data/lib/sacs_ruby/credentials.rb +35 -0
  46. data/lib/sacs_ruby/error.rb +55 -0
  47. data/lib/sacs_ruby/json.rb +16 -0
  48. data/lib/sacs_ruby/version.rb +3 -0
  49. data/sacs_ruby.gemspec +28 -0
  50. metadata +189 -0
@@ -0,0 +1,27 @@
1
+ module SacsRuby
2
+ # Base class for POST API
3
+ module BasePost
4
+ def get
5
+ validate!(opts)
6
+ response = SacsRuby.client.post(request_params)
7
+ @results = JSON.load(response)
8
+ end
9
+
10
+ def url
11
+ fail ConfigurationError if SacsRuby.configuration.nil?
12
+ [SacsRuby.configuration.environment, endpoint_url].join
13
+ end
14
+
15
+ private
16
+
17
+ def required_vars
18
+ %i(payload)
19
+ end
20
+
21
+ def request_params
22
+ params = { url: url, payload: opts[:payload] }
23
+ params.merge!(token: @token) unless @token.nil?
24
+ params
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,124 @@
1
+ module SacsRuby
2
+ # Client class
3
+ class Client
4
+ attr_accessor :access_token, :expires_on
5
+
6
+ def initialize
7
+ @credentials = Credentials.new
8
+ end
9
+
10
+ def get(params)
11
+ token = params.fetch(:token, nil) || access_token
12
+ RestClient::Request.execute(
13
+ method: :get,
14
+ url: params[:url],
15
+ headers: { 'Authorization' => "Bearer #{token}" }
16
+ ) do |response, request, result, &block|
17
+ case_response(response, request, result, 'get', &block)
18
+ end
19
+ end
20
+
21
+ def post(params) # rubocop:disable MethodLength
22
+ token = params.fetch(:token, nil) || access_token
23
+ RestClient::Request.execute(
24
+ method: :post,
25
+ url: params[:url],
26
+ payload: params[:payload],
27
+ headers: {
28
+ 'Authorization' => "Bearer #{token}",
29
+ 'Content-Type' => 'application/json',
30
+ 'Accept-Encoding' => 'gzip'
31
+ }
32
+ ) do |response, request, result, &block|
33
+ case_response(response, request, result, 'post', &block)
34
+ end
35
+ end
36
+
37
+ def request_token(auth_key) # rubocop:disable MethodLength
38
+ RestClient::Request.execute(
39
+ method: :post,
40
+ url: SacsRuby.configuration.auth_base_url,
41
+ payload: 'grant_type=client_credentials',
42
+ headers: {
43
+ 'Authorization' => "Basic #{auth_key}",
44
+ 'Content-Type' => 'application/x-www-form-urlencoded'
45
+ }) do |response, request, result, &block|
46
+ case response.code
47
+ when 200
48
+ response
49
+ when 401
50
+ parsed_response = JSON.load(response)
51
+ fail TokenRequestError, parsed_response
52
+ when 429
53
+ parsed_response = JSON.load(response)
54
+ fail TooManyRequestsError, parsed_response
55
+ else
56
+ response.return!(request, result, &block)
57
+ end
58
+ end
59
+ end
60
+
61
+ def access_token
62
+ refresh_if_needed
63
+ @access_token
64
+ end
65
+
66
+ def expires_on
67
+ @expires_on || nil
68
+ end
69
+
70
+ def fetch_token
71
+ response = request_token(@credentials.encoded)
72
+ parsed_response = JSON.load(response)
73
+
74
+ self.access_token = parsed_response['access_token']
75
+ self.expires_on = Time.now.utc + parsed_response['expires_in'].to_i
76
+ self
77
+ end
78
+
79
+ private
80
+
81
+ def refresh_if_needed
82
+ fetch_token if close_expiration? && !SacsRuby.configuration.shared_token?
83
+ end
84
+
85
+ def close_expiration?
86
+ if @access_token
87
+ (Time.now.utc.to_i + 2 * 60) > @expires_on.to_i
88
+ else
89
+ true
90
+ end
91
+ end
92
+
93
+ def case_response(response, request, result, method, &block)
94
+ case response.code
95
+ when 200
96
+ response
97
+ when 400
98
+ parsed_response = JSON.load(response)
99
+ fail BadRequestError, parsed_response
100
+ when 401
101
+ parsed_response = JSON.load(response)
102
+ fail AuthorizationError, parsed_response
103
+ when 403
104
+ parsed_response = JSON.load(response)
105
+ fail ClientRequestError, parsed_response
106
+ when 404
107
+ parsed_response = JSON.load(response)
108
+ fail NotFoundError, parsed_response
109
+ when 429
110
+ parsed_response = JSON.load(response)
111
+ fail TooManyRequestsError, parsed_response
112
+ when 500
113
+ if method == 'post'
114
+ parsed_response = JSON.load(response)
115
+ fail JsonSchemaError, parsed_response
116
+ else
117
+ response.return!(request, result, &block)
118
+ end
119
+ else
120
+ response.return!(request, result, &block)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,23 @@
1
+ module SacsRuby
2
+ # Class for holding configuration
3
+ class Configuration
4
+ attr_accessor :user_id, :group, :domain, :environment, :client_secret,
5
+ :token_strategy
6
+
7
+ def initialize(attrs = {})
8
+ self.attributes = attrs
9
+ end
10
+
11
+ def attributes=(attrs = {})
12
+ attrs.each { |key, value| instance_variable_set("@#{key}", value) }
13
+ end
14
+
15
+ def auth_base_url
16
+ "#{environment}/v2/auth/token"
17
+ end
18
+
19
+ def shared_token?
20
+ token_strategy == :shared
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,35 @@
1
+ require 'base64'
2
+
3
+ module SacsRuby
4
+ # Class for constructing base64 encoded credentials
5
+ class Credentials
6
+ attr_reader :user_id, :group, :domain, :client_secret, :encoded
7
+
8
+ def initialize
9
+ fail ConfigurationError if SacsRuby.configuration.nil?
10
+ @user_id = SacsRuby.configuration.user_id
11
+ @group = SacsRuby.configuration.group
12
+ @domain = SacsRuby.configuration.domain
13
+ @client_secret = SacsRuby.configuration.client_secret
14
+ @encoded = encode("#{encoded_client_id}:#{encoded_client_secret}")
15
+ end
16
+
17
+ private
18
+
19
+ def encode(str)
20
+ Base64.strict_encode64(str)
21
+ end
22
+
23
+ def encoded_client_id
24
+ encode(build_client_id)
25
+ end
26
+
27
+ def encoded_client_secret
28
+ encode(client_secret)
29
+ end
30
+
31
+ def build_client_id
32
+ "V1:#{user_id}:#{group}:#{domain}"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,55 @@
1
+ module SacsRuby
2
+ # Error implementation
3
+ class Error < StandardError
4
+ def initialize(response)
5
+ @response = response
6
+ end
7
+
8
+ def error
9
+ @error = @response['type']
10
+ end
11
+
12
+ def description
13
+ @description = @response['message']
14
+ end
15
+
16
+ def to_s
17
+ "error='#{error}' description='#{description}'"
18
+ end
19
+ end
20
+
21
+ # 404 HTTP Error
22
+ class NotFoundError < Error
23
+ def initialize(response)
24
+ if response.nil?
25
+ @response = { 'type' => 'Not Found', 'message' => '404 Not Found' }
26
+ else
27
+ super
28
+ end
29
+ end
30
+ end
31
+
32
+ class BadRequestError < Error; end
33
+ class AuthorizationError < Error; end
34
+ class ClientRequestError < Error; end
35
+ class TooManyRequestsError < Error; end
36
+ class JsonSchemaError < Error; end
37
+
38
+ # Token Authentication Error
39
+ class TokenRequestError < Error
40
+ def initialize(response)
41
+ @response = {
42
+ 'type' => response['error'],
43
+ 'message' => response['error_description']
44
+ }
45
+ end
46
+ end
47
+
48
+ class ApiArgumentError < StandardError; end
49
+ class ApiEndpointError < StandardError; end
50
+ class ConfigurationError < StandardError
51
+ def to_s
52
+ 'Missing configuration. Please initialize it before use.'
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,16 @@
1
+ require 'multi_json'
2
+
3
+ module SacsRuby
4
+ module JSON
5
+ extend self
6
+
7
+ def dump(object, adapter_options = {})
8
+ MultiJson.dump(object, adapter_options)
9
+ end
10
+
11
+ def load(string, adapter_options = {})
12
+ return nil if string.empty?
13
+ MultiJson.load(string, adapter_options)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module SacsRuby
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sacs_ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sacs_ruby'
8
+ spec.version = SacsRuby::VERSION
9
+ spec.authors = ['Sabre Inc']
10
+ spec.email = ['support@sabre.com']
11
+
12
+ spec.summary = 'A Ruby wrapper around Sabre Dev API.'
13
+ spec.description = 'A Ruby wrapper around Sabre Dev API.'
14
+ spec.homepage = "https://github.com/SabreDevStudio/SACS-Ruby"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'webmock'
25
+ spec.add_development_dependency 'timecop'
26
+ spec.add_dependency 'rest-client'
27
+ spec.add_dependency 'multi_json', '~> 1.0'
28
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sacs_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sabre Inc
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-23 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: timecop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rest-client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: multi_json
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
111
+ description: A Ruby wrapper around Sabre Dev API.
112
+ email:
113
+ - support@sabre.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".ruby-gemset"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/sacs_ruby.rb
129
+ - lib/sacs_ruby/api/advanced_calendar_search.rb
130
+ - lib/sacs_ruby/api/aircraft_equipment_lookup.rb
131
+ - lib/sacs_ruby/api/airline_alliance_lookup.rb
132
+ - lib/sacs_ruby/api/airline_lookup.rb
133
+ - lib/sacs_ruby/api/airports_at_cities_lookup.rb
134
+ - lib/sacs_ruby/api/alternate_airport_shop.rb
135
+ - lib/sacs_ruby/api/alternate_date.rb
136
+ - lib/sacs_ruby/api/bargain_finder_max.rb
137
+ - lib/sacs_ruby/api/car_availability.rb
138
+ - lib/sacs_ruby/api/city_pairs_lookup.rb
139
+ - lib/sacs_ruby/api/countries_lookup.rb
140
+ - lib/sacs_ruby/api/destination_finder.rb
141
+ - lib/sacs_ruby/api/fare_range.rb
142
+ - lib/sacs_ruby/api/flights_to.rb
143
+ - lib/sacs_ruby/api/geo_autocomplete.rb
144
+ - lib/sacs_ruby/api/geo_code.rb
145
+ - lib/sacs_ruby/api/geo_search.rb
146
+ - lib/sacs_ruby/api/insta_flights_search.rb
147
+ - lib/sacs_ruby/api/lead_price_calendar.rb
148
+ - lib/sacs_ruby/api/low_fare_forecast.rb
149
+ - lib/sacs_ruby/api/low_fare_history.rb
150
+ - lib/sacs_ruby/api/multi_airport_city_lookup.rb
151
+ - lib/sacs_ruby/api/point_of_sale_country_code_lookup.rb
152
+ - lib/sacs_ruby/api/seat_map.rb
153
+ - lib/sacs_ruby/api/top_destinations.rb
154
+ - lib/sacs_ruby/api/travel_seasonality.rb
155
+ - lib/sacs_ruby/api/travel_seasonality_airports_lookup.rb
156
+ - lib/sacs_ruby/api/travel_theme_lookup.rb
157
+ - lib/sacs_ruby/base.rb
158
+ - lib/sacs_ruby/base_post.rb
159
+ - lib/sacs_ruby/client.rb
160
+ - lib/sacs_ruby/configuration.rb
161
+ - lib/sacs_ruby/credentials.rb
162
+ - lib/sacs_ruby/error.rb
163
+ - lib/sacs_ruby/json.rb
164
+ - lib/sacs_ruby/version.rb
165
+ - sacs_ruby.gemspec
166
+ homepage: https://github.com/SabreDevStudio/SACS-Ruby
167
+ licenses: []
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.5.1
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: A Ruby wrapper around Sabre Dev API.
189
+ test_files: []