endo 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +28 -0
- data/.rubocop_todo.yml +270 -0
- data/README.md +15 -14
- data/endo/sample.rb +9 -9
- data/lib/endo/cli.rb +4 -3
- data/lib/endo/core.rb +55 -65
- data/lib/endo/error/http_error.rb +1 -3
- data/lib/endo/error/validation_error.rb +1 -2
- data/lib/endo/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac22efa794a76374a866f7c8f19ee7b7a261efda
|
4
|
+
data.tar.gz: 25f3b904a025040605012eb92327951ea8b87310
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 727908b09dfa7314bd86b96ed4a6afda60dede68beb0de41f0bd9bb715850a9979f74a72835211dcf537c0705271f861459f8370c6e83a645d40ce361ad32603
|
7
|
+
data.tar.gz: 5f501ccbfc7ec6f91a07d480cef5f120dfde0bc9b47c0d33325af782b213a6f6254473295c3f1d7ebb6512c7a953d6378ceab0e6ad7f24e3b95dabde1b126a6c
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
Style/Documentation:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Style/AsciiComments:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Metrics/LineLength:
|
8
|
+
Max: 160
|
9
|
+
|
10
|
+
Style/IfUnlessModifier:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Metrics/MethodLength:
|
14
|
+
Max: 20
|
15
|
+
Exclude:
|
16
|
+
- "db/migrate/*.rb"
|
17
|
+
|
18
|
+
Metrics/AbcSize:
|
19
|
+
Max: 30
|
20
|
+
|
21
|
+
Metrics/CyclomaticComplexity:
|
22
|
+
Max: 10
|
23
|
+
|
24
|
+
Metrics/PerceivedComplexity:
|
25
|
+
Max: 8
|
26
|
+
|
27
|
+
AllCops:
|
28
|
+
TargetRubyVersion: 2.2
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,270 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2016-05-02 14:46:30 +0900 using RuboCop version 0.39.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
Lint/AmbiguousOperator:
|
11
|
+
Exclude:
|
12
|
+
- 'lib/endo/core.rb'
|
13
|
+
|
14
|
+
# Offense count: 5
|
15
|
+
# Cop supports --auto-correct.
|
16
|
+
# Configuration parameters: AlignWith, SupportedStyles, AutoCorrect.
|
17
|
+
# SupportedStyles: keyword, variable, start_of_line
|
18
|
+
Lint/EndAlignment:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
# Offense count: 1
|
22
|
+
Lint/UselessAssignment:
|
23
|
+
Exclude:
|
24
|
+
- 'lib/endo/core.rb'
|
25
|
+
|
26
|
+
# Offense count: 3
|
27
|
+
Metrics/AbcSize:
|
28
|
+
Max: 40
|
29
|
+
|
30
|
+
# Offense count: 1
|
31
|
+
# Configuration parameters: CountComments.
|
32
|
+
Metrics/ClassLength:
|
33
|
+
Max: 149
|
34
|
+
|
35
|
+
# Offense count: 2
|
36
|
+
Metrics/CyclomaticComplexity:
|
37
|
+
Max: 8
|
38
|
+
|
39
|
+
# Offense count: 3
|
40
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
|
41
|
+
# URISchemes: http, https
|
42
|
+
Metrics/LineLength:
|
43
|
+
Max: 127
|
44
|
+
|
45
|
+
# Offense count: 4
|
46
|
+
# Configuration parameters: CountComments.
|
47
|
+
Metrics/MethodLength:
|
48
|
+
Max: 25
|
49
|
+
|
50
|
+
# Offense count: 1
|
51
|
+
Metrics/PerceivedComplexity:
|
52
|
+
Max: 8
|
53
|
+
|
54
|
+
# Offense count: 3
|
55
|
+
# Cop supports --auto-correct.
|
56
|
+
Performance/RedundantBlockCall:
|
57
|
+
Exclude:
|
58
|
+
- 'lib/endo/core.rb'
|
59
|
+
|
60
|
+
# Offense count: 1
|
61
|
+
Style/AsciiComments:
|
62
|
+
Exclude:
|
63
|
+
- 'lib/endo/core.rb'
|
64
|
+
|
65
|
+
# Offense count: 7
|
66
|
+
# Cop supports --auto-correct.
|
67
|
+
# Configuration parameters: SupportedStyles, IndentOneStep, IndentationWidth.
|
68
|
+
# SupportedStyles: case, end
|
69
|
+
Style/CaseIndentation:
|
70
|
+
IndentWhenRelativeTo: end
|
71
|
+
|
72
|
+
# Offense count: 2
|
73
|
+
# Cop supports --auto-correct.
|
74
|
+
Style/DeprecatedHashMethods:
|
75
|
+
Exclude:
|
76
|
+
- 'lib/endo/core.rb'
|
77
|
+
|
78
|
+
# Offense count: 10
|
79
|
+
Style/Documentation:
|
80
|
+
Exclude:
|
81
|
+
- 'spec/**/*'
|
82
|
+
- 'test/**/*'
|
83
|
+
- 'lib/endo.rb'
|
84
|
+
- 'lib/endo/cli.rb'
|
85
|
+
- 'lib/endo/core.rb'
|
86
|
+
- 'lib/endo/error/http_error.rb'
|
87
|
+
- 'lib/endo/error/validation_error.rb'
|
88
|
+
- 'lib/endo/expectation_target.rb'
|
89
|
+
- 'lib/endo/matchers.rb'
|
90
|
+
- 'lib/endo/matchers/base_matcher.rb'
|
91
|
+
- 'lib/endo/matchers/eq.rb'
|
92
|
+
- 'lib/endo/matchers/include.rb'
|
93
|
+
|
94
|
+
# Offense count: 5
|
95
|
+
# Cop supports --auto-correct.
|
96
|
+
Style/ElseAlignment:
|
97
|
+
Exclude:
|
98
|
+
- 'lib/endo/core.rb'
|
99
|
+
|
100
|
+
# Offense count: 3
|
101
|
+
# Cop supports --auto-correct.
|
102
|
+
Style/EmptyLines:
|
103
|
+
Exclude:
|
104
|
+
- 'lib/endo/expectation_target.rb'
|
105
|
+
- 'lib/endo/matchers.rb'
|
106
|
+
|
107
|
+
# Offense count: 4
|
108
|
+
# Cop supports --auto-correct.
|
109
|
+
Style/EmptyLinesAroundAccessModifier:
|
110
|
+
Exclude:
|
111
|
+
- 'lib/endo/cli.rb'
|
112
|
+
- 'lib/endo/core.rb'
|
113
|
+
- 'lib/endo/matchers/eq.rb'
|
114
|
+
- 'lib/endo/matchers/include.rb'
|
115
|
+
|
116
|
+
# Offense count: 4
|
117
|
+
# Cop supports --auto-correct.
|
118
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
119
|
+
# SupportedStyles: empty_lines, no_empty_lines
|
120
|
+
Style/EmptyLinesAroundClassBody:
|
121
|
+
Exclude:
|
122
|
+
- 'lib/endo/core.rb'
|
123
|
+
- 'lib/endo/error/http_error.rb'
|
124
|
+
- 'lib/endo/error/validation_error.rb'
|
125
|
+
- 'lib/endo/expectation_target.rb'
|
126
|
+
|
127
|
+
# Offense count: 2
|
128
|
+
# Cop supports --auto-correct.
|
129
|
+
Style/EmptyLinesAroundMethodBody:
|
130
|
+
Exclude:
|
131
|
+
- 'lib/endo/core.rb'
|
132
|
+
|
133
|
+
# Offense count: 1
|
134
|
+
# Configuration parameters: MinBodyLength.
|
135
|
+
Style/GuardClause:
|
136
|
+
Exclude:
|
137
|
+
- 'lib/endo/matchers/include.rb'
|
138
|
+
|
139
|
+
# Offense count: 2
|
140
|
+
# Cop supports --auto-correct.
|
141
|
+
# Configuration parameters: MaxLineLength.
|
142
|
+
Style/IfUnlessModifier:
|
143
|
+
Exclude:
|
144
|
+
- 'lib/endo/core.rb'
|
145
|
+
|
146
|
+
# Offense count: 2
|
147
|
+
# Cop supports --auto-correct.
|
148
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
149
|
+
# SupportedStyles: normal, rails
|
150
|
+
Style/IndentationConsistency:
|
151
|
+
Exclude:
|
152
|
+
- 'lib/endo/matchers/eq.rb'
|
153
|
+
- 'lib/endo/matchers/include.rb'
|
154
|
+
|
155
|
+
# Offense count: 3
|
156
|
+
# Cop supports --auto-correct.
|
157
|
+
# Configuration parameters: Width.
|
158
|
+
Style/IndentationWidth:
|
159
|
+
Exclude:
|
160
|
+
- 'lib/endo/core.rb'
|
161
|
+
|
162
|
+
# Offense count: 3
|
163
|
+
# Cop supports --auto-correct.
|
164
|
+
Style/LeadingCommentSpace:
|
165
|
+
Exclude:
|
166
|
+
- 'lib/endo/core.rb'
|
167
|
+
|
168
|
+
# Offense count: 1
|
169
|
+
# Cop supports --auto-correct.
|
170
|
+
Style/MutableConstant:
|
171
|
+
Exclude:
|
172
|
+
- 'lib/endo/version.rb'
|
173
|
+
|
174
|
+
# Offense count: 2
|
175
|
+
# Cop supports --auto-correct.
|
176
|
+
# Configuration parameters: PreferredDelimiters.
|
177
|
+
Style/PercentLiteralDelimiters:
|
178
|
+
Exclude:
|
179
|
+
- 'endo.gemspec'
|
180
|
+
|
181
|
+
# Offense count: 5
|
182
|
+
# Configuration parameters: SupportedStyles.
|
183
|
+
# SupportedStyles: compact, exploded
|
184
|
+
Style/RaiseArgs:
|
185
|
+
EnforcedStyle: compact
|
186
|
+
|
187
|
+
# Offense count: 1
|
188
|
+
# Cop supports --auto-correct.
|
189
|
+
Style/RedundantParentheses:
|
190
|
+
Exclude:
|
191
|
+
- 'lib/endo/core.rb'
|
192
|
+
|
193
|
+
# Offense count: 2
|
194
|
+
# Cop supports --auto-correct.
|
195
|
+
# Configuration parameters: AllowMultipleReturnValues.
|
196
|
+
Style/RedundantReturn:
|
197
|
+
Exclude:
|
198
|
+
- 'lib/endo/core.rb'
|
199
|
+
|
200
|
+
# Offense count: 2
|
201
|
+
# Cop supports --auto-correct.
|
202
|
+
Style/RedundantSelf:
|
203
|
+
Exclude:
|
204
|
+
- 'lib/endo/core.rb'
|
205
|
+
|
206
|
+
# Offense count: 3
|
207
|
+
# Cop supports --auto-correct.
|
208
|
+
# Configuration parameters: SupportedStyles.
|
209
|
+
# SupportedStyles: space, no_space
|
210
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
211
|
+
EnforcedStyle: no_space
|
212
|
+
|
213
|
+
# Offense count: 1
|
214
|
+
# Cop supports --auto-correct.
|
215
|
+
Style/SpaceAroundKeyword:
|
216
|
+
Exclude:
|
217
|
+
- 'lib/endo/core.rb'
|
218
|
+
|
219
|
+
# Offense count: 4
|
220
|
+
# Cop supports --auto-correct.
|
221
|
+
# Configuration parameters: AllowForAlignment.
|
222
|
+
Style/SpaceAroundOperators:
|
223
|
+
Exclude:
|
224
|
+
- 'lib/endo/core.rb'
|
225
|
+
|
226
|
+
# Offense count: 1
|
227
|
+
# Cop supports --auto-correct.
|
228
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
|
229
|
+
# SupportedStyles: space, no_space
|
230
|
+
Style/SpaceInsideBlockBraces:
|
231
|
+
Enabled: false
|
232
|
+
|
233
|
+
# Offense count: 1
|
234
|
+
# Cop supports --auto-correct.
|
235
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
236
|
+
# SupportedStyles: use_perl_names, use_english_names
|
237
|
+
Style/SpecialGlobalVars:
|
238
|
+
Enabled: false
|
239
|
+
|
240
|
+
# Offense count: 26
|
241
|
+
# Cop supports --auto-correct.
|
242
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
|
243
|
+
# SupportedStyles: single_quotes, double_quotes
|
244
|
+
Style/StringLiterals:
|
245
|
+
Enabled: false
|
246
|
+
|
247
|
+
# Offense count: 12
|
248
|
+
# Cop supports --auto-correct.
|
249
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
250
|
+
# SupportedStyles: final_newline, final_blank_line
|
251
|
+
Style/TrailingBlankLines:
|
252
|
+
Exclude:
|
253
|
+
- 'Rakefile'
|
254
|
+
- 'bin/endo'
|
255
|
+
- 'lib/endo.rb'
|
256
|
+
- 'lib/endo/core.rb'
|
257
|
+
- 'lib/endo/error.rb'
|
258
|
+
- 'lib/endo/error/http_error.rb'
|
259
|
+
- 'lib/endo/error/validation_error.rb'
|
260
|
+
- 'lib/endo/expectation_target.rb'
|
261
|
+
- 'lib/endo/matchers.rb'
|
262
|
+
- 'lib/endo/matchers/base_matcher.rb'
|
263
|
+
- 'lib/endo/matchers/eq.rb'
|
264
|
+
- 'lib/endo/matchers/include.rb'
|
265
|
+
|
266
|
+
# Offense count: 2
|
267
|
+
# Cop supports --auto-correct.
|
268
|
+
Style/UnneededPercentQ:
|
269
|
+
Exclude:
|
270
|
+
- 'endo.gemspec'
|
data/README.md
CHANGED
@@ -1,37 +1,38 @@
|
|
1
1
|
# endo
|
2
2
|
|
3
|
-
This is a tool for testing api endpoints.
|
4
|
-
In development state.
|
3
|
+
This is a tool for testing json api endpoints.
|
5
4
|
|
6
5
|
# Usage
|
7
6
|
|
8
7
|
Write endo file.
|
9
|
-
ex. endo/sample.
|
8
|
+
ex. endo/sample.rb
|
10
9
|
|
11
10
|
```ruby
|
12
|
-
|
11
|
+
base_url 'http://localhost:3000'
|
12
|
+
basic_auth 'user', 'pass'
|
13
13
|
|
14
14
|
get '/articles' do
|
15
|
-
expect(
|
15
|
+
expect(header: 'Content-Type').to eq 'application/json; charset=utf-8'
|
16
|
+
end
|
17
|
+
|
18
|
+
post '/articles.json' do
|
19
|
+
param 'article[title]', 'hello'
|
20
|
+
param 'article[content]', 'Hello, world!'
|
16
21
|
end
|
17
22
|
|
18
23
|
get '/articles/:article_id' do
|
19
24
|
param :article_id do
|
20
|
-
from :
|
25
|
+
from :post, '/articles.json', -> { self[:id] }
|
21
26
|
end
|
22
27
|
|
23
|
-
expect(header: 'Content-Type').to eq 'application/json
|
24
|
-
|
25
|
-
|
26
|
-
post '/articless' do
|
27
|
-
param :title, 'hello'
|
28
|
-
param :text, 'Hello, world!'
|
28
|
+
expect(header: 'Content-Type').to eq 'application/json'
|
29
|
+
expect(body: -> { self[:title] }).to eq 'hello'
|
29
30
|
end
|
30
31
|
```
|
31
32
|
|
32
33
|
Exec endo command.
|
33
34
|
|
34
|
-
![2016-
|
35
|
+
![ss_ 2016-05-02 16 12 58](https://cloud.githubusercontent.com/assets/1129887/14948974/f20798cc-1080-11e6-8b0d-90679c26b44e.png)
|
35
36
|
|
36
37
|
## Installation
|
37
38
|
|
@@ -51,4 +52,4 @@ Or install it yourself as:
|
|
51
52
|
|
52
53
|
```bash
|
53
54
|
$ gem install endo
|
54
|
-
```
|
55
|
+
```
|
data/endo/sample.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
base_url 'http://localhost:3000'
|
2
2
|
basic_auth 'user', 'pass'
|
3
3
|
|
4
4
|
get '/articles' do
|
@@ -12,16 +12,16 @@ end
|
|
12
12
|
|
13
13
|
get '/articles/:article_id' do
|
14
14
|
param :article_id do
|
15
|
-
from :post, '/articles.json', ->{ self[:id] }
|
15
|
+
from :post, '/articles.json', -> { self[:id] }
|
16
16
|
end
|
17
17
|
|
18
18
|
expect(header: 'Content-Type').to eq 'application/json; charset=utf-8'
|
19
|
-
expect(body: ->{ self[:title] }).to eq 'hello'
|
19
|
+
expect(body: -> { self[:title] }).to eq 'hello'
|
20
20
|
end
|
21
21
|
|
22
22
|
patch '/articles/:article_id.json' do
|
23
23
|
param :article_id do
|
24
|
-
from :post, '/articles.json', ->{ self[:id] }
|
24
|
+
from :post, '/articles.json', -> { self[:id] }
|
25
25
|
end
|
26
26
|
|
27
27
|
param 'article[title]', 'こんにちは'
|
@@ -29,15 +29,15 @@ end
|
|
29
29
|
|
30
30
|
get '/articles/:article_id' do
|
31
31
|
param :article_id do
|
32
|
-
from :post, '/articles.json', ->{ self[:id] }
|
32
|
+
from :post, '/articles.json', -> { self[:id] }
|
33
33
|
end
|
34
34
|
|
35
|
-
expect(body: ->{ self[:title] }).to eq 'こんにちは'
|
35
|
+
expect(body: -> { self[:title] }).to eq 'こんにちは'
|
36
36
|
end
|
37
37
|
|
38
38
|
put '/articles/:article_id.json' do
|
39
39
|
param :article_id do
|
40
|
-
from :post, '/articles.json', ->{ self[:id] }
|
40
|
+
from :post, '/articles.json', -> { self[:id] }
|
41
41
|
end
|
42
42
|
|
43
43
|
param 'article[title]', 'bonjour'
|
@@ -45,6 +45,6 @@ end
|
|
45
45
|
|
46
46
|
delete '/articles/:article_id.json' do
|
47
47
|
param :article_id do
|
48
|
-
from :post, '/articles.json', ->{ self[:id] }
|
48
|
+
from :post, '/articles.json', -> { self[:id] }
|
49
49
|
end
|
50
|
-
end
|
50
|
+
end
|
data/lib/endo/cli.rb
CHANGED
@@ -5,8 +5,8 @@ module Endo
|
|
5
5
|
class CLI < Thor
|
6
6
|
default_command :exec
|
7
7
|
|
8
|
-
desc
|
9
|
-
def exec(endo_file=nil)
|
8
|
+
desc 'exec usage', 'exec desc'
|
9
|
+
def exec(endo_file = nil)
|
10
10
|
if endo_file.nil?
|
11
11
|
Dir.glob('endo/*.rb').each do |f|
|
12
12
|
exec_proc(f)
|
@@ -16,12 +16,13 @@ module Endo
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
desc
|
19
|
+
desc 'version', 'Print version'
|
20
20
|
def version
|
21
21
|
puts "endo version #{Endo::VERSION}"
|
22
22
|
end
|
23
23
|
|
24
24
|
private
|
25
|
+
|
25
26
|
def exec_proc(file_path)
|
26
27
|
executor = Endo::Core.new
|
27
28
|
executor.instance_eval File.read(file_path)
|
data/lib/endo/core.rb
CHANGED
@@ -8,16 +8,10 @@ module Endo
|
|
8
8
|
include Endo::Matchers
|
9
9
|
|
10
10
|
def initialize
|
11
|
-
@props = {}
|
12
11
|
@responses = {}
|
13
12
|
@expect_alls = {}
|
14
13
|
end
|
15
14
|
|
16
|
-
def set(key, val)
|
17
|
-
#TODO: validate key
|
18
|
-
@props[key] = val
|
19
|
-
end
|
20
|
-
|
21
15
|
def get(endpoint, &block)
|
22
16
|
request(endpoint, :get, &block)
|
23
17
|
end
|
@@ -38,43 +32,39 @@ module Endo
|
|
38
32
|
request(endpoint, :put, &block)
|
39
33
|
end
|
40
34
|
|
41
|
-
#TODO: 制限
|
42
|
-
def param(key, val=nil
|
43
|
-
unless
|
44
|
-
raise ArgumentError.new('DupValue')
|
45
|
-
end
|
46
|
-
|
47
|
-
@params[key.to_s] = if val
|
48
|
-
val
|
49
|
-
else
|
50
|
-
block.call
|
51
|
-
end
|
35
|
+
# TODO: 制限
|
36
|
+
def param(key, val = nil)
|
37
|
+
raise ArgumentError, 'DupValue' unless !val.nil? ^ block_given?
|
52
38
|
|
39
|
+
@params[key.to_s] = val ? val : yield
|
53
40
|
end
|
54
41
|
|
55
|
-
#TODO: Limit scope
|
56
|
-
def from(method, endpoint, lmd=nil, &
|
42
|
+
# TODO: Limit scope
|
43
|
+
def from(method, endpoint, lmd = nil, &_block)
|
57
44
|
key = build_response_key(method, endpoint)
|
58
|
-
unless @responses.
|
59
|
-
raise RuntimeError.new("NotFoundKey [#{key}]")
|
60
|
-
end
|
45
|
+
raise RuntimeError "NotFoundKey [#{key}]" unless @responses.key? key
|
61
46
|
|
62
47
|
res = @responses[key]
|
63
|
-
val =
|
64
|
-
|
65
|
-
|
66
|
-
|
48
|
+
val = nil
|
49
|
+
if !lmd.nil?
|
50
|
+
val = res.instance_exec(&lmd)
|
51
|
+
elsif block_given?
|
52
|
+
val = yield res
|
67
53
|
else
|
68
|
-
raise ArgumentError
|
54
|
+
raise ArgumentError, 'UndefinedBlock'
|
69
55
|
end
|
70
56
|
|
71
57
|
unless val.is_a?(String) || val.is_a?(Integer) || val.is_a?(Numeric)
|
72
|
-
raise
|
58
|
+
raise 'BadValueType'
|
73
59
|
end
|
74
60
|
|
75
61
|
val
|
76
62
|
end
|
77
63
|
|
64
|
+
def base_url(url)
|
65
|
+
@base_url = url
|
66
|
+
end
|
67
|
+
|
78
68
|
def basic_auth(user, pass)
|
79
69
|
@basic_auth = {
|
80
70
|
user: user, pass: pass
|
@@ -82,27 +72,22 @@ module Endo
|
|
82
72
|
end
|
83
73
|
|
84
74
|
private
|
85
|
-
def request(endpoint, method, &block)
|
86
|
-
org_endpoint = endpoint.clone
|
87
75
|
|
76
|
+
def request(endpoint, method, &_block)
|
88
77
|
@params = {}
|
89
78
|
@expects = []
|
90
|
-
|
79
|
+
yield if block_given?
|
91
80
|
|
92
81
|
endpoint = apply_pattern_vars(endpoint, @params)
|
93
|
-
|
94
|
-
url = @props[:base_url] + endpoint
|
82
|
+
url = @base_url + endpoint
|
95
83
|
|
96
84
|
begin
|
97
|
-
|
98
|
-
res = http_request(url, @params, method: method)
|
99
|
-
t_end = Time.now.instance_eval { self.to_i * 1000 + (usec/1000) }
|
100
|
-
|
101
|
-
res_data = parse_body_json(res)
|
102
|
-
@responses[build_response_key(method, endpoint)] = res_data
|
85
|
+
res, time_ms = request_with_timer(url, method, @params)
|
103
86
|
validate_expects(res) unless @expect_alls.empty? && @expects.empty?
|
104
|
-
|
105
|
-
|
87
|
+
@responses[build_response_key(method, endpoint)] = parse_body_json(res)
|
88
|
+
|
89
|
+
message = "🍺 #{method.upcase} #{endpoint} [#{time_ms}ms]"
|
90
|
+
rescue Error::HttpError => e
|
106
91
|
message = "💩 #{method.upcase} #{endpoint} [code: #{e.code}]".red
|
107
92
|
exit 1
|
108
93
|
rescue Error::ValidationError => e
|
@@ -110,7 +95,6 @@ module Endo
|
|
110
95
|
ensure
|
111
96
|
puts message
|
112
97
|
end
|
113
|
-
|
114
98
|
end
|
115
99
|
|
116
100
|
def apply_pattern_vars(endpoint, params)
|
@@ -118,9 +102,7 @@ module Endo
|
|
118
102
|
if patterns.any?
|
119
103
|
patterns.flatten!
|
120
104
|
patterns.each do |pattern|
|
121
|
-
unless params.
|
122
|
-
raise RuntimeError.new("NotFoundPattern #{pattern}")
|
123
|
-
end
|
105
|
+
raise "NotFoundPattern #{pattern}" unless params.key? pattern
|
124
106
|
|
125
107
|
endpoint.sub!(/:#{pattern}/, params[pattern].to_s)
|
126
108
|
end
|
@@ -132,10 +114,28 @@ module Endo
|
|
132
114
|
endpoint
|
133
115
|
end
|
134
116
|
|
117
|
+
def request_with_timer(url, method, params)
|
118
|
+
t_start = Time.now.instance_eval { to_i * 1000 + (usec / 1000) }
|
119
|
+
res = http_request(url, params, method: method)
|
120
|
+
t_end = Time.now.instance_eval { to_i * 1000 + (usec / 1000) }
|
121
|
+
|
122
|
+
[res, t_end - t_start]
|
123
|
+
end
|
124
|
+
|
135
125
|
def http_request(url, params, method: :get)
|
136
126
|
uri = URI.parse url
|
127
|
+
req = create_request_each_method(uri, params, method)
|
128
|
+
|
129
|
+
req.basic_auth @basic_auth[:user], @basic_auth[:pass] if @basic_auth
|
130
|
+
|
131
|
+
res = Net::HTTP.start(uri.host, uri.port) { |http| http.request(req) }
|
132
|
+
raise Error::HttpError.new("HTTP Bad Status[#{res.code}] #{res.body}", res.code, res.body) unless /^20[0-8]$/ =~ res.code
|
133
|
+
|
134
|
+
res
|
135
|
+
end
|
137
136
|
|
138
|
-
|
137
|
+
def create_request_each_method(uri, params, method)
|
138
|
+
case method
|
139
139
|
when :get
|
140
140
|
uri.query = URI.encode_www_form(params)
|
141
141
|
Net::HTTP::Get.new uri
|
@@ -145,23 +145,14 @@ module Endo
|
|
145
145
|
req
|
146
146
|
when :delete
|
147
147
|
uri.query = URI.encode_www_form(params)
|
148
|
-
|
148
|
+
Net::HTTP::Delete.new uri
|
149
149
|
when :patch
|
150
150
|
uri.query = URI.encode_www_form(params)
|
151
|
-
|
151
|
+
Net::HTTP::Patch.new uri
|
152
152
|
when :put
|
153
153
|
uri.query = URI.encode_www_form(params)
|
154
|
-
|
154
|
+
Net::HTTP::Put.new uri
|
155
155
|
end
|
156
|
-
|
157
|
-
if @basic_auth
|
158
|
-
req.basic_auth @basic_auth[:user], @basic_auth[:pass]
|
159
|
-
end
|
160
|
-
|
161
|
-
res = Net::HTTP.start(uri.host, uri.port) {|http| http.request req }
|
162
|
-
raise Error::HttpError.new("HTTP Bad Status[#{res.code}] #{res.body}", res.code, res.body) unless /^20[0-8]$/ =~ res.code
|
163
|
-
|
164
|
-
return res
|
165
156
|
end
|
166
157
|
|
167
158
|
def parse_body_json(res)
|
@@ -173,15 +164,15 @@ module Endo
|
|
173
164
|
end
|
174
165
|
|
175
166
|
def expect(header: nil, body: nil)
|
176
|
-
|
177
|
-
ExpectationTarget.new(:header, header)
|
178
|
-
elsif
|
179
|
-
ExpectationTarget.new(:body, body)
|
167
|
+
if header
|
168
|
+
expectation = ExpectationTarget.new(:header, header)
|
169
|
+
elsif body
|
170
|
+
expectation = ExpectationTarget.new(:body, body)
|
180
171
|
else
|
181
172
|
raise 'TODO'
|
182
173
|
end
|
183
174
|
@expects << expectation
|
184
|
-
|
175
|
+
expectation
|
185
176
|
end
|
186
177
|
|
187
178
|
def validate_expects(res)
|
@@ -191,6 +182,5 @@ module Endo
|
|
191
182
|
end
|
192
183
|
end
|
193
184
|
end
|
194
|
-
|
195
185
|
end
|
196
|
-
end
|
186
|
+
end
|
data/lib/endo/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: endo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maruware
|
@@ -89,6 +89,8 @@ extensions: []
|
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
91
|
- ".gitignore"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".rubocop_todo.yml"
|
92
94
|
- Gemfile
|
93
95
|
- README.md
|
94
96
|
- Rakefile
|