Class: Waves::Matchers::URI

Inherits:
Object show all
Defined in:
lib/waves/matchers/uri.rb

Overview

Matcher for the whole URL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ URI

Returns a new instance of URI.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/waves/matchers/uri.rb', line 11

def initialize(options)
  
  # Simplest to fake it if there is no path to match
  @path = if options[:path]
    Waves::Matchers::Path.new( options[:path] )
  else 
    lambda { {} }
  end
  
  @constraints = {}

  @constraints[:host] = options[:host] if options[:host]
  @constraints[:port] = options[:port] if options[:port]
  @constraints[:scheme] = options[:scheme] if options[:scheme]

  raise ArgumentError, "No URI matching" if !@path and @constraints.empty?
end

Instance Attribute Details

#constraintsObject

Returns the value of attribute constraints.



9
10
11
# File 'lib/waves/matchers/uri.rb', line 9

def constraints
  @constraints
end

Instance Method Details

#[](request) ⇒ Object

Proc-like interface



60
61
62
# File 'lib/waves/matchers/uri.rb', line 60

def [](request)
  call request
end

#call(request) ⇒ Object

Match resource URL.

This returns the set of captures if matching!



33
34
35
36
37
# File 'lib/waves/matchers/uri.rb', line 33

def call(request)
  if captures = @path.call(request) and test(request)
    captures
  end
end

#test(request) ⇒ Object

TODO:

This could maybe be optimised by detecting empty constraints before calling. Not high importance. –rue



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/waves/matchers/uri.rb', line 44

def test(request)
  constraints.all? {|key, val|
    if val.nil? or val == true
      true
    else
      if val.respond_to? :call
        val.call( request )
      else
        val == request.send( key ) or val === request.send( key ) or request.send( key ) === val
      end
    end
  }
end