Class: Acrylic::Route

Inherits:
Object show all
Defined in:
lib/cascade.rb

Direct Known Subclasses

ErrorRoute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, handler) ⇒ Route

Returns a new instance of Route.



13
14
15
16
17
18
# File 'lib/cascade.rb', line 13

def initialize args, handler
    @handler = handler

    @paths  = args.fetch(:path).containerize
    @verbs  = (args[:verb] or :GET).containerize.map do |verb| verb.downcase.to_sym end
end

Instance Attribute Details

#pathsObject (readonly)

list of path rules (wildcards or regexps)



11
12
13
# File 'lib/cascade.rb', line 11

def paths
  @paths
end

#verbsObject (readonly)

list of HTTP verbs the request will be matched against.



8
9
10
# File 'lib/cascade.rb', line 8

def verbs
  @verbs
end

Instance Method Details

#call(req) ⇒ Object



37
38
39
# File 'lib/cascade.rb', line 37

def call req
    @handler.call req
end

#match?(req) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/cascade.rb', line 32

def match? req
    # match against paths
    (path_match? req.path) and (@verbs.include? req.request_method.downcase.to_sym )
end

#path_match?(path) ⇒ Boolean

checks if a path matches one of the rules.

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
# File 'lib/cascade.rb', line 21

def path_match? path
    @paths.each do |rule|
        if     ((rule.is_a? Regexp) and (rule.match? path)) \
            or ((rule.is_a? Proc)   and (rule.call path))   \
            or ((rule.is_a? String) and (File.fnmatch rule, path, File::Constants::FNM_EXTGLOB | File::Constants::FNM_PATHNAME))
        then return true end
    end

    return false
end