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

Attributes inherited from BaseMatcher

#request_schema

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.



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

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

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



9
10
11
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 9

def default
  @default
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



9
10
11
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 9

def http_method
  @http_method
end

#matcherObject (readonly)

Returns the value of attribute matcher.



9
10
11
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 9

def matcher
  @matcher
end

#queryObject (readonly)

Returns the value of attribute query.



9
10
11
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 9

def query
  @query
end

#response_generatorObject (readonly)

Returns the value of attribute response_generator.



9
10
11
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 9

def response_generator
  @response_generator
end

#routeObject (readonly)

Returns the value of attribute route.



9
10
11
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 9

def route
  @route
end

Instance Method Details

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 19

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

#readonly?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 27

def readonly?
  true
end

#response(request) ⇒ Object



23
24
25
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 23

def response(request)
  response_generator.call(SmartRequest.new(request, self))
end

#to_sObject



31
32
33
34
35
# File 'lib/api_sim/matchers/dynamic_request_matcher.rb', line 31

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