stub_requests 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.reek.yml +1 -0
  3. data/CHANGELOG.md +2 -0
  4. data/README.md +2 -2
  5. data/lib/stub_requests/api.rb +8 -22
  6. data/lib/stub_requests/callback.rb +58 -0
  7. data/lib/stub_requests/callback_registry.rb +188 -0
  8. data/lib/stub_requests/core_ext/all.rb +1 -0
  9. data/lib/stub_requests/core_ext/array/extract_options.rb +4 -6
  10. data/lib/stub_requests/core_ext/class/attribute.rb +2 -3
  11. data/lib/stub_requests/core_ext/kernel/singleton_class.rb +2 -3
  12. data/lib/stub_requests/core_ext/module/redefine_method.rb +6 -0
  13. data/lib/stub_requests/core_ext/object/blank.rb +21 -22
  14. data/lib/stub_requests/core_ext/string/to_route_param.rb +35 -0
  15. data/lib/stub_requests/dsl/define_method.rb +49 -0
  16. data/lib/stub_requests/dsl/method_definition.rb +72 -0
  17. data/lib/stub_requests/dsl.rb +94 -0
  18. data/lib/stub_requests/endpoint.rb +48 -36
  19. data/lib/stub_requests/endpoint_stub.rb +89 -0
  20. data/lib/stub_requests/endpoints.rb +147 -0
  21. data/lib/stub_requests/observable.rb +0 -44
  22. data/lib/stub_requests/request_stub.rb +80 -0
  23. data/lib/stub_requests/service.rb +77 -0
  24. data/lib/stub_requests/service_registry.rb +174 -0
  25. data/lib/stub_requests/stub_registry.rb +159 -0
  26. data/lib/stub_requests/uri/builder.rb +27 -34
  27. data/lib/stub_requests/uri.rb +31 -4
  28. data/lib/stub_requests/version.rb +1 -1
  29. data/lib/stub_requests/webmock/stub_registry_extension.rb +1 -1
  30. data/lib/stub_requests.rb +12 -12
  31. data/lib/tasks/changelog.rake +2 -1
  32. metadata +14 -16
  33. data/gemfiles/webmock_2.3.gemfile.lock +0 -206
  34. data/gemfiles/webmock_3.5.gemfile.lock +0 -206
  35. data/gemfiles/webmock_develop.gemfile.lock +0 -211
  36. data/lib/stub_requests/metrics/endpoint.rb +0 -98
  37. data/lib/stub_requests/metrics/registry.rb +0 -132
  38. data/lib/stub_requests/metrics/request.rb +0 -89
  39. data/lib/stub_requests/metrics.rb +0 -33
  40. data/lib/stub_requests/observable/registry.rb +0 -152
  41. data/lib/stub_requests/observable/subscription.rb +0 -58
  42. data/lib/stub_requests/registration/endpoint.rb +0 -107
  43. data/lib/stub_requests/registration/endpoints.rb +0 -156
  44. data/lib/stub_requests/registration/registry.rb +0 -112
  45. data/lib/stub_requests/registration/service.rb +0 -85
  46. data/lib/stub_requests/registration.rb +0 -87
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be0196cbd37c83eff04abe21413a0bded52b335db5437e6c31eaf79b7484d81b
4
- data.tar.gz: 2c4d16e0b0d23d285a7b2532cbb6d3f3507348c05f473bacdfe52dee352ff1e2
3
+ metadata.gz: 981c31047c2aad42cd7c11962cb1429e742dfda6ed6656485a78d4b83f5acd78
4
+ data.tar.gz: 49b9c4cfd8fde9f4ab99429fcd5ae02b68809f98d47009b43b880f7802d09202
5
5
  SHA512:
6
- metadata.gz: dd5f10556ecfb6aef179cf59dcfaf5938eadbda4c7ae64f0556ffd2502caf434e0747e1f3b8e7ed73a7e99a2636f23fb825967c5dea919ccc486ae8156da2973
7
- data.tar.gz: 78e59cb1fbe7ad99282f0162ab0cdb7cbe97a77647dd5be0055edc6e106ac132fd48cd6fe92a764551719e541c114949081b1ed8c6e2b91a92be990f75b10358
6
+ metadata.gz: 725506e6bfec1e4088bd8cbe436c33c285a0a0d7e7a9601cb6257d4f9375a321ba3ea663e5bcbf81a3d2dbdb5aac75b265676cbcbec220faaf6856706ec12de4
7
+ data.tar.gz: eafe65b7a4cec3296935ac42745480a486a1929c5f95a62b8ba9f438317f4ce7d6291724aaece80352e36728193a88cfe782d11651198c349ada37ef560bce7c
data/.reek.yml CHANGED
@@ -12,3 +12,4 @@ exclude_paths:
12
12
  - vendor/bundle
13
13
  - gemfiles
14
14
  - lib/stub_requests/core_ext/
15
+ - spec/
data/CHANGELOG.md CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  **Implemented enhancements:**
8
8
 
9
+ - Add rough initial implementation of subscriptions [\#12](https://github.com/mhenrixon/stub_requests/pull/12) ([mhenrixon](https://github.com/mhenrixon))
9
10
  - Script updating documentation [\#9](https://github.com/mhenrixon/stub_requests/pull/9) ([mhenrixon](https://github.com/mhenrixon))
10
11
  - Add a simple script to update documentation [\#8](https://github.com/mhenrixon/stub_requests/pull/8) ([mhenrixon](https://github.com/mhenrixon))
11
12
  - Improve documentation [\#7](https://github.com/mhenrixon/stub_requests/pull/7) ([mhenrixon](https://github.com/mhenrixon))
@@ -14,6 +15,7 @@
14
15
 
15
16
  **Merged pull requests:**
16
17
 
18
+ - Update Changelog [\#11](https://github.com/mhenrixon/stub_requests/pull/11) ([mhenrixon](https://github.com/mhenrixon))
17
19
  - Allow older webmock versions [\#4](https://github.com/mhenrixon/stub_requests/pull/4) ([mhenrixon](https://github.com/mhenrixon))
18
20
  - Add docs folder [\#3](https://github.com/mhenrixon/stub_requests/pull/3) ([mhenrixon](https://github.com/mhenrixon))
19
21
  - Add docs folder and CHANGELOG.md [\#2](https://github.com/mhenrixon/stub_requests/pull/2) ([mhenrixon](https://github.com/mhenrixon))
data/README.md CHANGED
@@ -131,12 +131,12 @@ end
131
131
 
132
132
  callback = ->(request) { p request; binding.pry }
133
133
 
134
- StubRequests.subscribe_to(:document_service, :show, :get, callback)
134
+ StubRequests.register(:document_service, :show, :get, callback)
135
135
  ```
136
136
 
137
137
  ```ruby
138
138
  # To unsubscribe from notifications
139
- StubRequests.unsubscribe_from(:document_service, :show, :get)
139
+ StubRequests.unregister(:document_service, :show, :get)
140
140
  ```
141
141
 
142
142
  <a id="future-improvements"></a>
@@ -47,7 +47,7 @@ module StubRequests
47
47
  #
48
48
  # :reek:UtilityFunction
49
49
  def register_service(service_id, service_uri, &block)
50
- StubRequests::Registration.register_service(service_id, service_uri, &block)
50
+ StubRequests::ServiceRegistry.register_service(service_id, service_uri, &block)
51
51
  end
52
52
 
53
53
  #
@@ -56,24 +56,10 @@ module StubRequests
56
56
  #
57
57
  # @param [Symbol] service_id the id of a registered service
58
58
  # @param [Symbol] endpoint_id the id of a registered endpoint
59
- # @param [Hash<Symbol>] uri_replacements a list of URI replacements
60
- # @param [Hash<Symbol>] options
61
- # @option options [optional, Hash<Symbol>] :request webmock request options
62
- # @option options [optional, Hash<Symbol>] :response webmock response options
63
- # @option options [optional, Array, Exception, StandardError, String] :error webmock error to raise
64
- # @option options [optional, TrueClass] :timeout set to true to raise some kind of timeout error
59
+ # @param [Hash<Symbol>] route_params a map with route parameters
65
60
  #
66
61
  # @note the kind of timeout error raised by webmock is depending on the HTTP client used
67
62
  #
68
- # @example Stub a request to a registered service endpoint
69
- # register_stub(
70
- # :google_api,
71
- # :get_map_location,
72
- # {}, # No URI replacements needed for this endpoint
73
- # { request: { headers: { "Accept" => "application/json" }}},
74
- # { response: { body: { id: "abyasdjasd", status: "successful" }}}
75
- # )
76
- #
77
63
  # @example Stub a request to a registered service endpoint using block version
78
64
  # register_stub(:documents, :index) do
79
65
  # with(headers: { "Accept" => "application/json" }}})
@@ -85,8 +71,8 @@ module StubRequests
85
71
  #
86
72
  # :reek:UtilityFunction
87
73
  # :reek:LongParameterList { max_params: 5 }
88
- def stub_endpoint(service_id, endpoint_id, uri_replacements = {}, options = {}, &callback)
89
- StubRequests::Registration.stub_endpoint(service_id, endpoint_id, uri_replacements, options, &callback)
74
+ def stub_endpoint(service_id, endpoint_id, route_params = {}, &callback)
75
+ StubRequests::ServiceRegistry.stub_endpoint(service_id, endpoint_id, route_params, &callback)
90
76
  end
91
77
 
92
78
  #
@@ -101,8 +87,8 @@ module StubRequests
101
87
  #
102
88
  # :reek:UtilityFunction
103
89
  # :reek:LongParameterList
104
- def subscribe_to(service_id, endpoint_id, verb, callback)
105
- StubRequests::Observable.subscribe_to(service_id, endpoint_id, verb, callback)
90
+ def register_callback(service_id, endpoint_id, verb, callback)
91
+ StubRequests::CallbackRegistry.register(service_id, endpoint_id, verb, callback)
106
92
  end
107
93
 
108
94
  #
@@ -114,8 +100,8 @@ module StubRequests
114
100
  # @return [void]
115
101
  #
116
102
  # :reek:UtilityFunction
117
- def unsubscribe_from(service_id, endpoint_id, verb)
118
- StubRequests::Observable.unsubscribe_from(service_id, endpoint_id, verb)
103
+ def unregister_callback(service_id, endpoint_id, verb)
104
+ StubRequests::CallbackRegistry.unregister(service_id, endpoint_id, verb)
119
105
  end
120
106
  end
121
107
  end
@@ -0,0 +1,58 @@
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
+ #
11
+ # Class Callback contains information about a subscription
12
+ #
13
+ # @author Mikael Henriksson <mikael@zoolutions.se>
14
+ # @since 0.1.3
15
+ #
16
+ class Callback
17
+ include Property
18
+ #
19
+ # @!attribute [rw] service_id
20
+ # @return [Symbol] the id of a service
21
+ property :service_id, type: Symbol
22
+ #
23
+ # @!attribute [rw] endpoint_id
24
+ # @return [Symbol] the id of an endpoint
25
+ property :endpoint_id, type: Symbol
26
+ #
27
+ # @!attribute [rw] verb
28
+ # @return [Symbol] the HTTP verb/method
29
+ property :verb, type: Symbol, default: :any
30
+ #
31
+ # @!attribute [rw] callback
32
+ # @return [Proc] a proc to callback on notify
33
+ property :callback, type: Proc
34
+
35
+ #
36
+ # Initialize a new Callback
37
+ #
38
+ # @param [Symbol] service_id the id of a service
39
+ # @param [Symbol] endpoint_id the id of an endpoint
40
+ # @param [Symbol] verb the HTTP verb/method
41
+ # @param [Proc] callback a proc to callback on notify
42
+ #
43
+ def initialize(service_id, endpoint_id, verb, callback)
44
+ self.service_id = service_id
45
+ self.endpoint_id = endpoint_id
46
+ self.verb = verb
47
+ self.callback = callback
48
+ end
49
+
50
+ def call(*args)
51
+ callback.call(*args)
52
+ end
53
+
54
+ def arity
55
+ callback.arity
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,188 @@
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
+ #
11
+ # Class Registry handles callbacks to webmock requests
12
+ #
13
+ # @author Mikael Henriksson <mikael@zoolutions.se>
14
+ # @since 0.1.3
15
+ #
16
+ # :reek:UtilityFunction
17
+ # :reek:DataClump
18
+ # :reek:FeatureEnvy
19
+ class CallbackRegistry
20
+ include Singleton
21
+ include Enumerable
22
+
23
+ #
24
+ # Register to a service endpoint call
25
+ # @see CallbackRegistry#register
26
+ #
27
+ #
28
+ # @param [Symbol] service_id the id of a service
29
+ # @param [Symbol] endpoint_id the id of an endpoint
30
+ # @param [Symbol] verb the HTTP verb to subscribe to
31
+ # @param [Proc] callback the callback to use for when.a request was made
32
+ #
33
+ # @return [Callback]
34
+ #
35
+ # :reek:LongParameterList
36
+ def self.register(service_id, endpoint_id, verb, callback)
37
+ instance.register(service_id, endpoint_id, verb, callback)
38
+ end
39
+
40
+ #
41
+ # Unregister from a service endpoint call
42
+ # @see CallbackRegistry#unregister
43
+ #
44
+ #
45
+ # @param [Symbol] service_id the id of a service
46
+ # @param [Symbol] endpoint_id the id of an endpoint
47
+ # @param [Symbol] verb the HTTP verb to subscribe to
48
+ #
49
+ # @return [Callback]
50
+ #
51
+ def self.unregister(service_id, endpoint_id, verb)
52
+ instance.unregister(service_id, endpoint_id, verb)
53
+ end
54
+
55
+ #
56
+ # Notifies subscribers that a request was made
57
+ # @see CallbackRegistry#invoke_callbacks
58
+ #
59
+ #
60
+ # @param [RequestStub] request the stubbed request
61
+ #
62
+ # @return [RequestStub]
63
+ #
64
+ def self.invoke_callbacks(request)
65
+ instance.invoke_callbacks(request)
66
+ end
67
+
68
+ #
69
+ # @!attribute [r] callbacks
70
+ # @return [Concurrent::Array<Callback>] a list of callbacks
71
+ attr_reader :callbacks
72
+
73
+ #
74
+ # Used by Singleton
75
+ #
76
+ #
77
+ def initialize
78
+ @callbacks = Concurrent::Array.new
79
+ end
80
+
81
+ #
82
+ # Resets the map with registered services
83
+ #
84
+ #
85
+ # @api private
86
+ def reset
87
+ callbacks.clear
88
+ end
89
+
90
+ #
91
+ # Required by Enumerable
92
+ #
93
+ #
94
+ # @return [Concurrent::Array<Callback>] a list with callbacks
95
+ #
96
+ # @yield used by Enumerable
97
+ #
98
+ def each(&block)
99
+ callbacks.each(&block)
100
+ end
101
+
102
+ #
103
+ # Register to a service endpoint call
104
+ #
105
+ #
106
+ # @param [Symbol] service_id the id of a service
107
+ # @param [Symbol] endpoint_id the id of an endpoint
108
+ # @param [optional, Symbol] verb the HTTP verb to subscribe to
109
+ # @param [proc] block the callback to use for when.a request was made
110
+ #
111
+ # @return [Callback] the added callback
112
+ #
113
+ # :reek:LongParameterList
114
+ def register(service_id, endpoint_id, verb, block)
115
+ callback = find_by(service_id, endpoint_id, verb)
116
+ return callback if callback
117
+
118
+ callback = Callback.new(service_id, endpoint_id, verb, block)
119
+ callbacks.push(callback)
120
+ callback
121
+ end
122
+
123
+ #
124
+ # Unregister to a service endpoint call
125
+ #
126
+ #
127
+ # @param [Symbol] service_id the id of a service
128
+ # @param [Symbol] endpoint_id the id of an endpoint
129
+ # @param [optional, Symbol] verb the HTTP verb to subscribe to
130
+ #
131
+ # @return [Callback] the deleted callback
132
+ #
133
+ # :reek:ControlParameter
134
+ def unregister(service_id, endpoint_id, verb)
135
+ return unless (callback = find_by(service_id, endpoint_id, verb))
136
+
137
+ callbacks.delete(callback)
138
+ end
139
+
140
+ #
141
+ # Notifies subscribers that a request was made
142
+ #
143
+ # @param [RequestStub] request the stubbed request
144
+ #
145
+ # @return [void]
146
+ #
147
+ def invoke_callbacks(request)
148
+ return unless (callback = find_by(request.service_id, request.endpoint_id, request.verb))
149
+
150
+ dispatch_callback(request, callback)
151
+ end
152
+
153
+ private
154
+
155
+ #
156
+ # Finds a callback for a service endpoint
157
+ #
158
+ #
159
+ # @param [Symbol] service_id the id of a service
160
+ # @param [Symbol] endpoint_id the id of an endpoint
161
+ # @param [optional, Symbol] verb the HTTP verb to subscribe to
162
+ #
163
+ # @return [Callback]
164
+ #
165
+ # :reek:ControlParameter
166
+ # :reek:DuplicateMethodCall
167
+ def find_by(service_id, endpoint_id, verb)
168
+ find do |sub|
169
+ sub.service_id == service_id &&
170
+ sub.endpoint_id == endpoint_id &&
171
+ ([sub.verb, verb].include?(:any) || sub.verb == verb)
172
+ end
173
+ end
174
+
175
+ def dispatch_callback(request_stub, callback)
176
+ arity = callback.arity
177
+
178
+ case arity
179
+ when 0
180
+ callback.call
181
+ when 1
182
+ callback.call(request_stub)
183
+ else
184
+ raise InvalidCallback, "The callback for a callback can either take 0 or 1 arguments (was #{arity})"
185
+ end
186
+ end
187
+ end
188
+ end
@@ -5,3 +5,4 @@ require "stub_requests/core_ext/kernel/singleton_class"
5
5
  require "stub_requests/core_ext/module/redefine_method"
6
6
  require "stub_requests/core_ext/class/attribute"
7
7
  require "stub_requests/core_ext/object/blank"
8
+ require "stub_requests/core_ext/string/to_route_param"
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # @see Hash
4
- # @api private
3
+ # :nodoc:
5
4
  class Hash
6
5
  # @api private
7
6
  def extractable_options?
@@ -9,10 +8,9 @@ class Hash
9
8
  end
10
9
  end
11
10
 
12
- # @see Array
13
- # @api private
11
+ # :nodoc:
14
12
  class Array
15
- # @api private
13
+ # :nodoc:
16
14
  def extract_options!
17
15
  if last.is_a?(Hash) && last.extractable_options?
18
16
  pop
@@ -21,7 +19,7 @@ class Array
21
19
  end
22
20
  end
23
21
 
24
- # @api private
22
+ # :nodoc:
25
23
  def extract_options
26
24
  if last.is_a?(Hash) && last.extractable_options?
27
25
  last
@@ -1,10 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  unless defined?(Rails) || defined?(ActiveSupport)
4
- # See {Class}
5
- # @api private
4
+ # :nodoc:
6
5
  class Class
7
- # @api private
6
+ # :nodoc:
8
7
  def class_attribute(*attrs)
9
8
  options = attrs.extract_options!
10
9
  default_value = options.fetch(:default, nil)
@@ -1,10 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  unless defined?(Rails) || defined?(ActiveSupport)
4
- # See {Class}
5
- # @api private
4
+ # :nodoc:
6
5
  module Kernel
7
- # @api private
6
+ # :nodoc:
8
7
  def class_eval(*args, &block)
9
8
  singleton_class.class_eval(*args, &block)
10
9
  end
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :nodoc:
4
+
3
5
  unless defined?(Rails) || defined?(ActiveSupport)
4
6
  # See {Module}
5
7
  # @api private
6
8
  class Module
7
9
  # @api private
10
+ # :nodoc:
8
11
  def silence_redefinition_of_method(method)
9
12
  if method_defined?(method) || private_method_defined?(method)
10
13
  alias_method :__rails_redefine, method
@@ -15,6 +18,7 @@ unless defined?(Rails) || defined?(ActiveSupport)
15
18
  # Replaces the existing method definition, if there is one, with the passed
16
19
  # block as its body.
17
20
  # @api private
21
+ # :nodoc:
18
22
  def redefine_method(method, &block)
19
23
  visibility = method_visibility(method)
20
24
  silence_redefinition_of_method(method)
@@ -25,11 +29,13 @@ unless defined?(Rails) || defined?(ActiveSupport)
25
29
  # Replaces the existing singleton method definition, if there is one, with
26
30
  # the passed block as its body.
27
31
  # @api private
32
+ # :nodoc:
28
33
  def redefine_singleton_method(method, &block)
29
34
  singleton_class.redefine_method(method, &block)
30
35
  end
31
36
 
32
37
  # @api private
38
+ # :nodoc:
33
39
  def method_visibility(method) # :nodoc:
34
40
  case
35
41
  when private_method_defined?(method)
@@ -4,76 +4,75 @@
4
4
 
5
5
  unless defined?(Rails) || defined?(ActiveSupport)
6
6
 
7
- # @see Object
8
- # @api private
7
+ # :nodoc:
9
8
  class Object
10
- # @api private
9
+ # :nodoc:
11
10
  def blank?
12
11
  respond_to?(:empty?) ? !!empty? : !self # rubocop:disable Style/DoubleNegation
13
12
  end
14
- # @api private
13
+ # :nodoc:
15
14
  def present?
16
15
  !blank?
17
16
  end
18
- # @api private
17
+ # :nodoc:
19
18
  def presence
20
19
  self if present?
21
20
  end
22
21
  end
23
22
 
24
23
  # @see NilClass
25
- # @api private
24
+ # :nodoc:
26
25
  class NilClass
27
- # @api private
26
+ # :nodoc:
28
27
  def blank?
29
28
  true
30
29
  end
31
30
  end
32
31
 
33
32
  # @see FalseClass
34
- # @api private
33
+ # :nodoc:
35
34
  class FalseClass
36
- # @api private
35
+ # :nodoc:
37
36
  def blank?
38
37
  true
39
38
  end
40
39
  end
41
40
 
42
41
  # @see TrueClass
43
- # @api private
42
+ # :nodoc:
44
43
  class TrueClass
45
- # @api private
44
+ # :nodoc:
46
45
  def blank?
47
46
  false
48
47
  end
49
48
  end
50
49
 
51
50
  # @see Array
52
- # @api private
51
+ # :nodoc:
53
52
  class Array
54
- # @api private
53
+ # :nodoc:
55
54
  alias blank? empty?
56
55
  end
57
56
 
58
57
  # @see Hash
59
- # @api private
58
+ # :nodoc:
60
59
  class Hash
61
- # @api private
60
+ # :nodoc:
62
61
  alias blank? empty?
63
62
  end
64
63
 
65
64
  # @see String
66
65
  class String
67
66
  # :nodoc:
68
- # @api private
67
+ # :nodoc:
69
68
  BLANK_RE = /\A[[:space:]]*\z/.freeze
70
69
  # :nodoc:
71
- # @api private
70
+ # :nodoc:
72
71
  ENCODED_BLANKS = Concurrent::Map.new do |map, enc|
73
72
  map[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING)
74
73
  end
75
74
 
76
- # @api private
75
+ # :nodoc:
77
76
  def blank?
78
77
  # The regexp that matches blank strings is expensive. For the case of empty
79
78
  # strings we can speed up this method (~3.5x) with an empty? call. The
@@ -96,18 +95,18 @@ unless defined?(Rails) || defined?(ActiveSupport)
96
95
  end
97
96
 
98
97
  # @see Numeric
99
- # @api private
98
+ # :nodoc:
100
99
  class Numeric
101
- # @api private
100
+ # :nodoc:
102
101
  def blank?
103
102
  false
104
103
  end
105
104
  end
106
105
 
107
106
  # @see Time
108
- # @api private
107
+ # :nodoc:
109
108
  class Time
110
- # @api private
109
+ # :nodoc:
111
110
  def blank?
112
111
  false
113
112
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ # @api private
5
+ class String
6
+ # :nodoc:
7
+ # @api private
8
+ def to_route_param
9
+ return self if start_with?(":")
10
+
11
+ ":#{+self}"
12
+ end unless method_defined?(:to_route_param)
13
+ end
14
+
15
+ # :nodoc:
16
+ # @api private
17
+ class Symbol
18
+ # :nodoc:
19
+ # @api private
20
+ def to_route_param
21
+ to_s.to_route_param
22
+ end unless method_defined?(:to_route_param)
23
+ end
24
+
25
+ # :nodoc:
26
+ # @api private
27
+ class Hash
28
+ # :nodoc:
29
+ # @api private
30
+ def to_route_param
31
+ each_with_object({}) do |(key, value), memo|
32
+ memo[key.to_route_param] = value
33
+ end
34
+ end unless method_defined?(:to_route_param)
35
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StubRequests
4
+ class DSL
5
+ #
6
+ # Class DefineMethod defines helper methods for stubbed endpoints
7
+ #
8
+ # @author Mikael Henriksson <mikael@zoolutions.se>
9
+ # @since 0.1.4
10
+ #
11
+ class DefineMethod
12
+ #
13
+ # @!attribute [r] definition
14
+ # @return [MethodDefinition] the method definition
15
+ attr_reader :definition
16
+ #
17
+ # @!attribute [r] receiver
18
+ # @return [Module] the receiver of the method definition
19
+ attr_reader :receiver
20
+
21
+ #
22
+ # Initialize a new instance of DefineMethod
23
+ #
24
+ #
25
+ # @param [MethodDefinition] definition the method definition
26
+ # @param [Module] receiver the receiver of the method definition
27
+ #
28
+ def initialize(definition, receiver)
29
+ @receiver = receiver
30
+ @definition = definition
31
+ end
32
+
33
+ #
34
+ # Define the {MethodDefinition#to_s} on the receiver
35
+ #
36
+ #
37
+ # @return [void]
38
+ #
39
+ def define
40
+ Docile.dsl_eval(receiver) do
41
+ silence_redefinition_of_method(definition.name)
42
+ module_eval <<~METHOD, __FILE__, __LINE__ + 1
43
+ #{definition}
44
+ METHOD
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end