Module: HttpRouter::RouteHelper
- Defined in:
- lib/http_router/route_helper.rb
Instance Method Summary collapse
- #add_default_values(hash) ⇒ Object
- #add_match_with(matchers) ⇒ Object
- #add_other_host(hosts) ⇒ Object
- #add_path(path) ⇒ Object
- #add_request_method(methods) ⇒ Object
- #delete ⇒ Object
- #get ⇒ Object
- #head ⇒ Object
- #name(name = nil) ⇒ Object
- #path ⇒ Object
- #path=(path) ⇒ Object
- #post ⇒ Object
- #process_opts(opts) ⇒ Object
- #put ⇒ Object
- #redirect(path, status = 302) ⇒ 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, &dest_block) ⇒ Object
Instance Method Details
#add_default_values(hash) ⇒ Object
26 27 28 29 |
# File 'lib/http_router/route_helper.rb', line 26 def add_default_values(hash) @default_values ||= {} @default_values.merge!(hash) end |
#add_match_with(matchers) ⇒ Object
31 32 33 34 |
# File 'lib/http_router/route_helper.rb', line 31 def add_match_with(matchers) @match_with ||= {} @match_with.merge!(matchers) end |
#add_other_host(hosts) ⇒ Object
36 37 38 |
# File 'lib/http_router/route_helper.rb', line 36 def add_other_host(hosts) (@other_hosts ||= []).concat(hosts) end |
#add_path(path) ⇒ Object
40 41 42 |
# File 'lib/http_router/route_helper.rb', line 40 def add_path(path) (@paths ||= []) << path end |
#add_request_method(methods) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/http_router/route_helper.rb', line 44 def add_request_method(methods) @request_methods ||= Set.new methods = [methods] unless methods.is_a?(Array) methods.each do |method| method = method.to_s.upcase raise unless Route::VALID_HTTP_VERBS.include?(method) @request_methods << method end end |
#delete ⇒ Object
100 101 102 103 |
# File 'lib/http_router/route_helper.rb', line 100 def delete add_request_method "DELETE" self end |
#get ⇒ Object
85 86 87 88 |
# File 'lib/http_router/route_helper.rb', line 85 def get add_request_method "GET" self end |
#head ⇒ Object
80 81 82 83 |
# File 'lib/http_router/route_helper.rb', line 80 def head add_request_method "HEAD" self end |
#name(name = nil) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/http_router/route_helper.rb', line 17 def name(name = nil) if name self.name = name self else @name end end |
#path ⇒ Object
3 4 5 |
# File 'lib/http_router/route_helper.rb', line 3 def path @route.path_for_generation end |
#path=(path) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/http_router/route_helper.rb', line 7 def path=(path) @original_path = path if path.respond_to?(:[]) and path[/[^\\]\*$/] @match_partially = true @path_for_generation = path[0..path.size - 2] else @path_for_generation = path end end |
#post ⇒ Object
90 91 92 93 |
# File 'lib/http_router/route_helper.rb', line 90 def post add_request_method "POST" self end |
#process_opts(opts) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/http_router/route_helper.rb', line 54 def process_opts(opts) if opts[:conditions] opts.merge!(opts[:conditions]) opts.delete(:conditions) end opts.each do |k, v| if respond_to?(:"#{k}=") send(:"#{k}=", v) elsif respond_to?(:"add_#{k}") send(:"add_#{k}", v) else add_match_with(k => v) end end end |
#put ⇒ Object
95 96 97 98 |
# File 'lib/http_router/route_helper.rb', line 95 def put add_request_method "PUT" self end |
#redirect(path, status = 302) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/http_router/route_helper.rb', line 105 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 } end |
#static(root) ⇒ Object
Sets the destination of this route to serve static files from either a directory or a single file.
116 117 118 119 120 121 122 123 124 |
# File 'lib/http_router/route_helper.rb', line 116 def static(root) @match_partially = true if File.directory?(root) to File.directory?(root) ? ::Rack::File.new(root) : proc {|env| env['PATH_INFO'] = File.basename(root) ::Rack::File.new(File.dirname(root)).call(env) } end |
#to(dest = nil, &dest_block) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/http_router/route_helper.rb', line 70 def to(dest = nil, &dest_block) @dest = dest || dest_block || raise("you didn't specify a destination") if @dest.respond_to?(:url_mount=) urlmount = UrlMount.new(@path_for_generation, @default_values || {}) # TODO url mount should accept nil here. urlmount.url_mount = @router.url_mount if @router.url_mount @dest.url_mount = urlmount end self end |