Class: Rack::Router::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/router/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method:, path:, value:, constraint: nil, content_type: nil, accept: nil) ⇒ Route

Returns a new instance of Route.



14
15
16
17
18
19
20
21
22
# File 'lib/rack/router/route.rb', line 14

def initialize(http_method:, path:, value:, constraint: nil, content_type: nil, accept: nil)
  @http_method = http_method.to_s.upcase
  @path = path
  @value = value
  @constraint = constraint
  @content_type = content_type ? Array.wrap(content_type) : nil
  @accept = accept ? Array.wrap(accept) : nil
  @path_parts = calculate_path_parts
end

Instance Attribute Details

#acceptObject (readonly)

Returns the value of attribute accept.



12
13
14
# File 'lib/rack/router/route.rb', line 12

def accept
  @accept
end

#constraintObject (readonly)

Returns the value of attribute constraint.



12
13
14
# File 'lib/rack/router/route.rb', line 12

def constraint
  @constraint
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



12
13
14
# File 'lib/rack/router/route.rb', line 12

def content_type
  @content_type
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



12
13
14
# File 'lib/rack/router/route.rb', line 12

def http_method
  @http_method
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/rack/router/route.rb', line 12

def path
  @path
end

#path_partsObject (readonly)

Returns the value of attribute path_parts.



12
13
14
# File 'lib/rack/router/route.rb', line 12

def path_parts
  @path_parts
end

#valueObject (readonly)

Returns the value of attribute value.



12
13
14
# File 'lib/rack/router/route.rb', line 12

def value
  @value
end

Instance Method Details

#path_hash(request_path) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rack/router/route.rb', line 24

def path_hash(request_path)
  request_path = request_path.to_s.gsub(/\..*\z/, '')
  request_path_parts = request_path.split('/').reject(&:empty?)
  pairs = path.split('/').map.with_index do |part, idx|
    next if part[0] != ':'
    [part[1..-1].to_sym, request_path_parts[idx]]
  end
  pairs.reject(&:nil?).to_h
end


34
35
36
37
38
39
# File 'lib/rack/router/route.rb', line 34

def print_route
  str = "\t#{http_method} \t/#{path}"
  str += "\tcontent_type=[#{content_type.join(',')}]" unless content_type.nil?
  str += "\taccept=[#{content_type.join(',')}]" unless accept.nil?
  str
end