class Hyperion::Kim::Matcher
Attributes
func[R]
Fancy predicates for HTTP requests. Features:
-
and/or/not combinators
-
If a predicate raises an error, it is caught and treated as falsey. simplifies predicates. For example: headers.starts_with?('application/') will raise if no Allow header was sent, however we really just want to treat that as a non-match.
-
Parameter extraction. A matcher can return an augmented
Request
as the truthy value.
Public Class Methods
and(*ms)
click to toggle source
# File lib/hyperion_test/kim/matcher.rb, line 41 def self.and(*ms) m, *rest = ms if rest.empty? m else m.and(Matcher.and(*rest)) end end
new(func=nil, &block)
click to toggle source
# File lib/hyperion_test/kim/matcher.rb, line 15 def initialize(func=nil, &block) @func = Matcher.wrap(block || func) end
Private Class Methods
coerce(f, req)
click to toggle source
# File lib/hyperion_test/kim/matcher.rb, line 75 def self.coerce(f, req) case v = f.call(req) when TrueClass then req when FalseClass then nil else v end rescue nil # treat predicate errors as falsey end
wrap(f)
click to toggle source
Coerce the return value of the function to nil/hash in case it returns a simple true/false. Update (mutate) the request params with any addition values gleaned by a successful match.
# File lib/hyperion_test/kim/matcher.rb, line 56 def self.wrap(f) if f.is_a?(Matcher) f else proc do |req| v = coerce(f, req) # Update the request parameters. respond_to?(:merge) is a # compromise between outright depending on Kim::Request # and threading a totally generic 'update' function # through all the matcher code. if v && req.respond_to?(:merge) req.merge(v) else v end end end end
Public Instance Methods
and(other)
click to toggle source
# File lib/hyperion_test/kim/matcher.rb, line 23 def and(other) Matcher.new do |req| (req2 = @func.call(req)) && other.call(req2) end end
call(req)
click to toggle source
# File lib/hyperion_test/kim/matcher.rb, line 19 def call(req) @func.call(req) end
not()
click to toggle source
# File lib/hyperion_test/kim/matcher.rb, line 35 def not Matcher.new do |req| @func.call(req) ? nil : req end end
or(other)
click to toggle source
# File lib/hyperion_test/kim/matcher.rb, line 29 def or(other) Matcher.new do |req| @func.call(req) || other.call(req) end end