class ServiceContractWebmock::ContractMatcher
Constants
- INT
Attributes
endpoint[R]
resources[R]
Public Class Methods
new(endpoint, resources)
click to toggle source
# File lib/service_contract_webmock/contract_matcher.rb, line 8 def initialize(endpoint, resources) @endpoint = endpoint @resources = resources end
Public Instance Methods
apply_pagination(records, query)
click to toggle source
# File lib/service_contract_webmock/contract_matcher.rb, line 77 def apply_pagination(records, query) params = CGI.parse(query) if params["per_page"].present? && params["per_page"].first.present? per_page = params["per_page"].first.to_i records = records.take(per_page) end records end
do_search(query)
click to toggle source
# File lib/service_contract_webmock/contract_matcher.rb, line 63 def do_search(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
extract_request(query)
click to toggle source
# File lib/service_contract_webmock/contract_matcher.rb, line 48 def extract_request(query) params = Rack::Utils.parse_nested_query(query).except("includes", "page", "per_page", "order") fields.select {|field| field.name.in?(params.keys) }.each_with_object({}) do |field, acc| val = params[field.name] data = (val.is_a?(Array) ? val : val.split(',')).flat_map {|value| field.convert(value)} acc[field.name] = data end end
field_int?(name)
click to toggle source
# File lib/service_contract_webmock/contract_matcher.rb, line 44 def field_int?(name) fields.detect {|f| f.name == name}.try(:int?) end
fields()
click to toggle source
# 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()
click to toggle source
# File lib/service_contract_webmock/contract_matcher.rb, line 22 def fields_and_pagination fields + pagination_fields end
found(query)
click to toggle source
# File lib/service_contract_webmock/contract_matcher.rb, line 57 def found(query) records = do_search(query) apply_pagination(records, query) end
pagination_fields()
click to toggle source
# 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()
click to toggle source
# 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