Class: Faraday::Adapter::Test::Stub

Inherits:
Struct
  • Object
show all
Defined in:
lib/faraday/adapter/test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full, headers, body, block) ⇒ Stub

Returns a new instance of Stub.



127
128
129
130
131
132
133
# File 'lib/faraday/adapter/test.rb', line 127

def initialize(full, headers, body, block)
  path, query = full.respond_to?(:split) ? full.split("?") : full
  params = query ?
    Faraday::Utils.parse_nested_query(query) :
    {}
  super(path, params, headers, body, block)
end

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



126
127
128
# File 'lib/faraday/adapter/test.rb', line 126

def block
  @block
end

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



126
127
128
# File 'lib/faraday/adapter/test.rb', line 126

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



126
127
128
# File 'lib/faraday/adapter/test.rb', line 126

def headers
  @headers
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



126
127
128
# File 'lib/faraday/adapter/test.rb', line 126

def params
  @params
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



126
127
128
# File 'lib/faraday/adapter/test.rb', line 126

def path
  @path
end

Instance Method Details

#headers_match?(request_headers) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
# File 'lib/faraday/adapter/test.rb', line 163

def headers_match?(request_headers)
  headers.keys.all? do |key|
    request_headers[key] == headers[key]
  end
end

#matches?(request_uri, request_headers, request_body) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/faraday/adapter/test.rb', line 135

def matches?(request_uri, request_headers, request_body)
  request_path, request_query = request_uri.split('?')
  request_params = request_query ?
    Faraday::Utils.parse_nested_query(request_query) :
    {}
  # meta is a hash use as carrier
  # that will be yielded to consumer block
  meta = {}
  return path_match?(request_path, meta) &&
    params_match?(request_params) &&
    (body.to_s.size.zero? || request_body == body) &&
    headers_match?(request_headers), meta
end

#params_match?(request_params) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
# File 'lib/faraday/adapter/test.rb', line 157

def params_match?(request_params)
  params.keys.all? do |key|
    request_params[key] == params[key]
  end
end

#path_match?(request_path, meta) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
# File 'lib/faraday/adapter/test.rb', line 149

def path_match?(request_path, meta)
  if path.is_a? Regexp
    !!(meta[:match_data] = path.match(request_path))
  else
    path == request_path
  end
end

#to_sObject



169
170
171
# File 'lib/faraday/adapter/test.rb', line 169

def to_s
  "#{path} #{body}"
end