Sha256: b68b6b93aeaada34a5d340b3d1b6a1ca6b9938450afa855f19aa7897c50c30ce

Contents?: true

Size: 1.61 KB

Versions: 16

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require "pp"

module WebMock
  class RequestSignatureSnippet

    attr_reader :request_signature, :request_stub

    def initialize(request_signature)
      @request_signature = request_signature
      @request_stub = RequestStub.from_request_signature(request_signature)
    end

    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

    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

    private

    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

    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

    def request_params
      @request_params ||=
        if request_signature.json_headers?
          JSON.parse(request_signature.body)
        else
          ""
        end
    end

    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

  end
end

Version data entries

16 entries across 16 versions & 5 rubygems

Version Path
webmock-3.25.1 lib/webmock/request_signature_snippet.rb
webmock-3.25.0 lib/webmock/request_signature_snippet.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/webmock-3.24.0/lib/webmock/request_signature_snippet.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/webmock-3.23.1/lib/webmock/request_signature_snippet.rb
webmock-3.24.0 lib/webmock/request_signature_snippet.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/webmock-3.23.1/lib/webmock/request_signature_snippet.rb
webmock-3.23.0 lib/webmock/request_signature_snippet.rb
webmock-3.22.0 lib/webmock/request_signature_snippet.rb
webmock-3.21.2 lib/webmock/request_signature_snippet.rb
webmock-3.21.1 lib/webmock/request_signature_snippet.rb
webmock-3.21.0 lib/webmock/request_signature_snippet.rb
webmock-3.20.0 lib/webmock/request_signature_snippet.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/webmock-3.19.1/lib/webmock/request_signature_snippet.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/webmock-3.19.1/lib/webmock/request_signature_snippet.rb
webmock-3.19.1 lib/webmock/request_signature_snippet.rb
webmock-3.19.0 lib/webmock/request_signature_snippet.rb