Class: HttpRouter::Route
- Inherits:
-
Object
- Object
- HttpRouter::Route
- Defined in:
- lib/http_router/route.rb
Direct Known Subclasses
Constant Summary
- DoubleCompileError =
Class.new(RuntimeError)
Instance Attribute Summary (collapse)
-
- (Object) conditions
readonly
Returns the value of attribute conditions.
-
- (Object) default_values
readonly
Returns the value of attribute default_values.
-
- (Object) dest
readonly
Returns the value of attribute dest.
-
- (Object) match_partially
(also: #match_partially?)
readonly
Returns the value of attribute match_partially.
-
- (Object) matches_with
readonly
Returns the value of attribute matches_with.
-
- (Object) named
readonly
Returns the value of attribute named.
-
- (Object) original_path
readonly
Returns the value of attribute original_path.
-
- (Object) path
readonly
Returns the value of attribute path.
-
- (Object) regex
(also: #regex?)
readonly
Returns the value of attribute regex.
-
- (Object) router
readonly
Returns the value of attribute router.
Instance Method Summary (collapse)
- - (Object) arbitrary(blk = nil, &blk2)
- - (Object) arbitrary_with_continue(blk = nil, &blk2)
- - (Object) as_options
- - (Object) clone(new_router)
- - (Boolean) compiled?
- - (Object) default(defaults)
- - (Object) delete
- - (Object) get
- - (Object) head
- - (Object) host(host)
-
- (Route) initialize(router, path, opts = {})
constructor
A new instance of Route.
- - (Object) matching(matchers)
- - (Object) matching_path(params, other_hash = nil)
- - (Object) name(n)
- - (Object) options
- - (Object) partial(match_partially = true)
- - (Object) patch
- - (Object) post
- - (Object) process_opts
- - (Object) put
-
- (Object) redirect(path, status = 302)
Sets the destination of this route to redirect to an arbitrary URL.
- - (Object) request_method(m)
- - (Object) scheme(scheme)
- - (Object) significant_variable_names
-
- (Object) static(root)
Sets the destination of this route to serve static files from either a directory or a single file.
- - (Object) to(dest = nil, &dest2)
- - (Object) to_s
- - (Object) url(*args)
- - (Object) url_with_params(*args)
- - (Object) user_agent(user_agent)
Constructor Details
- (Route) initialize(router, path, opts = {})
A new instance of Route
9 10 11 12 13 14 15 16 |
# File 'lib/http_router/route.rb', line 9 def initialize(router, path, opts = {}) @router, @original_path, @opts = router, path, opts if @original_path[-1] == ?* @match_partially = true path.slice!(-1) end process_opts end |
Instance Attribute Details
- (Object) conditions (readonly)
Returns the value of attribute conditions
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def conditions @conditions end |
- (Object) default_values (readonly)
Returns the value of attribute default_values
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def default_values @default_values end |
- (Object) dest (readonly)
Returns the value of attribute dest
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def dest @dest end |
- (Object) match_partially (readonly) Also known as: match_partially?
Returns the value of attribute match_partially
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def match_partially @match_partially end |
- (Object) matches_with (readonly)
Returns the value of attribute matches_with
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def matches_with @matches_with end |
- (Object) named (readonly)
Returns the value of attribute named
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def named @named end |
- (Object) original_path (readonly)
Returns the value of attribute original_path
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def original_path @original_path end |
- (Object) path (readonly)
Returns the value of attribute path
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def path @path end |
- (Object) regex (readonly) Also known as: regex?
Returns the value of attribute regex
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def regex @regex end |
- (Object) router (readonly)
Returns the value of attribute router
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def router @router end |
Instance Method Details
- (Object) arbitrary(blk = nil, &blk2)
112 113 114 115 116 |
# File 'lib/http_router/route.rb', line 112 def arbitrary(blk = nil, &blk2) arbitrary_with_continue { |req, params| req.continue[(blk || blk2)[req, params]] } end |
- (Object) arbitrary_with_continue(blk = nil, &blk2)
118 119 120 121 |
# File 'lib/http_router/route.rb', line 118 def arbitrary_with_continue(blk = nil, &blk2) (@arbitrary ||= []) << (blk || blk2) self end |
- (Object) as_options
31 32 33 |
# File 'lib/http_router/route.rb', line 31 def {:__matching__ => @matches_with, :__conditions__ => @conditions, :__default_values__ => @default_values, :__name__ => @named, :__partial__ => @partially_match, :__arbitrary__ => @arbitrary} end |
- (Object) clone(new_router)
128 129 130 |
# File 'lib/http_router/route.rb', line 128 def clone(new_router) Route.new(new_router, @original_path.dup, ) end |
- (Boolean) compiled?
35 36 37 |
# File 'lib/http_router/route.rb', line 35 def compiled? !@paths.nil? end |
- (Object) default(defaults)
77 78 79 80 |
# File 'lib/http_router/route.rb', line 77 def default(defaults) (@default_values ||= {}).merge!(defaults) self end |
- (Object) delete
107 |
# File 'lib/http_router/route.rb', line 107 def delete; request_method('DELETE'); end |
- (Object) get
105 |
# File 'lib/http_router/route.rb', line 105 def get; request_method('GET'); end |
- (Object) head
108 |
# File 'lib/http_router/route.rb', line 108 def head; request_method('HEAD'); end |
- (Object) host(host)
60 61 62 |
# File 'lib/http_router/route.rb', line 60 def host(host) ((@conditions ||= {})[:host] ||= []) << host; self end |
- (Object) matching(matchers)
72 73 74 75 |
# File 'lib/http_router/route.rb', line 72 def matching(matchers) @matches_with.merge!(matchers.is_a?(Array) ? Hash[*matchers] : matchers) self end |
- (Object) matching_path(params, other_hash = nil)
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/http_router/route.rb', line 147 def matching_path(params, other_hash = nil) return @paths.first if @paths.size == 1 case params when Array significant_keys = other_hash && significant_variable_names & other_hash.keys @paths.find { |path| path.param_names.size == (significant_keys ? params.size + significant_keys.size : params.size) } when Hash @paths.find { |path| (params && !params.empty? && (path.param_names & params.keys).size == path.param_names.size) || path.param_names.empty? } end end |
- (Object) name(n)
50 51 52 53 54 |
# File 'lib/http_router/route.rb', line 50 def name(n) @named = n @router.named_routes[n] = self self end |
- (Object) options
109 |
# File 'lib/http_router/route.rb', line 109 def ; request_method('OPTIONS'); end |
- (Object) partial(match_partially = true)
39 40 41 42 |
# File 'lib/http_router/route.rb', line 39 def partial(match_partially = true) @match_partially = match_partially self end |
- (Object) patch
110 |
# File 'lib/http_router/route.rb', line 110 def patch; request_method('PATCH'); end |
- (Object) post
104 |
# File 'lib/http_router/route.rb', line 104 def post; request_method('POST'); end |
- (Object) process_opts
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/http_router/route.rb', line 18 def process_opts @default_values = @opts[:__default_values__] || @opts[:default_values] || {} @arbitrary = @opts[:__arbitrary__] || @opts[:arbitrary] @matches_with = significant_variable_names.include?(:matching) ? @opts : @opts[:__matching__] || @opts[:matching] || {} significant_variable_names.each do |name| @matches_with[name] = @opts[name] if @opts.key?(name) && !@matches_with.key?(name) end @conditions = @opts[:__conditions__] || @opts[:conditions] || {} @match_partially = @opts[:__partial__] if @match_partially.nil? && !@opts[:__partial__].nil? @match_partially = @opts[:partial] if @match_partially.nil? && !@opts[:partial].nil? name(@opts[:__name__] || @opts[:name]) if @opts.key?(:__name__) || @opts.key?(:name) end |
- (Object) put
106 |
# File 'lib/http_router/route.rb', line 106 def put; request_method('PUT'); end |
- (Object) redirect(path, status = 302)
Sets the destination of this route to redirect to an arbitrary URL.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/http_router/route.rb', line 83 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 |
- (Object) request_method(m)
56 57 58 |
# File 'lib/http_router/route.rb', line 56 def request_method(m) ((@conditions ||= {})[:request_method] ||= []) << m; self end |
- (Object) scheme(scheme)
64 65 66 |
# File 'lib/http_router/route.rb', line 64 def scheme(scheme) ((@conditions ||= {})[:scheme] ||= []) << scheme; self end |
- (Object) significant_variable_names
143 144 145 |
# File 'lib/http_router/route.rb', line 143 def significant_variable_names @significant_variable_names ||= @original_path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym} end |
- (Object) static(root)
Sets the destination of this route to serve static files from either a directory or a single file.
95 96 97 98 99 100 101 102 |
# File 'lib/http_router/route.rb', line 95 def static(root) if File.directory?(root) partial.to ::Rack::File.new(root) else to {|env| env['PATH_INFO'] = File.basename(root); ::Rack::File.new(File.dirname(root)).call(env) } end self end |
- (Object) to(dest = nil, &dest2)
44 45 46 47 48 |
# File 'lib/http_router/route.rb', line 44 def to(dest = nil, &dest2) @dest = dest || dest2 add_path_to_tree self end |
- (Object) to_s
158 159 160 |
# File 'lib/http_router/route.rb', line 158 def to_s "#<HttpRouter:Route #{object_id} @original_path=#{@original_path.inspect} @conditions=#{@conditions.inspect} @arbitrary=#{@arbitrary.inspect}>" end |
- (Object) url(*args)
123 124 125 126 |
# File 'lib/http_router/route.rb', line 123 def url(*args) result, extra_params = url_with_params(*args) append_querystring(result, extra_params) end |
- (Object) url_with_params(*args)
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/http_router/route.rb', line 132 def url_with_params(*args) = args.last.is_a?(Hash) ? args.pop : nil = .nil? ? default_values.dup : default_values.merge() if default_values .delete_if{ |k,v| v.nil? } if path = args.empty? ? matching_path() : matching_path(args, ) raise InvalidRouteException unless path result, params = path.url(args, ) mount_point = router.url_mount && router.url_mount.url() mount_point ? [File.join(mount_point, result), params] : [result, params] end |
- (Object) user_agent(user_agent)
68 69 70 |
# File 'lib/http_router/route.rb', line 68 def user_agent(user_agent) ((@conditions ||= {})[:user_agent] ||= []) << user_agent; self end |