stuart-client-ruby 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rubocop.yml +274 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.md +68 -0
- data/Rakefile +48 -0
- data/lib/stuart/.DS_Store +0 -0
- data/lib/stuart/client.rb +11 -0
- data/lib/stuart/infrastructure/api_response.rb +16 -0
- data/lib/stuart/infrastructure/authenticator.rb +30 -0
- data/lib/stuart/infrastructure/environment.rb +9 -0
- data/lib/stuart/infrastructure/http_client.rb +38 -0
- data/lib/stuart/version.rb +9 -0
- data/lib/stuart.rb +5 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/stuart/infrastructure/authenticator_spec.rb +52 -0
- data/spec/stuart/infrastructure/http_client_spec.rb +57 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e6fe407f7e85f077cba35da969e8229ea66fe3cf
|
4
|
+
data.tar.gz: 0865cf44453d5fb5702d264fc507207af4521941
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff636f7d5fbb3d80de6dab3eb9fd47df37829e5ddb48bc3ca4b9825f8a92db5742c111f72b1aa6e39b35015cdb17eb4dc8d7d91f4f44b136a5fc950758037d64
|
7
|
+
data.tar.gz: 7f03c699e1bd01593ccb79ea80bd9a9d655c3526615981ada0dfc9c9bf94e6b71552c989bb8162d04a9d8ff178b972b3360637422a1852b71ab24c97aad007b9
|
data/.document
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,274 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.2
|
3
|
+
Exclude:
|
4
|
+
- "vendor/**/*"
|
5
|
+
- "db/schema.rb"
|
6
|
+
UseCache: false
|
7
|
+
Rails:
|
8
|
+
Enabled: true
|
9
|
+
Style/CollectionMethods:
|
10
|
+
Description: Preferred collection methods.
|
11
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
|
12
|
+
Enabled: true
|
13
|
+
PreferredMethods:
|
14
|
+
collect: map
|
15
|
+
collect!: map!
|
16
|
+
find: detect
|
17
|
+
find_all: select
|
18
|
+
reduce: inject
|
19
|
+
Style/DotPosition:
|
20
|
+
Description: Checks the position of the dot in multi-line method calls.
|
21
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
|
22
|
+
Enabled: true
|
23
|
+
EnforcedStyle: trailing
|
24
|
+
SupportedStyles:
|
25
|
+
- leading
|
26
|
+
- trailing
|
27
|
+
Style/FileName:
|
28
|
+
Description: Use snake_case for source file names.
|
29
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
|
30
|
+
Enabled: false
|
31
|
+
Exclude: []
|
32
|
+
Style/GuardClause:
|
33
|
+
Description: Check for conditionals that can be replaced with guard clauses
|
34
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
|
35
|
+
Enabled: false
|
36
|
+
MinBodyLength: 1
|
37
|
+
Style/IfUnlessModifier:
|
38
|
+
Description: Favor modifier if/unless usage when you have a single-line body.
|
39
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
|
40
|
+
Enabled: false
|
41
|
+
MaxLineLength: 99
|
42
|
+
Style/OptionHash:
|
43
|
+
Description: Don't use option hashes when you can use keyword arguments.
|
44
|
+
Enabled: false
|
45
|
+
Style/PercentLiteralDelimiters:
|
46
|
+
Description: Use `%`-literal delimiters consistently
|
47
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
|
48
|
+
Enabled: false
|
49
|
+
PreferredDelimiters:
|
50
|
+
"%": "()"
|
51
|
+
"%i": "()"
|
52
|
+
"%q": "()"
|
53
|
+
"%Q": "()"
|
54
|
+
"%r": "{}"
|
55
|
+
"%s": "()"
|
56
|
+
"%w": "()"
|
57
|
+
"%W": "()"
|
58
|
+
"%x": "()"
|
59
|
+
Style/PredicateName:
|
60
|
+
Description: Check the names of predicate methods.
|
61
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark
|
62
|
+
Enabled: true
|
63
|
+
NamePrefix:
|
64
|
+
- is_
|
65
|
+
- has_
|
66
|
+
- have_
|
67
|
+
NamePrefixBlacklist:
|
68
|
+
- is_
|
69
|
+
Exclude:
|
70
|
+
- spec/**/*
|
71
|
+
Style/RaiseArgs:
|
72
|
+
Description: Checks the arguments passed to raise/fail.
|
73
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#exception-class-messages
|
74
|
+
Enabled: false
|
75
|
+
EnforcedStyle: exploded
|
76
|
+
SupportedStyles:
|
77
|
+
- compact
|
78
|
+
- exploded
|
79
|
+
Style/SignalException:
|
80
|
+
Description: Checks for proper usage of fail and raise.
|
81
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#fail-method
|
82
|
+
Enabled: false
|
83
|
+
EnforcedStyle: semantic
|
84
|
+
SupportedStyles:
|
85
|
+
- only_raise
|
86
|
+
- only_fail
|
87
|
+
- semantic
|
88
|
+
Style/SingleLineBlockParams:
|
89
|
+
Description: Enforces the names of some block params.
|
90
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#reduce-blocks
|
91
|
+
Enabled: false
|
92
|
+
Methods:
|
93
|
+
- reduce:
|
94
|
+
- a
|
95
|
+
- e
|
96
|
+
- inject:
|
97
|
+
- a
|
98
|
+
- e
|
99
|
+
Style/SingleLineMethods:
|
100
|
+
Description: Avoid single-line methods.
|
101
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-single-line-methods
|
102
|
+
Enabled: false
|
103
|
+
AllowIfMethodIsEmpty: true
|
104
|
+
Style/StringLiterals:
|
105
|
+
Description: Checks if uses of quotes match the configured preference.
|
106
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
|
107
|
+
Enabled: true
|
108
|
+
EnforcedStyle: double_quotes
|
109
|
+
SupportedStyles:
|
110
|
+
- single_quotes
|
111
|
+
- double_quotes
|
112
|
+
Style/StringLiteralsInInterpolation:
|
113
|
+
Description: Checks if uses of quotes inside expressions in interpolated strings
|
114
|
+
match the configured preference.
|
115
|
+
Enabled: true
|
116
|
+
EnforcedStyle: single_quotes
|
117
|
+
SupportedStyles:
|
118
|
+
- single_quotes
|
119
|
+
- double_quotes
|
120
|
+
Style/TrailingCommaInArguments:
|
121
|
+
Description: 'Checks for trailing comma in argument lists.'
|
122
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
123
|
+
Enabled: false
|
124
|
+
EnforcedStyleForMultiline: no_comma
|
125
|
+
SupportedStyles:
|
126
|
+
- comma
|
127
|
+
- consistent_comma
|
128
|
+
- no_comma
|
129
|
+
Style/TrailingCommaInLiteral:
|
130
|
+
Description: 'Checks for trailing comma in array and hash literals.'
|
131
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
132
|
+
Enabled: false
|
133
|
+
EnforcedStyleForMultiline: no_comma
|
134
|
+
SupportedStyles:
|
135
|
+
- comma
|
136
|
+
- consistent_comma
|
137
|
+
- no_comma
|
138
|
+
Metrics/BlockLength:
|
139
|
+
Enabled: false
|
140
|
+
Metrics/AbcSize:
|
141
|
+
Description: A calculated magnitude based on number of assignments, branches, and
|
142
|
+
conditions.
|
143
|
+
Enabled: false
|
144
|
+
Max: 15
|
145
|
+
Metrics/ClassLength:
|
146
|
+
Description: Avoid classes longer than 100 lines of code.
|
147
|
+
Enabled: false
|
148
|
+
CountComments: false
|
149
|
+
Max: 100
|
150
|
+
Metrics/LineLength:
|
151
|
+
Max: 100
|
152
|
+
# To make it possible to copy or click on URIs in the code, we allow lines
|
153
|
+
# containing a URI to be longer than Max.
|
154
|
+
AllowHeredoc: true
|
155
|
+
AllowURI: true
|
156
|
+
URISchemes:
|
157
|
+
- http
|
158
|
+
- https
|
159
|
+
Metrics/ModuleLength:
|
160
|
+
CountComments: false
|
161
|
+
Max: 100
|
162
|
+
Description: Avoid modules longer than 100 lines of code.
|
163
|
+
Enabled: false
|
164
|
+
Metrics/CyclomaticComplexity:
|
165
|
+
Description: A complexity metric that is strongly correlated to the number of test
|
166
|
+
cases needed to validate a method.
|
167
|
+
Enabled: false
|
168
|
+
Max: 6
|
169
|
+
Metrics/MethodLength:
|
170
|
+
Description: Avoid methods longer than 10 lines of code.
|
171
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
|
172
|
+
Enabled: false
|
173
|
+
CountComments: false
|
174
|
+
Max: 10
|
175
|
+
Metrics/ParameterLists:
|
176
|
+
Description: Avoid parameter lists longer than three or four parameters.
|
177
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#too-many-params
|
178
|
+
Enabled: false
|
179
|
+
Max: 5
|
180
|
+
CountKeywordArgs: true
|
181
|
+
Metrics/PerceivedComplexity:
|
182
|
+
Description: A complexity metric geared towards measuring complexity for a human
|
183
|
+
reader.
|
184
|
+
Enabled: false
|
185
|
+
Max: 7
|
186
|
+
Lint/AssignmentInCondition:
|
187
|
+
Description: Don't use assignment in conditions.
|
188
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition
|
189
|
+
Enabled: false
|
190
|
+
AllowSafeAssignment: true
|
191
|
+
Style/InlineComment:
|
192
|
+
Description: Avoid inline comments.
|
193
|
+
Enabled: false
|
194
|
+
Style/AccessorMethodName:
|
195
|
+
Description: Check the naming of accessor methods for get_/set_.
|
196
|
+
Enabled: false
|
197
|
+
Style/Alias:
|
198
|
+
Description: Use alias_method instead of alias.
|
199
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
|
200
|
+
Enabled: false
|
201
|
+
Style/Documentation:
|
202
|
+
Description: Document classes and non-namespace modules.
|
203
|
+
Enabled: false
|
204
|
+
Style/DoubleNegation:
|
205
|
+
Description: Checks for uses of double negation (!!).
|
206
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-bang-bang
|
207
|
+
Enabled: false
|
208
|
+
Style/EachWithObject:
|
209
|
+
Description: Prefer `each_with_object` over `inject` or `reduce`.
|
210
|
+
Enabled: false
|
211
|
+
Style/EmptyLiteral:
|
212
|
+
Description: Prefer literals to Array.new/Hash.new/String.new.
|
213
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#literal-array-hash
|
214
|
+
Enabled: false
|
215
|
+
Style/ModuleFunction:
|
216
|
+
Description: Checks for usage of `extend self` in modules.
|
217
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#module-function
|
218
|
+
Enabled: false
|
219
|
+
Style/OneLineConditional:
|
220
|
+
Description: Favor the ternary operator(?:) over if/then/else/end constructs.
|
221
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#ternary-operator
|
222
|
+
Enabled: false
|
223
|
+
Style/PerlBackrefs:
|
224
|
+
Description: Avoid Perl-style regex back references.
|
225
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers
|
226
|
+
Enabled: false
|
227
|
+
Style/SymbolArray:
|
228
|
+
SupportedStyles:
|
229
|
+
- percent
|
230
|
+
- brackets
|
231
|
+
EnforcedStyle: percent
|
232
|
+
Style/Send:
|
233
|
+
Description: Prefer `Object#__send__` or `Object#public_send` to `send`, as `send`
|
234
|
+
may overlap with existing methods.
|
235
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#prefer-public-send
|
236
|
+
Enabled: false
|
237
|
+
Style/SpecialGlobalVars:
|
238
|
+
Description: Avoid Perl-style global variables.
|
239
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms
|
240
|
+
Enabled: false
|
241
|
+
Style/VariableInterpolation:
|
242
|
+
Description: Don't interpolate global, instance and class variables directly in
|
243
|
+
strings.
|
244
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#curlies-interpolate
|
245
|
+
Enabled: false
|
246
|
+
Style/WhenThen:
|
247
|
+
Description: Use when x then ... for one-line cases.
|
248
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#one-line-cases
|
249
|
+
Enabled: false
|
250
|
+
Lint/EachWithObjectArgument:
|
251
|
+
Description: Check for immutable argument given to each_with_object.
|
252
|
+
Enabled: true
|
253
|
+
Lint/HandleExceptions:
|
254
|
+
Description: Don't suppress exception.
|
255
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions
|
256
|
+
Enabled: false
|
257
|
+
Lint/LiteralInCondition:
|
258
|
+
Description: Checks of literals used in conditions.
|
259
|
+
Enabled: false
|
260
|
+
Lint/LiteralInInterpolation:
|
261
|
+
Description: Checks for literals used in interpolation.
|
262
|
+
Enabled: false
|
263
|
+
Style/Lambda:
|
264
|
+
Description: Use the new lambda literal syntax for single-line blocks.
|
265
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#lambda-multi-line
|
266
|
+
Enabled: false
|
267
|
+
Rails/HttpPositionalArguments:
|
268
|
+
Enabled: false
|
269
|
+
Rails/SkipsModelValidations:
|
270
|
+
Enabled: false
|
271
|
+
Lint/AmbiguousBlockAssociation:
|
272
|
+
Enabled: false
|
273
|
+
Style/ClassAndModuleChildren:
|
274
|
+
Enabled: false
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2018 Maximilien Tyc
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Stuart Ruby Client
|
2
|
+
For a complete documentation of all endpoints offered by the Stuart API, you can visit [Stuart API documentation](https://stuart.api-docs.io).
|
3
|
+
|
4
|
+
## Install
|
5
|
+
Via Composer:
|
6
|
+
|
7
|
+
``` bash
|
8
|
+
$ gem install stuart-client-ruby
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
### Initialize HTTP client
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
environment = Stuart::Infrastructure::Environment::SANDBOX
|
17
|
+
api_client_id = 'c6058849d0a056fc743203acb8e6a850dad103485c3edc51b16a9260cc7a7689' # can be found here: https://admin-sandbox.stuart.com/client/api
|
18
|
+
api_client_secret = 'aa6a415fce31967501662c1960fcbfbf4745acff99acb19dbc1aae6f76c9c618' # can be found here: https://admin-sandbox.stuart.com/client/api
|
19
|
+
auth = Stuart::Infrastructure::Authenticator.new(environment, api_client_id, api_client_secret)
|
20
|
+
|
21
|
+
http_client = Stuart::Infrastructure::HttpClient.new(auth)
|
22
|
+
```
|
23
|
+
|
24
|
+
### Custom requests
|
25
|
+
|
26
|
+
#### Example: create a job
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
job = {
|
30
|
+
job: {
|
31
|
+
transport_type: "bike",
|
32
|
+
pickups: [
|
33
|
+
{
|
34
|
+
address: "46 Boulevard Barbès, 75018 Paris",
|
35
|
+
comment: "Wait outside for an employee to come.",
|
36
|
+
contact: {
|
37
|
+
firstname: "Martin",
|
38
|
+
lastname: "Pont",
|
39
|
+
phone: "+33698348756'",
|
40
|
+
company: "KFC Paris Barbès"
|
41
|
+
}
|
42
|
+
}
|
43
|
+
],
|
44
|
+
dropoffs: [
|
45
|
+
{
|
46
|
+
address: "156 rue de Charonne, 75011 Paris",
|
47
|
+
package_description: "Red packet.",
|
48
|
+
client_reference: "12345678ABCDE", # must be unique
|
49
|
+
comment: "code: 3492B. 3e étage droite. Sonner à Durand.",
|
50
|
+
contact: {
|
51
|
+
firstname: "Alex",
|
52
|
+
lastname: "Durand",
|
53
|
+
phone: "+33634981209",
|
54
|
+
company: "Durand associates."
|
55
|
+
}
|
56
|
+
}
|
57
|
+
]
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
http_client.perform_post '/v2/jobs', JSON.generate(job)
|
62
|
+
```
|
63
|
+
|
64
|
+
#### Example: get a list of jobs
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
http_client.perform_get('/v2/jobs')
|
68
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "bundler"
|
5
|
+
require "./lib/stuart/version"
|
6
|
+
begin
|
7
|
+
Bundler.setup(:default, :development)
|
8
|
+
rescue Bundler::BundlerError => e
|
9
|
+
$stderr.puts e.message
|
10
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
11
|
+
exit e.status_code
|
12
|
+
end
|
13
|
+
require "rake"
|
14
|
+
require "juwelier"
|
15
|
+
Juwelier::Tasks.new do |gem|
|
16
|
+
gem.name = "stuart-client-ruby"
|
17
|
+
gem.homepage = "http://github.com/stuartapp/stuart-client-ruby"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = "Stuart API Ruby client"
|
20
|
+
gem.description = "Communicate with the Stuart API"
|
21
|
+
gem.email = "engineering@stuart.com"
|
22
|
+
gem.authors = ["Paul Caillau", "Maximilien Tyc"]
|
23
|
+
gem.version = Stuart::Version::STRING
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Juwelier::RubygemsDotOrgTasks.new
|
27
|
+
require "rake/testtask"
|
28
|
+
Rake::TestTask.new(:test) do |test|
|
29
|
+
test.libs << "lib" << "test"
|
30
|
+
test.pattern = "test/**/test_*.rb"
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "Code coverage detail"
|
35
|
+
task :simplecov do
|
36
|
+
ENV["COVERAGE"] = "true"
|
37
|
+
Rake::Task["test"].execute
|
38
|
+
end
|
39
|
+
|
40
|
+
task default: :test
|
41
|
+
|
42
|
+
require "rdoc/task"
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
rdoc.rdoc_dir = "rdoc"
|
45
|
+
rdoc.title = "stuart-client-ruby #{Stuart::Version::STRING}"
|
46
|
+
rdoc.rdoc_files.include("README*")
|
47
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
48
|
+
end
|
Binary file
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Stuart
|
2
|
+
module Infrastructure
|
3
|
+
class ApiResponse
|
4
|
+
attr_reader :status_code, :body
|
5
|
+
|
6
|
+
def initialize(status_code, body)
|
7
|
+
@status_code = status_code
|
8
|
+
@body = body
|
9
|
+
end
|
10
|
+
|
11
|
+
def success?
|
12
|
+
@status_code == (200 || 201)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "oauth2"
|
2
|
+
|
3
|
+
module Stuart
|
4
|
+
module Infrastructure
|
5
|
+
class Authenticator
|
6
|
+
attr_reader :environment
|
7
|
+
|
8
|
+
def initialize(environment, api_client_id, api_client_secret)
|
9
|
+
@environment = environment
|
10
|
+
@oauth_client = OAuth2::Client.new(api_client_id,
|
11
|
+
api_client_secret,
|
12
|
+
site: environment[:base_url])
|
13
|
+
end
|
14
|
+
|
15
|
+
def access_token
|
16
|
+
has_valid_token? ? @access_token.token : new_access_token.token
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def new_access_token
|
22
|
+
@access_token = @oauth_client.client_credentials.get_token
|
23
|
+
end
|
24
|
+
|
25
|
+
def has_valid_token?
|
26
|
+
!@access_token.nil? && !@access_token.expired?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "typhoeus"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module Stuart
|
5
|
+
module Infrastructure
|
6
|
+
class HttpClient
|
7
|
+
def initialize(authenticator)
|
8
|
+
@authenticator = authenticator
|
9
|
+
end
|
10
|
+
|
11
|
+
def perform_get(resource)
|
12
|
+
to_api_response Typhoeus.get(url(resource), headers: default_header)
|
13
|
+
end
|
14
|
+
|
15
|
+
def perform_post(resource, body)
|
16
|
+
to_api_response Typhoeus.post(url(resource), headers: default_header, body: body)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def url(resource)
|
22
|
+
"#{@authenticator.environment[:base_url]}#{resource}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def default_header
|
26
|
+
{ "Authorization" => "Bearer #{@authenticator.access_token}",
|
27
|
+
"User-Agent" => "stuart-client-ruby/#{Stuart::Version::STRING}",
|
28
|
+
"Content-Type" => "application/json" }
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_api_response(response)
|
32
|
+
status_code = response.response_code
|
33
|
+
body = JSON.parse(response.response_body) if response.response_body
|
34
|
+
ApiResponse.new(status_code, body)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/stuart.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "securerandom"
|
3
|
+
|
4
|
+
describe Stuart::Infrastructure::Authenticator do
|
5
|
+
let(:environment) { Stuart::Infrastructure::Environment::SANDBOX }
|
6
|
+
let(:authenticator) do
|
7
|
+
Stuart::Infrastructure::Authenticator.new(environment, "some-id",
|
8
|
+
"some-secret")
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:client_credentials_double) { instance_double("OAuth2::Strategy::ClientCredentials") }
|
12
|
+
|
13
|
+
before do
|
14
|
+
allow(OAuth2::Strategy::ClientCredentials).to receive(:new).
|
15
|
+
and_return(client_credentials_double)
|
16
|
+
allow(client_credentials_double).to receive(:get_token).
|
17
|
+
and_return(OAuth2::AccessToken.new(nil, "new-access-token"))
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#access_token" do
|
21
|
+
subject(:access_token) { authenticator.access_token }
|
22
|
+
|
23
|
+
context "when no access token exists" do
|
24
|
+
it "returns a new access token" do
|
25
|
+
expect(access_token).to eq("new-access-token")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when a valid access token already exists" do
|
30
|
+
let(:oauth_access_token) { OAuth2::AccessToken.new(nil, "existing-access-token") }
|
31
|
+
|
32
|
+
before do
|
33
|
+
authenticator.instance_variable_set(:@access_token, oauth_access_token)
|
34
|
+
allow(authenticator).to receive(:has_valid_token?).and_return(true)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns the current valid access token" do
|
38
|
+
expect(access_token).to eq("existing-access-token")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when a non valid access token already exists" do
|
43
|
+
before do
|
44
|
+
allow(authenticator).to receive(:has_valid_token?).and_return(false)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns a new access token" do
|
48
|
+
expect(access_token).to eq("new-access-token")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Stuart::Infrastructure::HttpClient do
|
4
|
+
let(:environment) { Stuart::Infrastructure::Environment::SANDBOX }
|
5
|
+
let(:authenticator) do
|
6
|
+
Stuart::Infrastructure::Authenticator.new(environment, "some-id",
|
7
|
+
"some-secret")
|
8
|
+
end
|
9
|
+
let(:http_client) { Stuart::Infrastructure::HttpClient.new(authenticator) }
|
10
|
+
|
11
|
+
let(:access_token) { "some-access-token" }
|
12
|
+
let(:ruby_gem_version) { Stuart::Version::STRING }
|
13
|
+
let(:resource) { "/some-endpoint" }
|
14
|
+
|
15
|
+
let(:expected_headers) do
|
16
|
+
{
|
17
|
+
"Authorization" => "Bearer #{access_token}",
|
18
|
+
"User-Agent" => "stuart-client-ruby/#{ruby_gem_version}",
|
19
|
+
"Content-Type" => "application/json"
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
before do
|
24
|
+
allow(authenticator).to receive(:access_token).and_return(access_token)
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#perform_get" do
|
28
|
+
before do
|
29
|
+
allow(Typhoeus).to receive(:get).and_return(Typhoeus::Response.new)
|
30
|
+
end
|
31
|
+
|
32
|
+
subject(:get) { http_client.perform_get(resource) }
|
33
|
+
|
34
|
+
it "calls Typhoeus with correct parameters" do
|
35
|
+
expect(Typhoeus).to receive(:get).with("#{environment[:base_url]}#{resource}",
|
36
|
+
headers: expected_headers)
|
37
|
+
get
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#perform_post" do
|
42
|
+
before do
|
43
|
+
allow(Typhoeus).to receive(:post).and_return(Typhoeus::Response.new)
|
44
|
+
end
|
45
|
+
|
46
|
+
let(:body) { "some-body" }
|
47
|
+
|
48
|
+
subject(:post) { http_client.perform_post(resource, body) }
|
49
|
+
|
50
|
+
it "calls Typhoeus with correct parameters" do
|
51
|
+
expect(Typhoeus).to receive(:post).with("#{environment[:base_url]}#{resource}",
|
52
|
+
headers: expected_headers,
|
53
|
+
body: body)
|
54
|
+
post
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stuart-client-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Caillau
|
8
|
+
- Maximilien Tyc
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-02-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: oauth2
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: typhoeus
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bundler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: juwelier
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rdoc
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rspec
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: shoulda
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: simplecov
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: Communicate with the Stuart API
|
127
|
+
email: engineering@stuart.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files:
|
131
|
+
- LICENSE.txt
|
132
|
+
- README.md
|
133
|
+
files:
|
134
|
+
- ".document"
|
135
|
+
- ".rubocop.yml"
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- lib/stuart.rb
|
141
|
+
- lib/stuart/.DS_Store
|
142
|
+
- lib/stuart/client.rb
|
143
|
+
- lib/stuart/infrastructure/api_response.rb
|
144
|
+
- lib/stuart/infrastructure/authenticator.rb
|
145
|
+
- lib/stuart/infrastructure/environment.rb
|
146
|
+
- lib/stuart/infrastructure/http_client.rb
|
147
|
+
- lib/stuart/version.rb
|
148
|
+
- spec/spec_helper.rb
|
149
|
+
- spec/stuart/infrastructure/authenticator_spec.rb
|
150
|
+
- spec/stuart/infrastructure/http_client_spec.rb
|
151
|
+
homepage: http://github.com/stuartapp/stuart-client-ruby
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
metadata: {}
|
155
|
+
post_install_message:
|
156
|
+
rdoc_options: []
|
157
|
+
require_paths:
|
158
|
+
- lib
|
159
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
requirements: []
|
170
|
+
rubyforge_project:
|
171
|
+
rubygems_version: 2.4.5.1
|
172
|
+
signing_key:
|
173
|
+
specification_version: 4
|
174
|
+
summary: Stuart API Ruby client
|
175
|
+
test_files: []
|