Sha256: 09b9bba57df785c16d31a06d7c6fa1dd8328662c776e3d894d49edb5d0d05740

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

module Peddler
  # A custom matcher that can be used to record MWS interactions when
  # integration-testing
  class VCRMatcher
    TRANSIENT_PARAMS = %w[
      Signature Timestamp StartDate CreatedAfter QueryStartDateTime
    ].freeze

    SELLER_PARAMS = %w[
      AWSAccessKeyId SellerId
    ].freeze

    class << self
      def call(*requests)
        new(*requests).compare
      end

      def ignored_params
        @ignored_params ||= TRANSIENT_PARAMS.dup
      end

      def ignore_seller!
        ignored_params.concat(SELLER_PARAMS)
      end
    end

    attr_reader :requests

    def initialize(*requests)
      @requests = requests
    end

    def compare
      compare_uris && compare_bodies
    end

    private

    def compare_uris
      return false if hosts.reduce(:!=) || paths.reduce(:!=)
      return true if queries.all?(&:empty?)

      queries.reduce(:==)
    end

    def compare_bodies
      bodies.reduce(:==)
    end

    def extract_params(string)
      return {} unless string

      params = ::CGI.parse(string)
      self.class.ignored_params.each do |k|
        params.delete(k)
      end

      params
    end

    def uris
      requests.map { |r| URI.parse(r.uri) }
    end

    def hosts
      uris.map(&:host)
    end

    def paths
      uris.map(&:path)
    end

    def queries
      uris.map { |uri| extract_params(uri.query) }
    end

    def bodies
      if queries.all?(&:empty?)
        requests.map { |request| extract_params(request.body) }
      else
        requests.map(&:body)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peddler-1.6.3 lib/peddler/vcr_matcher.rb
peddler-1.6.2 lib/peddler/vcr_matcher.rb
peddler-1.6.1 lib/peddler/vcr_matcher.rb