stub_requests 0.1.9 → 0.1.10

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.reek.yml +5 -9
  3. data/.rubocop.yml +1 -0
  4. data/CHANGELOG.md +23 -0
  5. data/README.md +9 -9
  6. data/Rakefile +7 -5
  7. data/lib/stub_requests.rb +30 -16
  8. data/lib/stub_requests/api.rb +45 -26
  9. data/lib/stub_requests/callback.rb +3 -1
  10. data/lib/stub_requests/callback_registry.rb +9 -55
  11. data/lib/stub_requests/concerns/argument_validation.rb +47 -0
  12. data/lib/stub_requests/concerns/property.rb +114 -0
  13. data/lib/stub_requests/concerns/property/validator.rb +137 -0
  14. data/lib/stub_requests/concerns/register_verb.rb +110 -0
  15. data/lib/stub_requests/configuration.rb +19 -2
  16. data/lib/stub_requests/dsl.rb +5 -6
  17. data/lib/stub_requests/dsl/method_definition.rb +2 -8
  18. data/lib/stub_requests/endpoint.rb +22 -23
  19. data/lib/stub_requests/endpoint_registry.rb +157 -0
  20. data/lib/stub_requests/exceptions.rb +28 -10
  21. data/lib/stub_requests/request_stub.rb +29 -14
  22. data/lib/stub_requests/service.rb +55 -7
  23. data/lib/stub_requests/service_registry.rb +30 -79
  24. data/lib/stub_requests/stub_registry.rb +22 -80
  25. data/lib/stub_requests/stub_requests.rb +8 -5
  26. data/lib/stub_requests/uri.rb +0 -17
  27. data/lib/stub_requests/utils/fuzzy.rb +70 -0
  28. data/lib/stub_requests/version.rb +1 -1
  29. data/lib/stub_requests/webmock/builder.rb +9 -51
  30. data/lib/stub_requests/webmock/stub_registry_extension.rb +1 -1
  31. data/lib/tasks/changelog.rake +1 -7
  32. data/stub_requests.gemspec +1 -0
  33. data/update_docs.sh +2 -2
  34. metadata +27 -8
  35. data/lib/stub_requests/argument_validation.rb +0 -48
  36. data/lib/stub_requests/endpoint_stub.rb +0 -89
  37. data/lib/stub_requests/endpoints.rb +0 -246
  38. data/lib/stub_requests/hash_util.rb +0 -32
  39. data/lib/stub_requests/observable.rb +0 -18
  40. data/lib/stub_requests/property.rb +0 -112
  41. data/lib/stub_requests/property/validator.rb +0 -135
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6f04591bc43a8c2ecb505a719517f6b07520c8ca85d603f6aa4b5f4219bbf25
4
- data.tar.gz: 2ae8bde3a1d335d5a855809624a205fa386bb953a147f1486578eecde44c08d0
3
+ metadata.gz: a536f3b0a7ae3cf0c19b91fd9f3c00ba97990f192c0361a070e993702bc69c5e
4
+ data.tar.gz: a8bb04de37a709efaf4859a71ac81c9c360109faf392ef0505ebb3e195fb5e68
5
5
  SHA512:
6
- metadata.gz: 2d6bbfaeb2eeaf0751bf8e3893229fc359b5cfd38f97f28aae23dde2ff26561c332ee3f6845fc52f288a533fcaed6cf3933a9ee201263482a7d15d62d4124663
7
- data.tar.gz: 3aa7bd9c09bf54c62d6895a50edcdcfe37f966317da927c0cbb7bbb84b8bdb783c9346d559c2cad06fe573488f52a5a428d18d22d7a3e22162399dcd8b8d62ed
6
+ metadata.gz: 16c128e4b4a894c4478eaa697aafb84aa467d7c51a0d6086340f30dd7f8acf969254cb506d6a52bc19d6c0f148af18492c4461a4b07ba988b0ee01eb223d232e
7
+ data.tar.gz: c30405764a9064978698b3fdbf6046bd8a01cac0a2ace240b4b7f8092f7e1716f108e0b01a26fef24216ac444c134377a518a4537d97a69b18314d33adf0ee8c
data/.reek.yml CHANGED
@@ -19,6 +19,7 @@ detectors:
19
19
  - RSpec::SubjectAsLambda#it!
20
20
  DataClump:
21
21
  exclude:
22
+ - StubRequests::Concerns::RegisterVerb
22
23
  - StubRequests::API
23
24
  - StubRequests::Endpoints
24
25
  - StubRequests::StubRegistry
@@ -27,20 +28,14 @@ detectors:
27
28
  - StubRequests::API#register_callback
28
29
  - StubRequests::CallbackRegistry#register
29
30
  - StubRequests::CallbackRegistry#self.register
30
- - StubRequests::Property::Validator#self.call
31
+ - StubRequests::Concerns::Property::Validator#self.call
31
32
  UtilityFunction:
32
33
  exclude:
33
- - StubRequests::API#define_stubs
34
- - StubRequests::API#print_stubs
35
- - StubRequests::API#register_callback
36
- - StubRequests::API#register_service
37
- - StubRequests::API#stub_endpoint
38
- - StubRequests::API#unregister_callback
39
- - StubRequests::StubRegistry#initialize_endpoint_stub
34
+ - StubRequests::API
40
35
  ControlParameter:
41
36
  exclude:
42
37
  - StubRequests::CallbackRegistry#find_by
43
- - StubRequests::Property::Validator#initialize
38
+ - StubRequests::Concerns::Property::Validator#initialize
44
39
  FeatureEnvy:
45
40
  exclude:
46
41
  - StubRequests::CallbackRegistry#dispatch_callback
@@ -52,4 +47,5 @@ detectors:
52
47
  - StubRequests#logger
53
48
  TooManyInstanceVariables:
54
49
  exclude:
50
+ - StubRequests::Endpoint
55
51
  - StubRequests::URI::Builder
data/.rubocop.yml CHANGED
@@ -49,6 +49,7 @@ Naming/FileName:
49
49
  Naming/UncommunicativeMethodParamName:
50
50
  AllowedNames:
51
51
  - ex
52
+ - id
52
53
 
53
54
  AlignLeftLetBrace:
54
55
  Enabled: true
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
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)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - More helpful stub methods [\#29](https://github.com/mhenrixon/stub_requests/pull/29) ([mhenrixon](https://github.com/mhenrixon))
10
+
11
+ **Merged pull requests:**
12
+
13
+ - Update Changelog [\#28](https://github.com/mhenrixon/stub_requests/pull/28) ([mhenrixon](https://github.com/mhenrixon))
14
+
15
+ ## [v0.1.9](https://github.com/mhenrixon/stub_requests/tree/v0.1.9) (2019-02-07)
16
+ [Full Changelog](https://github.com/mhenrixon/stub_requests/compare/v0.1.8...v0.1.9)
17
+
18
+ ## [v0.1.8](https://github.com/mhenrixon/stub_requests/tree/v0.1.8) (2019-02-07)
19
+ [Full Changelog](https://github.com/mhenrixon/stub_requests/compare/v0.1.7...v0.1.8)
20
+
21
+ **Merged pull requests:**
22
+
23
+ - Reduce number of warnings [\#27](https://github.com/mhenrixon/stub_requests/pull/27) ([mhenrixon](https://github.com/mhenrixon))
24
+ - Update Changelog [\#26](https://github.com/mhenrixon/stub_requests/pull/26) ([mhenrixon](https://github.com/mhenrixon))
25
+
3
26
  ## [v0.1.7](https://github.com/mhenrixon/stub_requests/tree/v0.1.7) (2019-02-07)
4
27
  [Full Changelog](https://github.com/mhenrixon/stub_requests/compare/v0.1.6...v0.1.7)
5
28
 
data/README.md CHANGED
@@ -84,7 +84,7 @@ Now we have a list of endpoints we can stub.
84
84
  ### Stubbing service endpoints
85
85
 
86
86
  ```ruby
87
- StubRequests.stub_endpoint(:google_ads, :index)
87
+ StubRequests.stub_endpoint(:ads_index)
88
88
  .to_return(code: 204, body: "")
89
89
 
90
90
  # This is the equivalent of doing the following in WebMock
@@ -95,7 +95,7 @@ WebMock.stub_request(:get, "#{Settings.google_ads_base_uri}/ads")
95
95
  ```
96
96
 
97
97
  ```ruby
98
- StubRequests.stub_endpoint(:google_ads, :update, id: 1) do
98
+ StubRequests.stub_endpoint(:ads_update, id: 1) do
99
99
  with(body: request_body.to_json)
100
100
  to_return(code: 200, body: response_body.to_json)
101
101
  end
@@ -117,7 +117,7 @@ Metrics collection are by default turned off. It can be turned on by the followi
117
117
 
118
118
  ```ruby
119
119
  StubRequests.configure do |config|
120
- config.record_metrics = true
120
+ config.record_stubs = true
121
121
  end
122
122
  ```
123
123
 
@@ -233,32 +233,32 @@ StubRequests.print_stubs(:documents)
233
233
  #
234
234
  module DocumentStubs
235
235
  def stub_documents_show(id:, &block)
236
- StubRequests.stub_endpoint(:documents, :documents_show, id: id, &block)
236
+ StubRequests.stub_endpoint(:documents_show, id: id, &block)
237
237
  end
238
238
 
239
239
 
240
240
  def stub_documents_index(&block)
241
- StubRequests.stub_endpoint(:documents, :documents_index, &block)
241
+ StubRequests.stub_endpoint(:documents_index, &block)
242
242
  end
243
243
 
244
244
 
245
245
  def stub_documents_create(&block)
246
- StubRequests.stub_endpoint(:documents, :documents_create, &block)
246
+ StubRequests.stub_endpoint(:documents_create, &block)
247
247
  end
248
248
 
249
249
 
250
250
  def stub_documents_update(id:, &block)
251
- StubRequests.stub_endpoint(:documents, :documents_update, id: id, &block)
251
+ StubRequests.stub_endpoint(:documents_update, id: id, &block)
252
252
  end
253
253
 
254
254
 
255
255
  def stub_document_put(id:, &block)
256
- StubRequests.stub_endpoint(:documents, :document_put, id: id, &block)
256
+ StubRequests.stub_endpoint(:document_put, id: id, &block)
257
257
  end
258
258
 
259
259
 
260
260
  def stub_documents_destroy(id:, &block)
261
- StubRequests.stub_endpoint(:documents, :documents_destroy, id: id, &block)
261
+ StubRequests.stub_endpoint(:documents_destroy, id: id, &block)
262
262
  end
263
263
  end
264
264
  ```
data/Rakefile CHANGED
@@ -20,9 +20,11 @@ end
20
20
 
21
21
  task default: :spec
22
22
 
23
- task :release do
24
- sh("gem release --tag --push")
25
- Rake::Task["changelog"].invoke
26
- sh("./update_docs.sh")
27
- sh("gem bump")
23
+ namespace :stub_requests do
24
+ task :release do
25
+ sh("./update_docs.sh")
26
+ Rake::Task["changelog"].invoke
27
+ sh("gem release --tag --push")
28
+ sh("gem bump")
29
+ end
28
30
  end
data/lib/stub_requests.rb CHANGED
@@ -1,24 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #
4
+ # 1. Require core classes
5
+ #
3
6
  require "forwardable"
4
7
  require "singleton"
5
8
 
9
+ #
10
+ # 2. Require from gems
11
+ #
12
+ require "addressable/uri"
13
+ require "jaro_winkler"
6
14
  require "concurrent/array"
7
15
  require "concurrent/map"
8
16
  require "docile"
17
+ require "public_suffix"
9
18
  require "webmock"
10
19
  require "webmock/api"
11
20
  require "webmock/stub_registry"
12
21
  require "webmock/request_stub"
13
22
 
14
- require "stub_requests/version"
15
-
16
- require "stub_requests/argument_validation"
23
+ #
24
+ # 3. Require shared functionality
25
+ #
17
26
  require "stub_requests/core_ext"
18
27
  require "stub_requests/exceptions"
19
- require "stub_requests/hash_util"
20
- require "stub_requests/property"
21
- require "stub_requests/property/validator"
28
+ require "stub_requests/utils/fuzzy"
29
+ require "stub_requests/concerns/argument_validation"
30
+ require "stub_requests/concerns/property"
31
+ require "stub_requests/concerns/property/validator"
32
+ require "stub_requests/concerns/register_verb"
22
33
  require "stub_requests/uri"
23
34
  require "stub_requests/uri/scheme"
24
35
  require "stub_requests/uri/suffix"
@@ -26,23 +37,26 @@ require "stub_requests/uri/validator"
26
37
  require "stub_requests/uri/builder"
27
38
  require "stub_requests/configuration"
28
39
 
40
+ #
41
+ # 4. Require core functionality
42
+ #
29
43
  require "stub_requests/callback"
30
44
  require "stub_requests/callback_registry"
31
-
32
- require "stub_requests/endpoint_stub"
33
- require "stub_requests/request_stub"
34
- require "stub_requests/stub_registry"
35
-
36
- require "stub_requests/endpoints"
45
+ require "stub_requests/dsl"
46
+ require "stub_requests/dsl/method_definition"
47
+ require "stub_requests/dsl/define_method"
37
48
  require "stub_requests/endpoint"
49
+ require "stub_requests/endpoint_registry"
50
+ require "stub_requests/request_stub"
38
51
  require "stub_requests/service"
39
52
  require "stub_requests/service_registry"
40
-
53
+ require "stub_requests/stub_registry"
41
54
  require "stub_requests/webmock/builder"
42
55
  require "stub_requests/webmock/stub_registry_extension"
43
56
 
57
+ #
58
+ # 5. Require public API
59
+ #
44
60
  require "stub_requests/api"
61
+ require "stub_requests/version"
45
62
  require "stub_requests/stub_requests"
46
- require "stub_requests/dsl/method_definition"
47
- require "stub_requests/dsl/define_method"
48
- require "stub_requests/dsl"
@@ -43,7 +43,49 @@ module StubRequests
43
43
  # @return [Service] a new service or a previously registered service
44
44
  #
45
45
  def register_service(service_id, service_uri, &block)
46
- StubRequests::ServiceRegistry.register_service(service_id, service_uri, &block)
46
+ service = ServiceRegistry.instance.register(service_id, service_uri)
47
+ Docile.dsl_eval(service, &block) if block.present?
48
+ service
49
+ end
50
+ alias register_service2 register_service
51
+
52
+ #
53
+ # Stub a request to a registered service endpoint
54
+ #
55
+ #
56
+ # @param [Symbol] endpoint_id the id of a registered endpoint
57
+ # @param [Hash<Symbol>] route_params a map with route parameters
58
+ #
59
+ # @note the kind of timeout error raised by webmock is depending on the HTTP client used
60
+ #
61
+ # @example Stub a request to a registered service endpoint
62
+ # stub_endpoint(:get_map_location, id: 1)
63
+ # .to_return(body: "No content", status: 204)
64
+ #
65
+ # @example Stub a request to a registered service endpoint using block
66
+ # stub_endpoint(:documents_index) do
67
+ # with(headers: { "Accept" => "application/json" }}})
68
+ # to_return(body: "No content", status: 204)
69
+ # end
70
+ #
71
+ # @return [WebMock::RequestStub] a mocked request
72
+ #
73
+ def stub_endpoint(endpoint_id, route_params = {}, &callback)
74
+ endpoint = EndpointRegistry.instance.find!(endpoint_id)
75
+ uri = URI::Builder.build(endpoint.service_uri, endpoint.path, route_params)
76
+ webmock_stub = WebMock::Builder.build(endpoint.verb, uri, &callback)
77
+
78
+ StubRegistry.instance.record(endpoint.id, webmock_stub)
79
+ ::WebMock::StubRegistry.instance.register_request_stub(webmock_stub)
80
+ end
81
+
82
+ # :nodoc:
83
+ def __stub_endpoint(endpoint_id, route_params = {})
84
+ endpoint = EndpointRegistry.instance.find!(endpoint_id)
85
+ uri = URI::Builder.build(endpoint.service_uri, endpoint.path, route_params)
86
+ endpoint_stub = WebMock::Builder.build(endpoint.verb, uri)
87
+
88
+ ::WebMock::StubRegistry.instance.register_request_stub(endpoint_stub)
47
89
  end
48
90
 
49
91
  #
@@ -74,29 +116,6 @@ module StubRequests
74
116
  DSL.new(service_id).print_stubs
75
117
  end
76
118
 
77
- #
78
- # Stub a request to a registered service endpoint
79
- #
80
- #
81
- # @param [Symbol] service_id the id of a registered service
82
- # @param [Symbol] endpoint_id the id of a registered endpoint
83
- # @param [Hash<Symbol>] route_params a map with route parameters
84
- #
85
- # @note the kind of timeout error raised by webmock is depending on the HTTP client used
86
- #
87
- # @example Stub a request to a registered service endpoint using block version
88
- # stub_endpoint(:documents, :index) do
89
- # with(headers: { "Accept" => "application/json" }}})
90
- # to_return(body: "No content", status: 204)
91
- # end
92
- #
93
- # @see #stub_http_request
94
- # @return [WebMock::RequestStub] a mocked request
95
- #
96
- def stub_endpoint(service_id, endpoint_id, route_params = {}, &callback)
97
- StubRequests::ServiceRegistry.stub_endpoint(service_id, endpoint_id, route_params, &callback)
98
- end
99
-
100
119
  #
101
120
  # Subscribe to notifications for a service endpoint
102
121
  #
@@ -108,7 +127,7 @@ module StubRequests
108
127
  # @return [void]
109
128
  #
110
129
  def register_callback(service_id, endpoint_id, verb, callback)
111
- StubRequests::CallbackRegistry.register(service_id, endpoint_id, verb, callback)
130
+ StubRequests::CallbackRegistry.instance.register(service_id, endpoint_id, verb, callback)
112
131
  end
113
132
 
114
133
  #
@@ -120,7 +139,7 @@ module StubRequests
120
139
  # @return [void]
121
140
  #
122
141
  def unregister_callback(service_id, endpoint_id, verb)
123
- StubRequests::CallbackRegistry.unregister(service_id, endpoint_id, verb)
142
+ StubRequests::CallbackRegistry.instance.unregister(service_id, endpoint_id, verb)
124
143
  end
125
144
  end
126
145
  end
@@ -14,7 +14,9 @@ module StubRequests
14
14
  # @since 0.1.3
15
15
  #
16
16
  class Callback
17
- include Property
17
+ # includes "Concerns::Property"
18
+ # @!parse include Concerns::Property
19
+ include Concerns::Property
18
20
  #
19
21
  # @!attribute [rw] service_id
20
22
  # @return [Symbol] the id of a service
@@ -14,52 +14,18 @@ module StubRequests
14
14
  # @since 0.1.3
15
15
  #
16
16
  class CallbackRegistry
17
+ # extend "Forwardable"
18
+ # @!parse extend Forwardable
19
+ extend Forwardable
20
+
21
+ # includes "Singleton"
22
+ # @!parse include Singleton
17
23
  include Singleton
24
+ # includes "Enumerable"
25
+ # @!parse include Enumerable
18
26
  include Enumerable
19
27
 
20
- #
21
- # Register to a service endpoint call
22
- # @see CallbackRegistry#register
23
- #
24
- #
25
- # @param [Symbol] service_id the id of a service
26
- # @param [Symbol] endpoint_id the id of an endpoint
27
- # @param [Symbol] verb the HTTP verb to subscribe to
28
- # @param [Proc] callback the callback to use for when.a request was made
29
- #
30
- # @return [Callback]
31
- #
32
- def self.register(service_id, endpoint_id, verb, callback)
33
- instance.register(service_id, endpoint_id, verb, callback)
34
- end
35
-
36
- #
37
- # Unregister from a service endpoint call
38
- # @see CallbackRegistry#unregister
39
- #
40
- #
41
- # @param [Symbol] service_id the id of a service
42
- # @param [Symbol] endpoint_id the id of an endpoint
43
- # @param [Symbol] verb the HTTP verb to subscribe to
44
- #
45
- # @return [Callback]
46
- #
47
- def self.unregister(service_id, endpoint_id, verb)
48
- instance.unregister(service_id, endpoint_id, verb)
49
- end
50
-
51
- #
52
- # Notifies subscribers that a request was made
53
- # @see CallbackRegistry#invoke_callbacks
54
- #
55
- #
56
- # @param [RequestStub] request the stubbed request
57
- #
58
- # @return [RequestStub]
59
- #
60
- def self.invoke_callbacks(request)
61
- instance.invoke_callbacks(request)
62
- end
28
+ delegate [:each] => :callbacks
63
29
 
64
30
  #
65
31
  # @!attribute [r] callbacks
@@ -83,18 +49,6 @@ module StubRequests
83
49
  callbacks.clear
84
50
  end
85
51
 
86
- #
87
- # Required by Enumerable
88
- #
89
- #
90
- # @return [Concurrent::Array<Callback>] a list with callbacks
91
- #
92
- # @yield used by Enumerable
93
- #
94
- def each(&block)
95
- callbacks.each(&block)
96
- end
97
-
98
52
  #
99
53
  # Register to a service endpoint call
100
54
  #
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Abstraction over WebMock to reduce duplication
5
+ #
6
+ # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @since 0.1.0
8
+ #
9
+ module StubRequests
10
+ module Concerns
11
+ #
12
+ # Module ArgumentValidation provides validation of method arguments
13
+ #
14
+ # @author Mikael Henriksson <mikael@zoolutions.se>
15
+ #
16
+ module ArgumentValidation
17
+ extend self
18
+
19
+ #
20
+ # Require the value to be any of the types past in
21
+ #
22
+ #
23
+ # @param [Symbol] name the name of the argument
24
+ # @param [Object] value the actual value of the argument
25
+ # @param [Array, Class, Module] type nil the expected argument value class
26
+ #
27
+ # @raise [InvalidArgumentType] when the value is disallowed
28
+ #
29
+ # @return [void]
30
+ #
31
+ def validate!(name:, value:, type:)
32
+ validate_type!(:name, name, [Symbol, String]) unless name
33
+ validate_type!(name, value, type) if type
34
+ end
35
+
36
+ def validate_type!(name, value, type)
37
+ expected_types = Array(type).flatten
38
+ return if expected_types.any? { |is_a| value.is_a?(is_a) }
39
+
40
+ raise StubRequests::InvalidArgumentType,
41
+ name: name,
42
+ actual: value.class,
43
+ expected: expected_types
44
+ end
45
+ end
46
+ end
47
+ end