Class: HttpRouter::Route
- Inherits:
-
Object
- Object
- HttpRouter::Route
- Defined in:
- lib/http_router/route.rb
Instance Attribute Summary collapse
-
#default_values ⇒ Object
Returns the value of attribute default_values.
-
#dest ⇒ Object
readonly
Returns the value of attribute dest.
-
#matches_with ⇒ Object
readonly
Returns the value of attribute matches_with.
-
#partially_match ⇒ Object
Returns the value of attribute partially_match.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#trailing_slash_ignore ⇒ Object
Returns the value of attribute trailing_slash_ignore.
Instance Method Summary collapse
-
#arbitrary(proc = nil, &block) ⇒ Object
Adds an arbitrary proc matcher to a Route.
-
#as_options ⇒ Object
Returns the options used to create this route.
-
#clone ⇒ Object
Creates a deep uncompiled copy of this route.
-
#compile ⇒ Object
Compiles the route and inserts it into the tree.
-
#compiled? ⇒ Boolean
Compile state for route.
-
#condition(conditions) ⇒ Object
(also: #conditions)
Sets a request condition for the route Returns
self. -
#default(v) ⇒ Object
Sets a default value for the route Returns
self. -
#delete ⇒ Object
Causes this route to recognize the DELETE request method.
-
#get ⇒ Object
Causes this route to recognize the GET and HEAD request methods.
-
#head ⇒ Object
Causes this route to recognize the HEAD request method.
-
#initialize(router, path) ⇒ Route
constructor
A new instance of Route.
-
#matching(match) ⇒ Object
Sets a regex matcher for a variable Returns
self. - #method_missing(method, *args, &block) ⇒ Object
-
#name(name) ⇒ Object
Sets the name of the route Returns
self. -
#named ⇒ Object
Returns the current route’s name.
-
#only_get ⇒ Object
Causes this route to recognize the GET request method.
-
#partial(match = true) ⇒ Object
Sets partial matching on this route.
-
#partially_match? ⇒ Boolean
The current state of partial matching on this route.
-
#post ⇒ Object
Causes this route to recognize the POST request method.
-
#put ⇒ Object
Causes this route to recognize the PUT request method.
-
#redirect(path, status = 302) ⇒ Object
Sets the destination of this route to redirect to an arbitrary URL.
-
#static(root) ⇒ Object
Sets the destination of this route to serve static files from either a directory or a single file.
-
#to(dest = nil, &block) ⇒ Object
Sets the destination of the route.
-
#trailing_slash_ignore? ⇒ Boolean
The current state of trailing / ignoring on this route.
-
#url(*args) ⇒ Object
Generates a URL for this route.
-
#with_options(options) ⇒ Object
Uses an option hash to apply conditions to a Route.
Constructor Details
#initialize(router, path) ⇒ Route
Returns a new instance of Route.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/http_router/route.rb', line 6 def initialize(router, path) @router = router path[0,0] = '/' unless path[0] == ?/ @path = path @original_path = path.dup @partially_match = extract_partial_match(path) @trailing_slash_ignore = extract_trailing_slash(path) @matches_with = {} @arbitrary = [] @conditions = {} @default_values = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/http_router/route.rb', line 19 def method_missing(method, *args, &block) if RequestNode::RequestMethods.include?(method) condition(method => args) else super end end |
Instance Attribute Details
#default_values ⇒ Object
Returns the value of attribute default_values.
4 5 6 |
# File 'lib/http_router/route.rb', line 4 def default_values @default_values end |
#dest ⇒ Object (readonly)
Returns the value of attribute dest.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def dest @dest end |
#matches_with ⇒ Object (readonly)
Returns the value of attribute matches_with.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def matches_with @matches_with end |
#partially_match ⇒ Object
Returns the value of attribute partially_match.
4 5 6 |
# File 'lib/http_router/route.rb', line 4 def partially_match @partially_match end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def path @path end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def paths @paths end |
#trailing_slash_ignore ⇒ Object
Returns the value of attribute trailing_slash_ignore.
4 5 6 |
# File 'lib/http_router/route.rb', line 4 def trailing_slash_ignore @trailing_slash_ignore end |
Instance Method Details
#arbitrary(proc = nil, &block) ⇒ Object
Adds an arbitrary proc matcher to a Route. Receives either a block, or a proc. The proc will receive a Rack::Request object and must return true for the Route to be matched. Returns self.
164 165 166 167 |
# File 'lib/http_router/route.rb', line 164 def arbitrary(proc = nil, &block) @arbitrary << (proc || block) self end |
#as_options ⇒ Object
Returns the options used to create this route.
28 29 30 |
# File 'lib/http_router/route.rb', line 28 def {:matching => @matches_with, :conditions => @conditions, :default_values => @default_values, :name => @name} end |
#clone ⇒ Object
Creates a deep uncompiled copy of this route.
33 34 35 |
# File 'lib/http_router/route.rb', line 33 def clone Route.new(@router, @original_path.dup).() end |
#compile ⇒ Object
Compiles the route and inserts it into the tree. This is called automatically when you add a destination via #to to the route. Until a route is compiled, it will not be recognized.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/http_router/route.rb', line 176 def compile if @paths.nil? router.named_routes[@name] = self if @name @paths = compile_paths @paths.each_with_index do |p1, i| @paths[i+1, @paths.size].each do |p2| raise AmbiguousRouteException.new if p1 === p2 end end @paths.each do |path| current_node = router.root.add_path(path) working_set = current_node.add_request_methods(@conditions) working_set.map!{|node| node.add_arbitrary(@arbitrary)} working_set.each do |current_node| current_node.value = path end end end self end |
#compiled? ⇒ Boolean
Compile state for route. Returns true or false.
170 171 172 |
# File 'lib/http_router/route.rb', line 170 def compiled? !@paths.nil? end |
#condition(conditions) ⇒ Object Also known as: conditions
Sets a request condition for the route Returns self.
Example
router = HttpRouter.new
router.add("/:test").condition(:host => 'www.example.org').name(:test).compile
110 111 112 113 114 115 116 117 118 |
# File 'lib/http_router/route.rb', line 110 def condition(conditions) guard_compiled conditions.each do |k,v| @conditions.key?(k) ? @conditions[k] << v : @conditions[k] = Array(v) end self end |
#default(v) ⇒ Object
Sets a default value for the route Returns self.
Example
router = HttpRouter.new
router.add("/:test").default(:test => 'foo').name(:test).compile
router.url(:test)
# ==> "/foo"
router.url(:test, 'override')
# ==> "/override"
69 70 71 72 |
# File 'lib/http_router/route.rb', line 69 def default(v) @default_values.merge!(v) self end |
#delete ⇒ Object
Causes this route to recognize the DELETE request method. Returns self.
95 96 97 |
# File 'lib/http_router/route.rb', line 95 def delete request_method('DELETE') end |
#get ⇒ Object
Causes this route to recognize the GET and HEAD request methods. Returns self.
75 76 77 |
# File 'lib/http_router/route.rb', line 75 def get request_method('GET', 'HEAD') end |
#head ⇒ Object
Causes this route to recognize the HEAD request method. Returns self.
85 86 87 |
# File 'lib/http_router/route.rb', line 85 def head request_method('HEAD') end |
#matching(match) ⇒ Object
Sets a regex matcher for a variable Returns self.
Example
router = HttpRouter.new
router.add("/:test").matching(:test => /\d+/).name(:test).compile
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/http_router/route.rb', line 127 def matching(match) guard_compiled match.each do |var_name, matchers| matchers = Array(matchers) matchers.each do |m| @matches_with.key?(var_name) ? raise : @matches_with[var_name] = m end end self end |
#name(name) ⇒ Object
Sets the name of the route Returns self.
53 54 55 56 57 |
# File 'lib/http_router/route.rb', line 53 def name(name) @name = name router.named_routes[@name] = self if @name && compiled? self end |
#named ⇒ Object
Returns the current route’s name.
139 140 141 |
# File 'lib/http_router/route.rb', line 139 def named @name end |
#only_get ⇒ Object
Causes this route to recognize the GET request method. Returns self.
100 101 102 |
# File 'lib/http_router/route.rb', line 100 def only_get request_method('GET') end |
#partial(match = true) ⇒ Object
Sets partial matching on this route. Defaults to true. Returns self.
158 159 160 161 |
# File 'lib/http_router/route.rb', line 158 def partial(match = true) @partially_match = match self end |
#partially_match? ⇒ Boolean
The current state of partial matching on this route. Returns true or false.
225 226 227 |
# File 'lib/http_router/route.rb', line 225 def partially_match? @partially_match end |
#post ⇒ Object
Causes this route to recognize the POST request method. Returns self.
80 81 82 |
# File 'lib/http_router/route.rb', line 80 def post request_method('POST') end |
#put ⇒ Object
Causes this route to recognize the PUT request method. Returns self.
90 91 92 |
# File 'lib/http_router/route.rb', line 90 def put request_method('PUT') end |
#redirect(path, status = 302) ⇒ Object
Sets the destination of this route to redirect to an arbitrary URL.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/http_router/route.rb', line 198 def redirect(path, status = 302) raise(ArgumentError, "Status has to be an integer between 300 and 399") unless (300..399).include?(status) to { |env| params = env['router.params'] response = ::Rack::Response.new response.redirect(eval(%|"#{path}"|), status) response.finish } self end |
#static(root) ⇒ Object
Sets the destination of this route to serve static files from either a directory or a single file.
210 211 212 213 214 215 216 217 |
# File 'lib/http_router/route.rb', line 210 def static(root) if File.directory?(root) partial.to ::Rack::File.new(root) else to proc{|env| env['PATH_INFO'] = File.basename(root); ::Rack::File.new(File.dirname(root)).call(env)} end self end |
#to(dest = nil, &block) ⇒ Object
Sets the destination of the route. Receives either a block, or a proc. Returns self.
Example
router = HttpRouter.new
router.add("/:test").matching(:test => /\d+/).name(:test).to(proc{ |env| Rack::Response.new("hi there").finish })
Or
router.add("/:test").matching(:test => /\d+/).name(:test).to { |env| Rack::Response.new("hi there").finish }
151 152 153 154 155 |
# File 'lib/http_router/route.rb', line 151 def to(dest = nil, &block) compile @dest = dest || block self end |
#trailing_slash_ignore? ⇒ Boolean
The current state of trailing / ignoring on this route. Returns true or false.
220 221 222 |
# File 'lib/http_router/route.rb', line 220 def trailing_slash_ignore? @trailing_slash_ignore end |
#url(*args) ⇒ Object
Generates a URL for this route. See HttpRouter#url for how the arguments for this are structured.
230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/http_router/route.rb', line 230 def url(*args) = args.last.is_a?(Hash) ? args.pop : nil ||= {} if default_values = default_values.merge() if default_values && path = if args.empty? matching_path() else matching_path(args, ) end raise UngeneratableRouteException.new unless path path.url(args, ) end |
#with_options(options) ⇒ Object
Uses an option hash to apply conditions to a Route. The following keys are supported. *name – Maps to #name method. *matching – Maps to #matching method. *conditions – Maps to #conditions method. *default_value – Maps to #default_value method.
43 44 45 46 47 48 49 |
# File 'lib/http_router/route.rb', line 43 def () name([:name]) if && [:name] matching([:matching]) if && [:matching] condition([:conditions]) if && [:conditions] default([:default_values]) if && [:default_values] self end |