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.
- checksums.yaml +4 -4
- data/.reek.yml +5 -9
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +23 -0
- data/README.md +9 -9
- data/Rakefile +7 -5
- data/lib/stub_requests.rb +30 -16
- data/lib/stub_requests/api.rb +45 -26
- data/lib/stub_requests/callback.rb +3 -1
- data/lib/stub_requests/callback_registry.rb +9 -55
- data/lib/stub_requests/concerns/argument_validation.rb +47 -0
- data/lib/stub_requests/concerns/property.rb +114 -0
- data/lib/stub_requests/concerns/property/validator.rb +137 -0
- data/lib/stub_requests/concerns/register_verb.rb +110 -0
- data/lib/stub_requests/configuration.rb +19 -2
- data/lib/stub_requests/dsl.rb +5 -6
- data/lib/stub_requests/dsl/method_definition.rb +2 -8
- data/lib/stub_requests/endpoint.rb +22 -23
- data/lib/stub_requests/endpoint_registry.rb +157 -0
- data/lib/stub_requests/exceptions.rb +28 -10
- data/lib/stub_requests/request_stub.rb +29 -14
- data/lib/stub_requests/service.rb +55 -7
- data/lib/stub_requests/service_registry.rb +30 -79
- data/lib/stub_requests/stub_registry.rb +22 -80
- data/lib/stub_requests/stub_requests.rb +8 -5
- data/lib/stub_requests/uri.rb +0 -17
- data/lib/stub_requests/utils/fuzzy.rb +70 -0
- data/lib/stub_requests/version.rb +1 -1
- data/lib/stub_requests/webmock/builder.rb +9 -51
- data/lib/stub_requests/webmock/stub_registry_extension.rb +1 -1
- data/lib/tasks/changelog.rake +1 -7
- data/stub_requests.gemspec +1 -0
- data/update_docs.sh +2 -2
- metadata +27 -8
- data/lib/stub_requests/argument_validation.rb +0 -48
- data/lib/stub_requests/endpoint_stub.rb +0 -89
- data/lib/stub_requests/endpoints.rb +0 -246
- data/lib/stub_requests/hash_util.rb +0 -32
- data/lib/stub_requests/observable.rb +0 -18
- data/lib/stub_requests/property.rb +0 -112
- data/lib/stub_requests/property/validator.rb +0 -135
@@ -1,32 +0,0 @@
|
|
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
|
-
# Provides convenience methods for hashes
|
12
|
-
#
|
13
|
-
# @author Mikael Henriksson <mikael@zoolutions.se>
|
14
|
-
#
|
15
|
-
module HashUtil
|
16
|
-
#
|
17
|
-
# Removes all entries with nil values (first level only)
|
18
|
-
#
|
19
|
-
# @param [Hash] options the hash to compact
|
20
|
-
#
|
21
|
-
# @return [Hash, nil] Returns
|
22
|
-
#
|
23
|
-
# @yieldparam [Hash] compacted the hash without nils
|
24
|
-
# @yieldreturn [void]
|
25
|
-
def self.compact(options)
|
26
|
-
return if options.blank?
|
27
|
-
|
28
|
-
compacted = options.delete_if { |_, val| val.blank? }
|
29
|
-
yield compacted if compacted.present?
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,18 +0,0 @@
|
|
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
|
-
# Module Observable handles observing webmock requests
|
12
|
-
#
|
13
|
-
# @author Mikael Henriksson <mikael@zoolutions.se>
|
14
|
-
# @since 0.1.3
|
15
|
-
#
|
16
|
-
module Observable
|
17
|
-
end
|
18
|
-
end
|
@@ -1,112 +0,0 @@
|
|
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
|
-
# Module Property provides type checked attribute definition with default value
|
12
|
-
#
|
13
|
-
# @author Mikael Henriksson <mikael@zoolutions.se>
|
14
|
-
# @since 0.1.2
|
15
|
-
#
|
16
|
-
module Property
|
17
|
-
include ArgumentValidation
|
18
|
-
|
19
|
-
#
|
20
|
-
# Extends the base class with the ClassMethods module
|
21
|
-
#
|
22
|
-
# @param [Class,Module] base the class where this module is included
|
23
|
-
#
|
24
|
-
# @return [void]
|
25
|
-
#
|
26
|
-
def self.included(base)
|
27
|
-
base.class_attribute :properties, default: {}
|
28
|
-
base.send(:extend, ClassMethods)
|
29
|
-
end
|
30
|
-
|
31
|
-
#
|
32
|
-
# Module ClassMethods provides class methods for {Property}
|
33
|
-
#
|
34
|
-
# @author Mikael Henriksson <mikael@zoolutions.se>
|
35
|
-
#
|
36
|
-
module ClassMethods
|
37
|
-
#
|
38
|
-
# Define property methods for the name
|
39
|
-
#
|
40
|
-
# @param [Symbol] name the name of the property
|
41
|
-
# @param [Object] type the expected type of the property
|
42
|
-
# @param [Hash<Symbol>] options a hash with options
|
43
|
-
# @option options [Object] :default a default value for the property
|
44
|
-
#
|
45
|
-
# @return [void]
|
46
|
-
#
|
47
|
-
def property(name, type:, **options)
|
48
|
-
type = normalize_type(type, options)
|
49
|
-
default = options[:default]
|
50
|
-
Property::Validator.call(name, type, default, properties)
|
51
|
-
|
52
|
-
Docile.dsl_eval(self) do
|
53
|
-
define_property(name, type, default)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# @api private
|
58
|
-
def normalize_type(type, **options)
|
59
|
-
type_array = Array(type)
|
60
|
-
return type_array unless (default = options[:default])
|
61
|
-
|
62
|
-
type_array.concat([default.class]).flatten.uniq
|
63
|
-
end
|
64
|
-
|
65
|
-
# @api private
|
66
|
-
def define_property(name, type, default)
|
67
|
-
property_reader(name, default)
|
68
|
-
property_predicate(name)
|
69
|
-
property_writer(name, type)
|
70
|
-
|
71
|
-
set_property_default(name, default)
|
72
|
-
set_property_defined(name, type, default)
|
73
|
-
end
|
74
|
-
|
75
|
-
# @api private
|
76
|
-
def property_reader(name, default)
|
77
|
-
invar = "@#{name}"
|
78
|
-
silence_redefinition_of_method(name.to_s)
|
79
|
-
redefine_method(name) do
|
80
|
-
instance_variable_set(invar, default) unless instance_variable_defined?(invar)
|
81
|
-
instance_variable_get(invar)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
# @api private
|
86
|
-
def property_predicate(name)
|
87
|
-
silence_redefinition_of_method("#{name}?")
|
88
|
-
redefine_method("#{name}?") do
|
89
|
-
!!public_send(name) # rubocop:disable Style/DoubleNegation
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
# @api private
|
94
|
-
def property_writer(name, type)
|
95
|
-
redefine_method("#{name}=") do |obj|
|
96
|
-
validate! name: name, value: obj, type: type
|
97
|
-
instance_variable_set("@#{name}", obj)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def set_property_default(name, default)
|
102
|
-
instance_variable_set("@#{name}", default)
|
103
|
-
end
|
104
|
-
|
105
|
-
# @api private
|
106
|
-
def set_property_defined(name, type, default)
|
107
|
-
self.properties ||= {}
|
108
|
-
properties[name] = { type: type, default: default }
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
@@ -1,135 +0,0 @@
|
|
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
|
-
# Module Property provides type checked attribute definition with default value
|
12
|
-
#
|
13
|
-
# @author Mikael Henriksson <mikael@zoolutions.se>
|
14
|
-
# @since 0.1.2
|
15
|
-
#
|
16
|
-
module Property
|
17
|
-
#
|
18
|
-
# Class Validator provides validation for adding properties
|
19
|
-
#
|
20
|
-
# @author Mikael Henriksson <mikael@zoolutions.se>
|
21
|
-
# @since 0.1.2
|
22
|
-
#
|
23
|
-
class Validator
|
24
|
-
include ArgumentValidation
|
25
|
-
|
26
|
-
#
|
27
|
-
# Validates that the property can be added to the class
|
28
|
-
#
|
29
|
-
#
|
30
|
-
# @param [Symbol] name the name of the property
|
31
|
-
# @param [Class, Module] type the type of the property
|
32
|
-
# @param [Object] default the default value of the property
|
33
|
-
# @param [Hash] properties the list of currently defined properties
|
34
|
-
#
|
35
|
-
# @raise [InvalidArgumentType] when name is not a Symbol
|
36
|
-
# @raise [InvalidArgumentType] when default does not match type
|
37
|
-
# @raise [PropertyDefined] when property has already been defined
|
38
|
-
#
|
39
|
-
# @return [void]
|
40
|
-
#
|
41
|
-
def self.call(name, type, default, properties)
|
42
|
-
new(name, type, default, properties).run_validations
|
43
|
-
end
|
44
|
-
|
45
|
-
#
|
46
|
-
# @!attribute [r] name
|
47
|
-
# @return [Symbol] the name of the property
|
48
|
-
attr_reader :name
|
49
|
-
#
|
50
|
-
# @!attribute [r] type
|
51
|
-
# @return [Class, Module] the type of the property
|
52
|
-
attr_reader :type
|
53
|
-
#
|
54
|
-
# @!attribute [r] default
|
55
|
-
# @return [Object] the default value of the property
|
56
|
-
attr_reader :default
|
57
|
-
#
|
58
|
-
# @!attribute [r] properties
|
59
|
-
# @return [Hash] the list of currently defined properties
|
60
|
-
attr_reader :properties
|
61
|
-
|
62
|
-
# Initializes a new {Validator}
|
63
|
-
#
|
64
|
-
# @param [Symbol] name the name of the property
|
65
|
-
# @param [Class, Module] type the type of the property
|
66
|
-
# @param [Object] default the default value of the property
|
67
|
-
# @param [Hash] properties the list of currently defined properties
|
68
|
-
#
|
69
|
-
def initialize(name, type, default = nil, properties = {})
|
70
|
-
@type = Array(type).flatten
|
71
|
-
@default = default
|
72
|
-
@name = name
|
73
|
-
@properties = properties || {}
|
74
|
-
end
|
75
|
-
|
76
|
-
#
|
77
|
-
# Performs all validations
|
78
|
-
#
|
79
|
-
#
|
80
|
-
# @raise [InvalidArgumentType] when name is not a Symbol
|
81
|
-
# @raise [InvalidArgumentType] when default does not match type
|
82
|
-
# @raise [PropertyDefined] when property has already been defined
|
83
|
-
#
|
84
|
-
# @return [void]
|
85
|
-
#
|
86
|
-
def run_validations
|
87
|
-
validate_undefined
|
88
|
-
validate_name
|
89
|
-
validate_default
|
90
|
-
end
|
91
|
-
|
92
|
-
private
|
93
|
-
|
94
|
-
#
|
95
|
-
# Validates that the name is of type Symbol
|
96
|
-
#
|
97
|
-
# @raise [InvalidArgumentType] when name is not a Symbol
|
98
|
-
#
|
99
|
-
# @return [void]
|
100
|
-
#
|
101
|
-
def validate_name
|
102
|
-
validate! name: :name, value: name, type: Symbol
|
103
|
-
end
|
104
|
-
|
105
|
-
#
|
106
|
-
# Validate that the default value matches the type
|
107
|
-
#
|
108
|
-
#
|
109
|
-
# @raise [InvalidArgumentType] when default does not match type
|
110
|
-
#
|
111
|
-
# @return [void]
|
112
|
-
#
|
113
|
-
def validate_default
|
114
|
-
return unless default || default.is_a?(FalseClass)
|
115
|
-
|
116
|
-
validate! name: :default, value: default, type: type
|
117
|
-
end
|
118
|
-
|
119
|
-
#
|
120
|
-
# Validate that the property has not been defined
|
121
|
-
#
|
122
|
-
#
|
123
|
-
# @raise [PropertyDefined] when property has already been defined
|
124
|
-
#
|
125
|
-
# @return [void]
|
126
|
-
#
|
127
|
-
def validate_undefined
|
128
|
-
return unless properties
|
129
|
-
return unless (prop = properties[name])
|
130
|
-
|
131
|
-
raise PropertyDefined, name: name, type: prop[:type], default: prop[:default]
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|