Sha256: 9d87583f32648506bbb27986b035b3b3d4fcc78b32dc687b1b8bbf54c8cd8565

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'json'

module Vacuum
  # Custom VCR matcher for stubbing calls to the Product Advertising API
  #
  # The matcher is not required by default.
  #
  # @example
  #    require 'vacuum/matcher'
  #
  #    # in your test
  #    VCR.insert_cassette('cassette_name',
  #                        match_requests_on: [Vacuum::Matcher])
  #
  # @see https://relishapp.com/vcr/vcr/v/5-0-0/docs/request-matching/register-and-use-a-custom-matcher
  class Matcher
    IGNORED_KEYS = %w[PartnerTag].freeze
    private_constant :IGNORED_KEYS

    # @!visibility private
    attr_reader :requests

    # @!visibility private
    def self.call(*requests)
      new(*requests).compare
    end

    # @!visibility private
    def initialize(*requests)
      @requests = requests
    end

    # @!visibility private
    def compare
      uris.reduce(:==) && bodies.reduce(:==)
    end

    private

    def uris
      requests.map(&:uri)
    end

    def bodies
      requests.map do |req|
        params = JSON.parse(req.body)
        IGNORED_KEYS.each { |k| params.delete(k) }

        params
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vacuum-3.3.0 lib/vacuum/matcher.rb
vacuum-3.2.0 lib/vacuum/matcher.rb
vacuum-3.1.0 lib/vacuum/matcher.rb