Sha256: c1d13577b4458bcf3d12a202c685a12ab98bc11df045c440bcfd45a54cc855b5

Contents?: true

Size: 1.58 KB

Versions: 120

Compression:

Stored size: 1.58 KB

Contents

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?

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

    def request_stubs
      return if WebMock::StubRegistry.instance.request_stubs.empty?

      text = "registered request stubs:\n"
      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("") do |stream|
        PP.pp(string_to_print, stream)
        stream.rewind
        stream.read
      end
    end

  end
end

Version data entries

120 entries across 111 versions & 8 rubygems

Version Path
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/webmock-1.24.6/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
cloudsmith-api-0.30.7 vendor/bundle/ruby/2.3.0/gems/webmock-1.24.6/lib/webmock/request_signature_snippet.rb
color_me_shop-1.0.0 vendor/bundle/ruby/2.5.0/gems/webmock-1.24.6/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.1.1.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.0.4.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.0.3.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.4.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/webmock-2.3.2/lib/webmock/request_signature_snippet.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/lib/webmock/request_signature_snippet.rb
cloudsmith-api-0.21.4 vendor/bundle/ruby/2.3.0/gems/webmock-1.24.6/lib/webmock/request_signature_snippet.rb