Class: WebMock::RequestPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/webmock/request_pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, options = {}) ⇒ RequestPattern

Returns a new instance of RequestPattern.


17
18
19
20
21
22
23
24
# File 'lib/webmock/request_pattern.rb', line 17

def initialize(method, uri, options = {})
  @method_pattern  = MethodPattern.new(method)
  @uri_pattern     = create_uri_pattern(uri)
  @body_pattern    = nil
  @headers_pattern = nil
  @with_block      = nil
  assign_options(options)
end

Instance Attribute Details

#body_patternObject (readonly)

Returns the value of attribute body_pattern


15
16
17
# File 'lib/webmock/request_pattern.rb', line 15

def body_pattern
  @body_pattern
end

#headers_patternObject (readonly)

Returns the value of attribute headers_pattern


15
16
17
# File 'lib/webmock/request_pattern.rb', line 15

def headers_pattern
  @headers_pattern
end

#method_patternObject (readonly)

Returns the value of attribute method_pattern


15
16
17
# File 'lib/webmock/request_pattern.rb', line 15

def method_pattern
  @method_pattern
end

#uri_patternObject (readonly)

Returns the value of attribute uri_pattern


15
16
17
# File 'lib/webmock/request_pattern.rb', line 15

def uri_pattern
  @uri_pattern
end

Instance Method Details

#matches?(request_signature) ⇒ Boolean

Returns:

  • (Boolean)

33
34
35
36
37
38
39
40
41
# File 'lib/webmock/request_pattern.rb', line 33

def matches?(request_signature)
  content_type = request_signature.headers['Content-Type'] if request_signature.headers
  content_type = content_type.split(';').first if content_type
  @method_pattern.matches?(request_signature.method) &&
    @uri_pattern.matches?(request_signature.uri) &&
    (@body_pattern.nil? || @body_pattern.matches?(request_signature.body, content_type || "")) &&
    (@headers_pattern.nil? || @headers_pattern.matches?(request_signature.headers)) &&
    (@with_block.nil? || @with_block.call(request_signature))
end

#to_sObject


43
44
45
46
47
48
49
50
# File 'lib/webmock/request_pattern.rb', line 43

def to_s
  string = "#{@method_pattern.to_s.upcase}".dup
  string << " #{@uri_pattern.to_s}"
  string << " with body #{@body_pattern.to_s}" if @body_pattern
  string << " with headers #{@headers_pattern.to_s}" if @headers_pattern
  string << " with given block" if @with_block
  string
end

#with(options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)

26
27
28
29
30
31
# File 'lib/webmock/request_pattern.rb', line 26

def with(options = {}, &block)
  raise ArgumentError.new('#with method invoked with no arguments. Either options hash or block must be specified. Created a block with do..end? Try creating it with curly braces {} instead.') if options.empty? && !block_given?
  assign_options(options)
  @with_block = block
  self
end