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

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

Overview

Stub request rubocop:disable Style/StructInheritance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

rubocop:enable Style/StructInheritance



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

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

  super(host, path, params, headers, body, block)
end

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



146
147
148
# File 'lib/faraday/adapter/test.rb', line 146

def block
  @block
end

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



146
147
148
# File 'lib/faraday/adapter/test.rb', line 146

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



146
147
148
# File 'lib/faraday/adapter/test.rb', line 146

def headers
  @headers
end

#hostObject

Returns the value of attribute host

Returns:

  • (Object)

    the current value of host



146
147
148
# File 'lib/faraday/adapter/test.rb', line 146

def host
  @host
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



146
147
148
# File 'lib/faraday/adapter/test.rb', line 146

def params
  @params
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



146
147
148
# File 'lib/faraday/adapter/test.rb', line 146

def path
  @path
end

Instance Method Details

#headers_match?(request_headers) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
# File 'lib/faraday/adapter/test.rb', line 192

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

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

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/faraday/adapter/test.rb', line 160

def matches?(request_host, request_uri, request_headers, request_body)
  request_path, request_query = request_uri.split('?')
  request_params =
    if request_query
      Faraday::Utils.parse_nested_query(request_query)
    else
      {}
    end
  # meta is a hash used as carrier
  # that will be yielded to consumer block
  meta = {}
  [(host.nil? || host == request_host) &&
    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)


186
187
188
189
190
# File 'lib/faraday/adapter/test.rb', line 186

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)


178
179
180
181
182
183
184
# File 'lib/faraday/adapter/test.rb', line 178

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



198
199
200
# File 'lib/faraday/adapter/test.rb', line 198

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