Class: ServiceContractWebmock::ContractMatcher
- Inherits:
-
Object
- Object
- ServiceContractWebmock::ContractMatcher
- Defined in:
- lib/service_contract_webmock/contract_matcher.rb
Constant Summary collapse
- INT =
Avro::Schema::PrimitiveSchema.new(:int)
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
- #extract_request(query) ⇒ Object
- #field_int?(name) ⇒ Boolean
- #fields ⇒ Object
- #fields_and_pagination ⇒ Object
- #found(query) ⇒ Object
-
#initialize(endpoint, resources) ⇒ ContractMatcher
constructor
A new instance of ContractMatcher.
- #pagination_fields ⇒ Object
- #to_regex ⇒ Object
Constructor Details
#initialize(endpoint, resources) ⇒ ContractMatcher
Returns a new instance of ContractMatcher.
8 9 10 11 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 8 def initialize(endpoint, resources) @endpoint = endpoint @resources = resources end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
6 7 8 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 6 def endpoint @endpoint end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
6 7 8 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 6 def resources @resources end |
Instance Method Details
#extract_request(query) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 48 def extract_request(query) params = CGI.parse(query) fields.select {|field| field.name.in?(params.keys)}.each_with_object({}) do |field, acc| data = params[field.name][0].split(',').flat_map {|value| field.convert(value)} acc[field.name] = data end end |
#field_int?(name) ⇒ Boolean
44 45 46 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 44 def field_int?(name) fields.detect {|f| f.name == name}.try(:int?) end |
#fields ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 30 def fields @fields ||= endpoint.parameters.map do |param| if param.type.definition.is_a?(Avro::Schema::PrimitiveSchema) Field.new(param.name, param.type.definition) elsif param.definition.type.is_a?(Avro::Schema::PrimitiveSchema) Field.new(param.name, param.definition.type) else param.type.definition.fields.map do |field| Field.new(field.name, field.type) unless ['page', 'per_page'].include?(field.name) end end end.flatten.compact end |
#fields_and_pagination ⇒ Object
22 23 24 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 22 def fields_and_pagination fields + pagination_fields end |
#found(query) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 56 def found(query) search = extract_request(query) if search.empty? resources else resources.select do |resource| search.all? do |key, data| key = "id" if key == "ids" resource[key].in?(data) end end end end |
#pagination_fields ⇒ Object
26 27 28 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 26 def pagination_fields [Field.new('page', INT), Field.new('per_page', INT)] end |
#to_regex ⇒ Object
15 16 17 18 19 20 |
# File 'lib/service_contract_webmock/contract_matcher.rb', line 15 def to_regex params = fields_and_pagination.map do |field| "#{field.name}=#{field.value}&?" end.join("|") "(#{params})+" end |