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.
-
#dest ⇒ Object
readonly
Returns the value of attribute dest.
-
#match_partially ⇒ Object
(also: #match_partially?)
readonly
Returns the value of attribute match_partially.
-
#matches_with ⇒ Object
readonly
Returns the value of attribute matches_with.
-
#named ⇒ Object
readonly
Returns the value of attribute named.
-
#original_path ⇒ Object
readonly
Returns the value of attribute original_path.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#regex ⇒ Object
(also: #regex?)
readonly
Returns the value of attribute regex.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Instance Method Summary collapse
- #add_to_contitions(name, *vals) ⇒ Object
- #arbitrary(blk = nil, &blk2) ⇒ Object
- #arbitrary_with_continue(blk = nil, &blk2) ⇒ Object
- #as_options ⇒ Object
- #clone(new_router) ⇒ Object
- #compiled? ⇒ Boolean
- #conenct ⇒ Object
- #default(defaults) ⇒ Object
- #delete ⇒ Object
- #get ⇒ Object
- #head ⇒ Object
- #host(*host) ⇒ Object
-
#initialize(router, path, opts = {}) ⇒ Route
constructor
A new instance of Route.
- #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) ⇒ Object
Sets the destination of this route to redirect to an arbitrary URL.
- #request_method(*method) ⇒ 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
- #trace ⇒ Object
- #url(*args) ⇒ Object
- #url_args_processing(args) ⇒ Object
- #url_with_params(*a) ⇒ 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 |
# File 'lib/http_router/route.rb', line 7 def initialize(router, path, opts = {}) @router, @original_path, @opts = router, path, opts if @original_path @match_partially = true and path.slice!(-1) if @original_path[/[^\\]\*$/] @original_path[0, 0] = '/' if @original_path[0] != ?/ else @match_partially = true end process_opts end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def conditions @conditions end |
#default_values ⇒ Object (readonly)
Returns the value of attribute default_values.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 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 |
#match_partially ⇒ Object (readonly) Also known as: match_partially?
Returns the value of attribute match_partially.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def match_partially @match_partially 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 |
#named ⇒ Object (readonly)
Returns the value of attribute named.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def named @named end |
#original_path ⇒ Object (readonly)
Returns the value of attribute original_path.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def original_path @original_path 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 |
#regex ⇒ Object (readonly) Also known as: regex?
Returns the value of attribute regex.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def regex @regex end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
3 4 5 |
# File 'lib/http_router/route.rb', line 3 def router @router end |
Instance Method Details
#add_to_contitions(name, *vals) ⇒ Object
74 75 76 77 |
# File 'lib/http_router/route.rb', line 74 def add_to_contitions(name, *vals) ((@conditions ||= {})[name] ||= []).concat(vals.flatten) self end |
#arbitrary(blk = nil, &blk2) ⇒ Object
121 122 123 124 125 |
# File 'lib/http_router/route.rb', line 121 def arbitrary(blk = nil, &blk2) arbitrary_with_continue { |req, params| req.continue[(blk || blk2)[req, params]] } end |
#arbitrary_with_continue(blk = nil, &blk2) ⇒ Object
127 128 129 130 |
# File 'lib/http_router/route.rb', line 127 def arbitrary_with_continue(blk = nil, &blk2) (@arbitrary ||= []) << (blk || blk2) self end |
#as_options ⇒ Object
32 33 34 |
# File 'lib/http_router/route.rb', line 32 def {:__matching__ => @matches_with, :__conditions__ => @conditions, :__default_values__ => @default_values, :__name__ => @named, :__partial__ => @partially_match, :__arbitrary__ => @arbitrary} end |
#clone(new_router) ⇒ Object
137 138 139 |
# File 'lib/http_router/route.rb', line 137 def clone(new_router) Route.new(new_router, @original_path.dup, ) end |
#compiled? ⇒ Boolean
36 37 38 |
# File 'lib/http_router/route.rb', line 36 def compiled? !@paths.nil? end |
#conenct ⇒ Object
119 |
# File 'lib/http_router/route.rb', line 119 def conenct; request_method('CONNECT'); end |
#default(defaults) ⇒ Object
84 85 86 87 |
# File 'lib/http_router/route.rb', line 84 def default(defaults) (@default_values ||= {}).merge!(defaults) self end |
#delete ⇒ Object
114 |
# File 'lib/http_router/route.rb', line 114 def delete; request_method('DELETE'); end |
#get ⇒ Object
112 |
# File 'lib/http_router/route.rb', line 112 def get; request_method('GET'); end |
#head ⇒ Object
115 |
# File 'lib/http_router/route.rb', line 115 def head; request_method('HEAD'); end |
#host(*host) ⇒ Object
62 63 64 |
# File 'lib/http_router/route.rb', line 62 def host(*host) add_to_contitions(:host, host) end |
#matching(matchers) ⇒ Object
79 80 81 82 |
# File 'lib/http_router/route.rb', line 79 def matching(matchers) @matches_with.merge!(matchers.is_a?(Array) ? Hash[*matchers] : matchers) self end |
#matching_path(params, other_hash = nil) ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/http_router/route.rb', line 162 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 |
#name(n) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/http_router/route.rb', line 51 def name(n) @named = n @router.named_routes[n] << self @router.named_routes[n].sort!{|r1, r2| r2.significant_variable_names.size <=> r1.significant_variable_names.size } self end |
#options ⇒ Object
116 |
# File 'lib/http_router/route.rb', line 116 def ; request_method('OPTIONS'); end |
#partial(match_partially = true) ⇒ Object
40 41 42 43 |
# File 'lib/http_router/route.rb', line 40 def partial(match_partially = true) @match_partially = match_partially self end |
#patch ⇒ Object
117 |
# File 'lib/http_router/route.rb', line 117 def patch; request_method('PATCH'); end |
#post ⇒ Object
111 |
# File 'lib/http_router/route.rb', line 111 def post; request_method('POST'); end |
#process_opts ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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) @needed_keys = significant_variable_names - @default_values.keys end |
#put ⇒ Object
113 |
# File 'lib/http_router/route.rb', line 113 def put; request_method('PUT'); end |
#redirect(path, status = 302) ⇒ Object
Sets the destination of this route to redirect to an arbitrary URL.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/http_router/route.rb', line 90 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 |
#request_method(*method) ⇒ Object
58 59 60 |
# File 'lib/http_router/route.rb', line 58 def request_method(*method) add_to_contitions(:request_method, method) end |
#scheme(*scheme) ⇒ Object
66 67 68 |
# File 'lib/http_router/route.rb', line 66 def scheme(*scheme) add_to_contitions(:scheme, scheme) end |
#significant_variable_names ⇒ Object
158 159 160 |
# File 'lib/http_router/route.rb', line 158 def significant_variable_names @significant_variable_names ||= @original_path.nil? ? [] : @original_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.
102 103 104 105 106 107 108 109 |
# File 'lib/http_router/route.rb', line 102 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) @dest = dest || dest2 add_path_to_tree self end |
#to_s ⇒ Object
173 174 175 |
# File 'lib/http_router/route.rb', line 173 def to_s "#<HttpRouter:Route #{object_id} @original_path=#{@original_path.inspect} @conditions=#{@conditions.inspect} @arbitrary=#{@arbitrary.inspect}>" end |
#trace ⇒ Object
118 |
# File 'lib/http_router/route.rb', line 118 def trace; request_method('TRACE'); end |
#url(*args) ⇒ Object
132 133 134 135 |
# File 'lib/http_router/route.rb', line 132 def url(*args) result, extra_params = url_with_params(*args) append_querystring(result, extra_params) end |
#url_args_processing(args) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/http_router/route.rb', line 149 def url_args_processing(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 result, params = yield args, mount_point = router.url_mount && router.url_mount.url() mount_point ? [File.join(mount_point, result), params] : [result, params] end |
#url_with_params(*a) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/http_router/route.rb', line 141 def url_with_params(*a) url_args_processing(a) do |args, | path = args.empty? ? matching_path() : matching_path(args, ) raise InvalidRouteException unless path path.url(args, ) end end |
#user_agent(*user_agent) ⇒ Object
70 71 72 |
# File 'lib/http_router/route.rb', line 70 def user_agent(*user_agent) add_to_contitions(:user_agent, user_agent) end |