Class: Waves::Matchers::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Request

TODO:

Further optimise the cases where there are no constraints. –rue

Returns a new instance of Request.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/waves/matchers/request.rb', line 14

def initialize(options)
  
  @uri = Matchers::URI.new( options )

  @constraints = {}

  if options[ :requested ]
    @constraints[ :requested ] = Matchers::Requested.new( options[ :requested ] )
  end
  
  if options.key?( :accept ) || options.key?( :lang ) || options.key?( :charset )
    @constraints[:accept] = Matchers::Accept.new( options ) 
  end
  
  if options.key?( :ext )
    @constraints[ :ext ] = Matchers::Ext.new(  options[ :ext ] )
  elsif options.key?( :extension )
    @constraints[ :ext ] = Matchers::Ext.new(  options[ :extension ] )
  end
  
  if options.key?( :query )
    @constraints[:query] = Matchers::Query.new( options[:query] ) 
  end
    
  if options[ :traits ]
    @constraints[ :traits ] = Matchers::Traits.new( options[ :traits ] )
  end
    
  if options[ :when ]
    @constraints[ :when ] = options[ :when ]
  end
  
end

Instance Attribute Details

#constraintsObject

Returns the value of attribute constraints.



6
7
8
# File 'lib/waves/matchers/request.rb', line 6

def constraints
  @constraints
end

Instance Method Details

#[](request) ⇒ Object

Proc-like interface



78
79
80
# File 'lib/waves/matchers/request.rb', line 78

def [](request)
  call request
end

#call(request) ⇒ Object

Process all matchers for request.



50
51
52
53
54
# File 'lib/waves/matchers/request.rb', line 50

def call(request)
  if captured = @uri.call(request) and test(request)
    request.traits.waves.captured = captured
  end
end

#test(request) ⇒ Object

TODO:

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/waves/matchers/request.rb', line 61

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