endo 0.4.0 → 0.4.1

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: ac22efa794a76374a866f7c8f19ee7b7a261efda
4
- data.tar.gz: 25f3b904a025040605012eb92327951ea8b87310
3
+ metadata.gz: 30a81727bcc13e0b820d605e38a94e5de3efc804
4
+ data.tar.gz: 26ee7de9bacac40fc237f6a32d5f790f3976d9e9
5
5
  SHA512:
6
- metadata.gz: 727908b09dfa7314bd86b96ed4a6afda60dede68beb0de41f0bd9bb715850a9979f74a72835211dcf537c0705271f861459f8370c6e83a645d40ce361ad32603
7
- data.tar.gz: 5f501ccbfc7ec6f91a07d480cef5f120dfde0bc9b47c0d33325af782b213a6f6254473295c3f1d7ebb6512c7a953d6378ceab0e6ad7f24e3b95dabde1b126a6c
6
+ metadata.gz: b2a5f170105a8bbf39b851ffa613c0d89341b55ff4ff6113e1b328fdd8c89ce30532d399baba383a9122272bf826de39c232d2de6e33f915eb85b6b3128bd85e
7
+ data.tar.gz: 7db7ace6ca42dcacf4cc16a68e23af76adab5ff0f95abb0cc0417420bfca69087a9a773d291b0e3d3d1691ebd8c0c82a4843832212cc5bef330568f665f6e019
data/lib/endo/cli.rb CHANGED
@@ -25,7 +25,7 @@ module Endo
25
25
 
26
26
  def exec_proc(file_path)
27
27
  executor = Endo::Core.new
28
- executor.instance_eval File.read(file_path)
28
+ executor.instance_eval(File.read(file_path), file_path)
29
29
  end
30
30
  end
31
31
  end
data/lib/endo/core.rb CHANGED
@@ -9,7 +9,16 @@ module Endo
9
9
 
10
10
  def initialize
11
11
  @responses = {}
12
- @expect_alls = {}
12
+ end
13
+
14
+ def base_url(url)
15
+ @base_url = url
16
+ end
17
+
18
+ def basic_auth(user, pass)
19
+ @basic_auth = {
20
+ user: user, pass: pass
21
+ }
13
22
  end
14
23
 
15
24
  def get(endpoint, &block)
@@ -32,17 +41,16 @@ module Endo
32
41
  request(endpoint, :put, &block)
33
42
  end
34
43
 
35
- # TODO: 制限
44
+ # TODO: Limit scope
36
45
  def param(key, val = nil)
37
46
  raise ArgumentError, 'DupValue' unless !val.nil? ^ block_given?
38
-
39
47
  @params[key.to_s] = val ? val : yield
40
48
  end
41
49
 
42
50
  # TODO: Limit scope
43
51
  def from(method, endpoint, lmd = nil, &_block)
44
52
  key = build_response_key(method, endpoint)
45
- raise RuntimeError "NotFoundKey [#{key}]" unless @responses.key? key
53
+ raise "NotFoundKey [#{key}]" unless @responses.key? key
46
54
 
47
55
  res = @responses[key]
48
56
  val = nil
@@ -61,32 +69,15 @@ module Endo
61
69
  val
62
70
  end
63
71
 
64
- def base_url(url)
65
- @base_url = url
66
- end
67
-
68
- def basic_auth(user, pass)
69
- @basic_auth = {
70
- user: user, pass: pass
71
- }
72
- end
73
-
74
72
  private
75
73
 
76
- def request(endpoint, method, &_block)
77
- @params = {}
78
- @expects = []
79
- yield if block_given?
80
-
81
- endpoint = apply_pattern_vars(endpoint, @params)
82
- url = @base_url + endpoint
83
-
74
+ def request(endpoint, method, &block)
84
75
  begin
85
- res, time_ms = request_with_timer(url, method, @params)
86
- validate_expects(res) unless @expect_alls.empty? && @expects.empty?
87
- @responses[build_response_key(method, endpoint)] = parse_body_json(res)
88
-
89
- message = "🍺 #{method.upcase} #{endpoint} [#{time_ms}ms]"
76
+ res_obj, duration_ms = request_proc(endpoint, method, &block)
77
+ message = "🍺 #{method.upcase} #{endpoint} [#{duration_ms}ms]"
78
+ rescue Errno::ECONNREFUSED => e
79
+ message = "💀 #{e.message}".red
80
+ exit 1
90
81
  rescue Error::HttpError => e
91
82
  message = "💩 #{method.upcase} #{endpoint} [code: #{e.code}]".red
92
83
  exit 1
@@ -97,21 +88,21 @@ module Endo
97
88
  end
98
89
  end
99
90
 
100
- def apply_pattern_vars(endpoint, params)
101
- patterns = endpoint.scan(/:(\w+)/)
102
- if patterns.any?
103
- patterns.flatten!
104
- patterns.each do |pattern|
105
- raise "NotFoundPattern #{pattern}" unless params.key? pattern
91
+ def request_proc(endpoint, method, &_block)
92
+ @params = {}
93
+ @expects = []
94
+ yield if block_given?
106
95
 
107
- endpoint.sub!(/:#{pattern}/, params[pattern].to_s)
108
- end
96
+ endpoint = apply_params_to_pattern(endpoint, @params)
97
+ url = @base_url + endpoint
109
98
 
110
- patterns.uniq.each do |pattern|
111
- params.delete(pattern)
112
- end
113
- end
114
- endpoint
99
+ res, duration_ms = request_with_timer(url, method, @params)
100
+ validate_expects(res) if @expects.any?
101
+
102
+ res_obj = parse_body_json(res)
103
+ @responses[build_response_key(method, endpoint)] = parse_body_json(res)
104
+
105
+ [res_obj, duration_ms]
115
106
  end
116
107
 
117
108
  def request_with_timer(url, method, params)
@@ -163,13 +154,31 @@ module Endo
163
154
  "#{method}:#{endpoint}"
164
155
  end
165
156
 
157
+ def apply_params_to_pattern(endpoint, params)
158
+ patterns = endpoint.scan(/:(\w+)/)
159
+ if patterns.any?
160
+ patterns.flatten!
161
+ patterns.each do |pattern|
162
+ raise "NotFoundPattern #{pattern}" unless params.key? pattern
163
+ endpoint.sub!(/:#{pattern}/, params[pattern].to_s)
164
+ end
165
+
166
+ patterns.uniq.each do |pattern|
167
+ params.delete(pattern)
168
+ end
169
+ end
170
+ endpoint
171
+ end
172
+
166
173
  def expect(header: nil, body: nil)
174
+ unless !header.nil? ^ !body.nil?
175
+ raise ArgumentError, '"expect" must be called with header or body'
176
+ end
177
+
167
178
  if header
168
179
  expectation = ExpectationTarget.new(:header, header)
169
180
  elsif body
170
181
  expectation = ExpectationTarget.new(:body, body)
171
- else
172
- raise 'TODO'
173
182
  end
174
183
  @expects << expectation
175
184
  expectation
data/lib/endo/error.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  require 'endo/error/http_error'
2
- require 'endo/error/validation_error'
2
+ require 'endo/error/validation_error'
@@ -12,21 +12,18 @@ module Endo
12
12
  end
13
13
 
14
14
  def matches?(res)
15
- @actual = case @target
15
+ case @target
16
16
  when :header
17
- res.header[@query]
17
+ @actual = res.header[@query]
18
18
  when :body
19
19
  obj = JSON.parse res.body, symbolize_names: true
20
- obj.instance_exec(&@query)
20
+ @actual = obj.instance_exec(&@query)
21
21
  end
22
22
  @matcher.matches?(@actual)
23
23
  end
24
24
 
25
-
26
25
  def expected
27
26
  @matcher.expected
28
27
  end
29
-
30
-
31
28
  end
32
- end
29
+ end
data/lib/endo/matchers.rb CHANGED
@@ -2,7 +2,6 @@ require 'endo/matchers/base_matcher'
2
2
  require 'endo/matchers/eq'
3
3
  require 'endo/matchers/include'
4
4
 
5
-
6
5
  module Endo
7
6
  module Matchers
8
7
  def eq(expected)
@@ -13,4 +12,4 @@ module Endo
13
12
  Matchers::Include.new(expected)
14
13
  end
15
14
  end
16
- end
15
+ end
@@ -12,4 +12,4 @@ module Endo
12
12
  end
13
13
  end
14
14
  end
15
- end
15
+ end
@@ -4,9 +4,10 @@ module Endo
4
4
  attr_reader :expected
5
5
 
6
6
  private
7
- def match(expected, actual)
8
- actual == expected
9
- end
7
+
8
+ def match(expected, actual)
9
+ actual == expected
10
+ end
10
11
  end
11
12
  end
12
- end
13
+ end
data/lib/endo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Endo
2
- VERSION = "0.4.0"
2
+ VERSION = '0.4.1'
3
3
  end
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.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maruware