Class: HttpRouter::Route
- Inherits:
-
Object
- Object
- HttpRouter::Route
- Defined in:
- lib/http_router/route.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#default_values ⇒ Object
readonly
Returns the value of attribute default_values.
-
#matches_with ⇒ Object
readonly
Returns the value of attribute matches_with.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Instance Method Summary collapse
- #arbitrary(blk = nil, &blk2) ⇒ Object
- #arbitrary_with_continue(blk = nil, &blk2) ⇒ Object
- #as_options ⇒ Object
- #clone(new_router) ⇒ Object
- #compiled? ⇒ Boolean
- #default(defaults) ⇒ Object
- #delete ⇒ Object
- #dest ⇒ Object
- #get ⇒ Object
- #head ⇒ Object
- #host(host) ⇒ Object
-
#initialize(router, path, opts = {}) ⇒ Route
constructor
A new instance of Route.
- #match_partially? ⇒ Boolean
- #matching(matchers) ⇒ Object
- #matching_path(params, other_hash = nil) ⇒ Object
- #name(n) ⇒ Object
- #named ⇒ Object
- #partial(match_partially = true) ⇒ Object
- #post ⇒ Object
- #put ⇒ Object
-
#redirect(path, status = 302) ⇒ Object
Sets the destination of this route to redirect to an arbitrary URL.
- #regex? ⇒ Boolean
- #request_method(m) ⇒ Object
- #scheme(scheme) ⇒ Object
- #significant_variable_names ⇒ Object
-
#static(root) ⇒ Object
Sets the destination of this route to serve static files from either a directory or a single file.
- #to(dest = nil, &dest2) ⇒ Object
- #to_s ⇒ Object
- #url(*args) ⇒ Object
- #url_with_params(*args) ⇒ Object
- #user_agent(user_agent) ⇒ Object
Constructor Details
#initialize(router, path, opts = {}) ⇒ Route
Returns a new instance of Route.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/http_router/route.rb', line 7 def initialize(router, path, opts = {}) @router = router @original_path = path @path = path @opts = opts @arbitrary = opts[:arbitrary] || opts[:__arbitrary__] @conditions = opts[:conditions] || opts[:__conditions__] || {} name(opts.delete(:name)) if opts.key?(:name) @matches_with = {} @default_values = opts[:default_values] || {} if @original_path[-1] == ?* @match_partially = true path.slice!(-1) end @paths = OptionalCompiler.new(path).paths end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def conditions @conditions end |
#default_values ⇒ Object (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 |
#matches_with ⇒ Object (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 |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/http_router/route.rb', line 5 def path @path end |
#router ⇒ Object (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
#arbitrary(blk = nil, &blk2) ⇒ Object
115 116 117 118 119 |
# File 'lib/http_router/route.rb', line 115 def arbitrary(blk = nil, &blk2) arbitrary_with_continue { |req, params| req.continue[(blk || blk2)[req, params]] } end |
#arbitrary_with_continue(blk = nil, &blk2) ⇒ Object
121 122 123 124 |
# File 'lib/http_router/route.rb', line 121 def arbitrary_with_continue(blk = nil, &blk2) (@arbitrary ||= []) << (blk || blk2) self end |
#as_options ⇒ Object
24 25 26 |
# File 'lib/http_router/route.rb', line 24 def {:matching => @matches_with, :conditions => @conditions, :default_values => @default_values, :name => @name, :partial => @partially_match, :arbitrary => @arbitrary} end |
#clone(new_router) ⇒ Object
131 132 133 |
# File 'lib/http_router/route.rb', line 131 def clone(new_router) Route.new(new_router, @original_path.dup, ) end |
#compiled? ⇒ Boolean
51 52 53 |
# File 'lib/http_router/route.rb', line 51 def compiled? @compiled end |
#default(defaults) ⇒ Object
82 83 84 85 |
# File 'lib/http_router/route.rb', line 82 def default(defaults) (@default_values ||= {}).merge!(defaults) self end |
#delete ⇒ Object
112 |
# File 'lib/http_router/route.rb', line 112 def delete; request_method('DELETE'); end |
#dest ⇒ Object
37 38 39 |
# File 'lib/http_router/route.rb', line 37 def dest @app end |
#get ⇒ Object
110 |
# File 'lib/http_router/route.rb', line 110 def get; request_method('GET'); end |
#head ⇒ Object
113 |
# File 'lib/http_router/route.rb', line 113 def head; request_method('HEAD'); end |
#host(host) ⇒ Object
65 66 67 |
# File 'lib/http_router/route.rb', line 65 def host(host) ((@conditions ||= {})[:host] ||= []) << host; self end |
#match_partially? ⇒ Boolean
33 34 35 |
# File 'lib/http_router/route.rb', line 33 def match_partially? @match_partially end |
#matching(matchers) ⇒ Object
77 78 79 80 |
# File 'lib/http_router/route.rb', line 77 def matching(matchers) @opts.merge!(matchers) self end |
#matching_path(params, other_hash = nil) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/http_router/route.rb', line 154 def matching_path(params, other_hash = nil) if @paths.size == 1 @paths.first else if params.is_a?(Array) significant_keys = other_hash && significant_variable_names & other_hash.keys @paths.find { |path| var_count = significant_keys ? params.size + significant_keys.size : params.size path.param_names.size == var_count } else @paths.reverse_each do |path| if params && !params.empty? return path if (path.param_names & params.keys).size == path.param_names.size elsif path.param_names.empty? return path end end nil end end end |
#name(n) ⇒ Object
55 56 57 58 59 |
# File 'lib/http_router/route.rb', line 55 def name(n) @name = n @router.named_routes[n] = self self end |
#named ⇒ Object
177 178 179 |
# File 'lib/http_router/route.rb', line 177 def named @name end |
#partial(match_partially = true) ⇒ Object
28 29 30 31 |
# File 'lib/http_router/route.rb', line 28 def partial(match_partially = true) @match_partially = match_partially self end |
#post ⇒ Object
109 |
# File 'lib/http_router/route.rb', line 109 def post; request_method('POST'); end |
#put ⇒ Object
111 |
# File 'lib/http_router/route.rb', line 111 def put; request_method('PUT'); end |
#redirect(path, status = 302) ⇒ Object
Sets the destination of this route to redirect to an arbitrary URL.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/http_router/route.rb', line 88 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 |
#regex? ⇒ Boolean
41 42 43 |
# File 'lib/http_router/route.rb', line 41 def regex? false end |
#request_method(m) ⇒ Object
61 62 63 |
# File 'lib/http_router/route.rb', line 61 def request_method(m) ((@conditions ||= {})[:request_method] ||= []) << m; self end |
#scheme(scheme) ⇒ Object
69 70 71 |
# File 'lib/http_router/route.rb', line 69 def scheme(scheme) ((@conditions ||= {})[:scheme] ||= []) << scheme; self end |
#significant_variable_names ⇒ Object
150 151 152 |
# File 'lib/http_router/route.rb', line 150 def significant_variable_names @significant_variable_names ||= @path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym} end |
#static(root) ⇒ Object
Sets the destination of this route to serve static files from either a directory or a single file.
100 101 102 103 104 105 106 107 |
# File 'lib/http_router/route.rb', line 100 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 |
#to(dest = nil, &dest2) ⇒ Object
45 46 47 48 49 |
# File 'lib/http_router/route.rb', line 45 def to(dest = nil, &dest2) @app = dest || dest2 compile self end |
#to_s ⇒ Object
181 182 183 |
# File 'lib/http_router/route.rb', line 181 def to_s "#<HttpRouter:Route #{object_id} @original_path=#{@original_path.inspect} @conditions=#{@conditions.inspect} @arbitrary=#{@arbitrary.inspect}>" end |
#url(*args) ⇒ Object
126 127 128 129 |
# File 'lib/http_router/route.rb', line 126 def url(*args) result, extra_params = url_with_params(*args) append_querystring(result, extra_params) end |
#url_with_params(*args) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/http_router/route.rb', line 135 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 = if args.empty? matching_path() else matching_path(args, ) end raise UngeneratableRouteException 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 |
#user_agent(user_agent) ⇒ Object
73 74 75 |
# File 'lib/http_router/route.rb', line 73 def user_agent(user_agent) ((@conditions ||= {})[:user_agent] ||= []) << user_agent; self end |