Sha256: 6ec705f5871e34839f263e0d0e9a99425fefd0c028a68fb67aee0dab65aa2df4

Contents?: true

Size: 1.63 KB

Versions: 12

Compression:

Stored size: 1.63 KB

Contents

require "addressable/template"

module WildcardMatchers
  module Matchers
    define_wildcard_matcher(:with_uri_template)

    class WithUriTemplate < ::WildcardMatchers::WildcardMatcher
      def initialize(expectations, position = ".", &block)
        template, expectation = *expectations

        @template    = template.is_a?(::Addressable::Template) ? template : ::Addressable::Template.new(template)

        @expectation = (block_given? ? block : expectation)
        @position    = position
      end

      def without_query!
        @without_query = true

        # umhhh
        uri = ::Addressable::URI.parse(@template.pattern.split("{?").first)

        @template_without_query = ::Addressable::Template.new(uri.omit(:query).to_s)

        self
      end

      protected
      def wildcard_match(actual)
        unless actual
          return errors.push("#{position}: expect URI but nil")
        end

        if @without_query
          uri = ::Addressable::URI.parse(actual)

          params = uri.query_values || {}

          unless extracted = @template_without_query.extract(uri.omit!(:query).to_s)
            return errors.push("#{position}: expect #{uri.to_s} to match #{@template_without_query.pattern}")
          end

          errors.push(*self.class.superclass.check_errors(extracted.merge(params), expectation, position))
        else
          unless extracted = @template.extract(actual)
            return errors.push("#{position}: expect #{actual} to match #{@template.pattern}")
          end

          errors.push(*self.class.superclass.check_errors(extracted, expectation, position))
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
wildcard_matchers-0.9.2 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.9.1 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.9.0 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.4.0 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.3.1 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.3.0 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.2.1 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.2.0 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.1.8 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.1.7 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.1.6 lib/wildcard_matchers/matchers/with_uri_template.rb
wildcard_matchers-0.1.5 lib/wildcard_matchers/matchers/with_uri_template.rb