stub_requests 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a536f3b0a7ae3cf0c19b91fd9f3c00ba97990f192c0361a070e993702bc69c5e
4
- data.tar.gz: a8bb04de37a709efaf4859a71ac81c9c360109faf392ef0505ebb3e195fb5e68
3
+ metadata.gz: 790cf23df8e8bd3f6532cb0e3008bf41150c189135ec7eed33ac795523325f20
4
+ data.tar.gz: 7c546bc75dc7aeb88aa7d92addc7455f1d240feca299427c4da970eceb20116b
5
5
  SHA512:
6
- metadata.gz: 16c128e4b4a894c4478eaa697aafb84aa467d7c51a0d6086340f30dd7f8acf969254cb506d6a52bc19d6c0f148af18492c4461a4b07ba988b0ee01eb223d232e
7
- data.tar.gz: c30405764a9064978698b3fdbf6046bd8a01cac0a2ace240b4b7f8092f7e1716f108e0b01a26fef24216ac444c134377a518a4537d97a69b18314d33adf0ee8c
6
+ metadata.gz: 68a87e36d03c65705c587417600ec9dd798c92ecfd56df45da50fce6b8471a8f460e27a8a1c34a5787daff77c54a383caf79bff35eb98c6da937ec0255659083
7
+ data.tar.gz: 92fccfea4f37480e5fb4cfff7863288897b7741fcd606848eb702b1a73459d66a1acf862ffa27fd171913defd513e2e702769a02f809285b5a48ff7818c1021e
data/.reek.yml CHANGED
@@ -17,6 +17,7 @@ detectors:
17
17
  exclude:
18
18
  - initialize
19
19
  - RSpec::SubjectAsLambda#it!
20
+ - StubRequests::UriSegmentMismatch#message_parts
20
21
  DataClump:
21
22
  exclude:
22
23
  - StubRequests::Concerns::RegisterVerb
@@ -1,8 +1,7 @@
1
1
  # Change Log
2
2
 
3
- ## [Unreleased](https://github.com/mhenrixon/stub_requests/tree/HEAD)
4
-
5
- [Full Changelog](https://github.com/mhenrixon/stub_requests/compare/v0.1.9...HEAD)
3
+ ## [v0.1.10](https://github.com/mhenrixon/stub_requests/tree/v0.1.10) (2019-02-07)
4
+ [Full Changelog](https://github.com/mhenrixon/stub_requests/compare/v0.1.9...v0.1.10)
6
5
 
7
6
  **Implemented enhancements:**
8
7
 
data/Rakefile CHANGED
@@ -23,8 +23,8 @@ task default: :spec
23
23
  namespace :stub_requests do
24
24
  task :release do
25
25
  sh("./update_docs.sh")
26
- Rake::Task["changelog"].invoke
27
26
  sh("gem release --tag --push")
27
+ Rake::Task["changelog"].invoke
28
28
  sh("gem bump")
29
29
  end
30
30
  end
@@ -72,7 +72,7 @@ module StubRequests
72
72
  #
73
73
  def stub_endpoint(endpoint_id, route_params = {}, &callback)
74
74
  endpoint = EndpointRegistry.instance.find!(endpoint_id)
75
- uri = URI::Builder.build(endpoint.service_uri, endpoint.path, route_params)
75
+ uri = URI::Builder.build(endpoint.uri, route_params)
76
76
  webmock_stub = WebMock::Builder.build(endpoint.verb, uri, &callback)
77
77
 
78
78
  StubRegistry.instance.record(endpoint.id, webmock_stub)
@@ -82,7 +82,7 @@ module StubRequests
82
82
  # :nodoc:
83
83
  def __stub_endpoint(endpoint_id, route_params = {})
84
84
  endpoint = EndpointRegistry.instance.find!(endpoint_id)
85
- uri = URI::Builder.build(endpoint.service_uri, endpoint.path, route_params)
85
+ uri = URI::Builder.build(endpoint.uri, route_params)
86
86
  endpoint_stub = WebMock::Builder.build(endpoint.verb, uri)
87
87
 
88
88
  ::WebMock::StubRegistry.instance.register_request_stub(endpoint_stub)
@@ -94,5 +94,54 @@ module StubRequests
94
94
  #
95
95
  # UriSegmentMismatch is raised when a segment cannot be replaced
96
96
  #
97
- class UriSegmentMismatch < Error; end
97
+ class UriSegmentMismatch < Error
98
+ attr_reader :uri, :expected_keys, :received_keys
99
+ def initialize(uri:, expected_keys:, received_keys:)
100
+ @uri = uri
101
+ @expected_keys = expected_keys
102
+ @received_keys = received_keys
103
+
104
+ super(message_parts.join("\n "))
105
+ end
106
+
107
+ private
108
+
109
+ def message_parts
110
+ [].tap do |arr|
111
+ arr << default_part
112
+ arr << expected_keys_part
113
+ arr << received_keys_part
114
+ arr << missing_keys_part if missing_keys.any?
115
+ arr << invalid_keys_part if invalid_keys.any?
116
+ end
117
+ end
118
+
119
+ def default_part
120
+ "The URI (#{uri}) received unexpected route parameters"
121
+ end
122
+
123
+ def expected_keys_part
124
+ "Expected: [#{expected_keys.join(',')}]"
125
+ end
126
+
127
+ def received_keys_part
128
+ "Received: [#{received_keys.join(',')}]"
129
+ end
130
+
131
+ def missing_keys_part
132
+ "Missing: [#{missing_keys.join(',')}]"
133
+ end
134
+
135
+ def invalid_keys_part
136
+ "Invalid: [#{invalid_keys.join(',')}]"
137
+ end
138
+
139
+ def missing_keys
140
+ @missing_keys ||= expected_keys - received_keys
141
+ end
142
+
143
+ def invalid_keys
144
+ @invalid_keys ||= received_keys - expected_keys
145
+ end
146
+ end
98
147
  end
@@ -21,7 +21,7 @@ module StubRequests
21
21
  class Builder
22
22
  #
23
23
  # @return [Regexp] A pattern for matching url segment keys
24
- URL_SEGMENT_REGEX = /(:\w+)/.freeze
24
+ URI_KEY = /(:[a-zA-Z_]+)/.freeze
25
25
 
26
26
  #
27
27
  # Convenience method to avoid .new.build
@@ -30,54 +30,44 @@ module StubRequests
30
30
  # @raise [UriSegmentMismatch] when there are unused URI segments
31
31
  # @raise [UriSegmentMismatch] when the template have unplaced URI segments
32
32
  #
33
- # @param [String] host the URI used to reach the service
34
- # @param [String] template the endpoint template
33
+ # @param [String] the URI to the service endpoint
35
34
  # @param [Hash<Symbol>] route_params a list of uri_replacement keys
36
35
  #
37
36
  # @return [String] a validated URI string
38
37
  #
39
- def self.build(host, template, route_params = {})
40
- new(host, template, route_params).build
38
+ def self.build(uri, route_params = {})
39
+ new(uri, route_params).build
41
40
  end
42
41
 
43
42
  #
44
43
  # @!attribute [r] uri
45
- # @return [String] the request host {Service#service_uri}
46
- attr_reader :host
47
- #
48
- # @!attribute [r] template
49
- # @return [String] a string template for the endpoint
50
- attr_reader :template
51
- #
52
- # @!attribute [r] path
53
- # @return [String] a valid URI path
54
- attr_reader :path
44
+ # @return [String] the URI to the service endpoint
45
+ attr_reader :uri
55
46
  #
56
47
  # @!attribute [r] route_params
57
48
  # @return [Hash<Symbol] a hash with keys matching the {#template}
58
49
  attr_reader :route_params
59
50
  #
60
- # @!attribute [r] unused
61
- # @return [Array<String>] a list with unused {#route_params}
62
- attr_reader :unused
51
+ # @!attribute [r] received_keys
52
+ # @return [Array<String>] a list with actual {#route_params} keys
53
+ attr_reader :received_keys
63
54
  #
64
- # @!attribute [r] unreplaced
65
- # @return [Array<String>] a list of uri_segments that should have been replaced
66
- attr_reader :unreplaced
55
+ # @!attribute [r] expected_keys
56
+ # @return [Array<String>] a list of expected route keys
57
+ attr_reader :expected_keys
67
58
 
68
59
  #
69
60
  # Initializes a new Builder
70
61
  #
71
62
  #
72
- # @param [String] host the URI used to reach the service
73
- # @param [String] template the endpoint template
63
+ # @param [String] uri the URI used to reach the service
74
64
  # @param [Hash<Symbol>] route_params a list of uri_replacement keys
75
65
  #
76
- def initialize(host, template, route_params = {})
77
- @host = +host
78
- @template = +template
79
- @path = +@template.dup
80
- @route_params = route_params.to_route_param
66
+ def initialize(uri, route_params = {})
67
+ @uri = +uri
68
+ @route_params = route_params.to_route_param
69
+ @received_keys = @route_params.keys
70
+ @expected_keys = uri.scan(URI_KEY).flatten.uniq
81
71
  end
82
72
 
83
73
  #
@@ -90,104 +80,33 @@ module StubRequests
90
80
  # @return [String] a validated URI string
91
81
  #
92
82
  def build
83
+ validate_uri_has_route_params!
93
84
  build_uri
94
- run_validations
85
+ validate_uri
95
86
 
96
87
  uri
97
88
  end
98
89
 
99
90
  private
100
91
 
101
- def build_uri
102
- replace_segments
103
- parse_unreplaced_segments
104
- end
105
-
106
- def uri
107
- @uri ||= [host, path].join("/")
108
- end
109
-
110
- def run_validations
111
- validate_route_params_used
112
- validate_route_keys_replaced
113
- validate_uri
114
- end
115
-
116
- #
117
- # Replaces the URI segments with the arguments in route_params
118
- #
119
- #
120
- # @return [Array] an list with unused route_params
121
- #
122
- def replace_segments
123
- @unused = route_params.map do |key, value|
124
- next key unless path.include?(key)
125
-
126
- path.gsub!(key, value.to_s)
127
- next
128
- end.compact
129
- end
130
-
131
- #
132
- # Validates that all route_params have been used
133
- #
134
- #
135
- # @raise [UriSegmentMismatch] when there are unused route_params
136
- #
137
- # @return [void]
138
- #
139
- def validate_route_params_used
140
- return if replacents_used?
141
-
142
- raise UriSegmentMismatch,
143
- "The URI segment(s) [#{unused.join(',')}] are missing in template (#{path})"
144
- end
92
+ def validate_uri_has_route_params!
93
+ return if validate_uri_has_route_params
145
94
 
146
- #
147
- # Checks that no route_params are left
148
- #
149
- #
150
- # @return [true,false]
151
- #
152
- def replacents_used?
153
- unused.none?
95
+ raise UriSegmentMismatch, uri: uri, expected_keys: expected_keys, received_keys: received_keys
154
96
  end
155
97
 
156
- #
157
- # Validates that all URI segments have been replaced in {#path}
158
- #
159
- #
160
- # @raise [UriSegmentMismatch] when the path have unplaced URI segments
161
- #
162
- # @return [void]
163
- #
164
- def validate_route_keys_replaced
165
- return if route_keys_replaced?
166
-
167
- raise UriSegmentMismatch,
168
- "The URI segment(s) [#{unreplaced.join(',')}]" \
169
- " were not replaced in template (#{path})." \
170
- " Given route_params=[#{route_params.keys.join(',')}]"
98
+ def validate_uri_has_route_params
99
+ expected_keys.sort == received_keys.sort
171
100
  end
172
101
 
173
- #
174
- # Checks that all URI keys were replaced
175
- #
176
- #
177
- # @return [true,false]
178
- #
179
- def route_keys_replaced?
180
- unreplaced.none?
102
+ def build_uri
103
+ route_params.each do |key, value|
104
+ replace_key(key, value)
105
+ end
181
106
  end
182
107
 
183
- #
184
- # Parses out all unused URI segments
185
- #
186
- #
187
- # @return [Array<String>] a list of not replaced uri_segments
188
- #
189
- def parse_unreplaced_segments
190
- @unreplaced = URL_SEGMENT_REGEX.match(path).to_a.uniq
108
+ def replace_key(key, value)
109
+ uri.gsub!(key, value.to_s)
191
110
  end
192
111
 
193
112
  #
@@ -9,5 +9,5 @@
9
9
  module StubRequests
10
10
  #
11
11
  # @return [String] a version string
12
- VERSION = "0.1.10"
12
+ VERSION = "0.1.11"
13
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stub_requests
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-07 00:00:00.000000000 Z
11
+ date: 2019-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docile