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.
- checksums.yaml +4 -4
- data/.reek.yml +1 -0
- data/CHANGELOG.md +2 -0
- data/README.md +2 -2
- data/lib/stub_requests/api.rb +8 -22
- data/lib/stub_requests/callback.rb +58 -0
- data/lib/stub_requests/callback_registry.rb +188 -0
- data/lib/stub_requests/core_ext/all.rb +1 -0
- data/lib/stub_requests/core_ext/array/extract_options.rb +4 -6
- data/lib/stub_requests/core_ext/class/attribute.rb +2 -3
- data/lib/stub_requests/core_ext/kernel/singleton_class.rb +2 -3
- data/lib/stub_requests/core_ext/module/redefine_method.rb +6 -0
- data/lib/stub_requests/core_ext/object/blank.rb +21 -22
- data/lib/stub_requests/core_ext/string/to_route_param.rb +35 -0
- data/lib/stub_requests/dsl/define_method.rb +49 -0
- data/lib/stub_requests/dsl/method_definition.rb +72 -0
- data/lib/stub_requests/dsl.rb +94 -0
- data/lib/stub_requests/endpoint.rb +48 -36
- data/lib/stub_requests/endpoint_stub.rb +89 -0
- data/lib/stub_requests/endpoints.rb +147 -0
- data/lib/stub_requests/observable.rb +0 -44
- data/lib/stub_requests/request_stub.rb +80 -0
- data/lib/stub_requests/service.rb +77 -0
- data/lib/stub_requests/service_registry.rb +174 -0
- data/lib/stub_requests/stub_registry.rb +159 -0
- data/lib/stub_requests/uri/builder.rb +27 -34
- data/lib/stub_requests/uri.rb +31 -4
- data/lib/stub_requests/version.rb +1 -1
- data/lib/stub_requests/webmock/stub_registry_extension.rb +1 -1
- data/lib/stub_requests.rb +12 -12
- data/lib/tasks/changelog.rake +2 -1
- metadata +14 -16
- data/gemfiles/webmock_2.3.gemfile.lock +0 -206
- data/gemfiles/webmock_3.5.gemfile.lock +0 -206
- data/gemfiles/webmock_develop.gemfile.lock +0 -211
- data/lib/stub_requests/metrics/endpoint.rb +0 -98
- data/lib/stub_requests/metrics/registry.rb +0 -132
- data/lib/stub_requests/metrics/request.rb +0 -89
- data/lib/stub_requests/metrics.rb +0 -33
- data/lib/stub_requests/observable/registry.rb +0 -152
- data/lib/stub_requests/observable/subscription.rb +0 -58
- data/lib/stub_requests/registration/endpoint.rb +0 -107
- data/lib/stub_requests/registration/endpoints.rb +0 -156
- data/lib/stub_requests/registration/registry.rb +0 -112
- data/lib/stub_requests/registration/service.rb +0 -85
- data/lib/stub_requests/registration.rb +0 -87
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 981c31047c2aad42cd7c11962cb1429e742dfda6ed6656485a78d4b83f5acd78
|
4
|
+
data.tar.gz: 49b9c4cfd8fde9f4ab99429fcd5ae02b68809f98d47009b43b880f7802d09202
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 725506e6bfec1e4088bd8cbe436c33c285a0a0d7e7a9601cb6257d4f9375a321ba3ea663e5bcbf81a3d2dbdb5aac75b265676cbcbec220faaf6856706ec12de4
|
7
|
+
data.tar.gz: eafe65b7a4cec3296935ac42745480a486a1929c5f95a62b8ba9f438317f4ce7d6291724aaece80352e36728193a88cfe782d11651198c349ada37ef560bce7c
|
data/.reek.yml
CHANGED
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.
|
134
|
+
StubRequests.register(:document_service, :show, :get, callback)
|
135
135
|
```
|
136
136
|
|
137
137
|
```ruby
|
138
138
|
# To unsubscribe from notifications
|
139
|
-
StubRequests.
|
139
|
+
StubRequests.unregister(:document_service, :show, :get)
|
140
140
|
```
|
141
141
|
|
142
142
|
<a id="future-improvements"></a>
|
data/lib/stub_requests/api.rb
CHANGED
@@ -47,7 +47,7 @@ module StubRequests
|
|
47
47
|
#
|
48
48
|
# :reek:UtilityFunction
|
49
49
|
def register_service(service_id, service_uri, &block)
|
50
|
-
StubRequests::
|
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>]
|
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,
|
89
|
-
StubRequests::
|
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
|
105
|
-
StubRequests::
|
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
|
118
|
-
StubRequests::
|
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
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
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
|
-
#
|
13
|
-
# @api private
|
11
|
+
# :nodoc:
|
14
12
|
class Array
|
15
|
-
#
|
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
|
-
#
|
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
|
-
#
|
5
|
-
# @api private
|
4
|
+
# :nodoc:
|
6
5
|
class Class
|
7
|
-
#
|
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,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
|
-
#
|
8
|
-
# @api private
|
7
|
+
# :nodoc:
|
9
8
|
class Object
|
10
|
-
#
|
9
|
+
# :nodoc:
|
11
10
|
def blank?
|
12
11
|
respond_to?(:empty?) ? !!empty? : !self # rubocop:disable Style/DoubleNegation
|
13
12
|
end
|
14
|
-
#
|
13
|
+
# :nodoc:
|
15
14
|
def present?
|
16
15
|
!blank?
|
17
16
|
end
|
18
|
-
#
|
17
|
+
# :nodoc:
|
19
18
|
def presence
|
20
19
|
self if present?
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
24
23
|
# @see NilClass
|
25
|
-
#
|
24
|
+
# :nodoc:
|
26
25
|
class NilClass
|
27
|
-
#
|
26
|
+
# :nodoc:
|
28
27
|
def blank?
|
29
28
|
true
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
33
32
|
# @see FalseClass
|
34
|
-
#
|
33
|
+
# :nodoc:
|
35
34
|
class FalseClass
|
36
|
-
#
|
35
|
+
# :nodoc:
|
37
36
|
def blank?
|
38
37
|
true
|
39
38
|
end
|
40
39
|
end
|
41
40
|
|
42
41
|
# @see TrueClass
|
43
|
-
#
|
42
|
+
# :nodoc:
|
44
43
|
class TrueClass
|
45
|
-
#
|
44
|
+
# :nodoc:
|
46
45
|
def blank?
|
47
46
|
false
|
48
47
|
end
|
49
48
|
end
|
50
49
|
|
51
50
|
# @see Array
|
52
|
-
#
|
51
|
+
# :nodoc:
|
53
52
|
class Array
|
54
|
-
#
|
53
|
+
# :nodoc:
|
55
54
|
alias blank? empty?
|
56
55
|
end
|
57
56
|
|
58
57
|
# @see Hash
|
59
|
-
#
|
58
|
+
# :nodoc:
|
60
59
|
class Hash
|
61
|
-
#
|
60
|
+
# :nodoc:
|
62
61
|
alias blank? empty?
|
63
62
|
end
|
64
63
|
|
65
64
|
# @see String
|
66
65
|
class String
|
67
66
|
# :nodoc:
|
68
|
-
#
|
67
|
+
# :nodoc:
|
69
68
|
BLANK_RE = /\A[[:space:]]*\z/.freeze
|
70
69
|
# :nodoc:
|
71
|
-
#
|
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
|
-
#
|
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
|
-
#
|
98
|
+
# :nodoc:
|
100
99
|
class Numeric
|
101
|
-
#
|
100
|
+
# :nodoc:
|
102
101
|
def blank?
|
103
102
|
false
|
104
103
|
end
|
105
104
|
end
|
106
105
|
|
107
106
|
# @see Time
|
108
|
-
#
|
107
|
+
# :nodoc:
|
109
108
|
class Time
|
110
|
-
#
|
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
|