Class: AngryWeb::Matcher

Inherits:
Object show all
Defined in:
lib/angry_web/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Matcher

Returns a new instance of Matcher.

Yields:

  • (_self)

Yield Parameters:



53
54
55
56
# File 'lib/angry_web/matcher.rb', line 53

def initialize(&blk)
  @parts = {}
  yield self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/angry_web/matcher.rb', line 77

def method_missing(meth,*args,&block)
  if args.size == 1
    @parts[meth] = __convert_value(args.first)
  else
    super
  end
end

Instance Attribute Details

#mismatchesObject (readonly)

Returns the value of attribute mismatches.



51
52
53
# File 'lib/angry_web/matcher.rb', line 51

def mismatches
  @mismatches
end

Instance Method Details

#__convert_value(value) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/angry_web/matcher.rb', line 85

def __convert_value(value)
  case value
  when ValueMatcher
    value
  when Regexp
    REMatcher.new(value)
  else
    EqlMatcher.new(value)
  end
end

#__transform_value_from_uri(uri, key) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/angry_web/matcher.rb', line 96

def __transform_value_from_uri(uri,key)
  case key
  when :params
    Rack::Utils.parse_query(uri[:query])
  else
    uri[key]
  end
end

#hash_subset(spec) ⇒ Object



73
74
75
# File 'lib/angry_web/matcher.rb', line 73

def hash_subset(spec)
  HashSubsetMatcher.new(spec)
end

#match?(uri) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/angry_web/matcher.rb', line 58

def match?(uri)
  uri = Addressable::URI.heuristic_parse(uri)
  uri_hash = uri.to_hash

  @mismatches = []

  @parts.each {|k,v|
    unless v.match?(__transform_value_from_uri(uri_hash,k))
      @mismatches << v.mismatch_reason
    end
  }

  @mismatches.empty?
end