Class: ApiSim::Matchers::DynamicRequestMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/api_sim/matchers/dynamic_request_matcher.rb

Constant Summary

Constants inherited from BaseMatcher

BaseMatcher::ALWAYS_TRUE_MATCHER, BaseMatcher::DEFAULT_RACK_RESPONSE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseMatcher

#custom_matcher?, #match_on_body?, #overridden!, #overridden?, #record_request, #requests, #reset!

Constructor Details

#initialize(http_method:, route:, response_generator:, default: false, matcher: ALWAYS_TRUE_MATCHER) ⇒ DynamicRequestMatcher

Returns a new instance of DynamicRequestMatcher.



9
10
11
12
13
14
15
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 9

def initialize(http_method:, route:, response_generator:, default: false, matcher: ALWAYS_TRUE_MATCHER)
  @matcher = matcher
  @route = route
  @http_method = http_method
  @default = default
  @response_generator = response_generator
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 7

def default
  @default
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



7
8
9
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 7

def http_method
  @http_method
end

#matcherObject (readonly)

Returns the value of attribute matcher.



7
8
9
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 7

def matcher
  @matcher
end

#response_generatorObject (readonly)

Returns the value of attribute response_generator.



7
8
9
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 7

def response_generator
  @response_generator
end

#routeObject (readonly)

Returns the value of attribute route.



7
8
9
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 7

def route
  @route
end

Instance Method Details

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 17

def matches?(request)
  request.path == route && request.request_method == http_method && matcher.call(request)
end

#readonly?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 25

def readonly?
  true
end

#response(request) ⇒ Object



21
22
23
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 21

def response(request)
  response_generator.call(request)
end

#to_sObject



29
30
31
32
33
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 29

def to_s
  <<-DOC.gsub(/^\s+/, '')
    #{http_method} #{route} -> DYNAMIC BASED ON REQUEST
  DOC
end