Class: Simple::Httpd::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/httpd/route.rb

Constant Summary collapse

H =
::Simple::Httpd::Helpers
SELF =
self

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/simple/httpd/route.rb', line 5

def path
  @path
end

#source_locationObject (readonly)

Returns the value of attribute source_location.



5
6
7
# File 'lib/simple/httpd/route.rb', line 5

def source_location
  @source_location
end

#verbObject (readonly)

Returns the value of attribute verb.



5
6
7
# File 'lib/simple/httpd/route.rb', line 5

def verb
  @verb
end

Class Method Details

.build(route) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/simple/httpd/route.rb', line 7

def self.build(route)
  expect! route => [
    ::Simple::Httpd::Route,
    {
      verb: %w(GET POST PUT DELETE HEAD OPTIONS),
      path: String,
      source_location: [nil, Array, String]
    }
  ]

  return route if route.is_a?(self)

  ::Simple::Httpd::Route.new(*route.values_at(:verb, :path, :source_location))
end

Instance Method Details

#inspectObject



37
38
39
# File 'lib/simple/httpd/route.rb', line 37

def inspect
  "<#{SELF.name}: #{self}>"
end

#prefix(*prefixes) ⇒ Object



41
42
43
# File 'lib/simple/httpd/route.rb', line 41

def prefix(*prefixes)
  SELF.new(verb, File.join(*prefixes, path), source_location)
end

#source_location_strObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/simple/httpd/route.rb', line 45

def source_location_str
  case source_location
  when Array
    path, lineno = *source_location
    path = H.shorten_path(path)
    "#{path}:#{lineno}"
  when String, nil
    source_location
  else
    source_location.inspect
  end
end

#to_sObject



32
33
34
35
# File 'lib/simple/httpd/route.rb', line 32

def to_s
  parts = [verb, path, source_location_str]
  parts.compact.join " "
end