class WebMock::RequestSignatureSnippet

Attributes

request_signature[R]
request_stub[R]

Public Class Methods

new(request_signature) click to toggle source
# File lib/webmock/request_signature_snippet.rb, line 8
def initialize(request_signature)
  @request_signature = request_signature
  @request_stub = RequestStub.from_request_signature(request_signature)
end

Public Instance Methods

request_stubs() click to toggle source
# File lib/webmock/request_signature_snippet.rb, line 20
def request_stubs
  return if WebMock::StubRegistry.instance.request_stubs.empty?

  text = "registered request stubs:\n".dup
  WebMock::StubRegistry.instance.request_stubs.each do |stub|
    text << "\n#{WebMock::StubRequestSnippet.new(stub).to_s(false)}"
    add_body_diff(stub, text) if WebMock.show_body_diff?
  end
  text
end
stubbing_instructions() click to toggle source
# File lib/webmock/request_signature_snippet.rb, line 13
def stubbing_instructions
  return unless WebMock.show_stubbing_instructions?

  "You can stub this request with the following snippet:\n\n" +
    WebMock::StubRequestSnippet.new(request_stub).to_s
end

Private Instance Methods

add_body_diff(stub, text) click to toggle source
# File lib/webmock/request_signature_snippet.rb, line 33
def add_body_diff(stub, text)
  body_diff_str = signature_stub_body_diff(stub)
  text << "\n\n#{body_diff_str}" unless body_diff_str.empty?
end
pretty_print_to_string(string_to_print) click to toggle source
# File lib/webmock/request_signature_snippet.rb, line 52
def pretty_print_to_string(string_to_print)
  StringIO.open("".dup) do |stream|
    PP.pp(string_to_print, stream)
    stream.rewind
    stream.read
  end
end
request_params() click to toggle source
# File lib/webmock/request_signature_snippet.rb, line 43
def request_params
  @request_params ||=
    if request_signature.json_headers?
      JSON.parse(request_signature.body)
    else
      ""
    end
end
signature_stub_body_diff(stub) click to toggle source
# File lib/webmock/request_signature_snippet.rb, line 38
def signature_stub_body_diff(stub)
  diff = RequestBodyDiff.new(request_signature, stub).body_diff
  diff.empty? ? "" : "Body diff:\n #{pretty_print_to_string(diff)}"
end