Module: Innate::Helper::Redirect
- Defined in:
- /home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/helper/redirect.rb
Instance Method Summary (collapse)
-
- (Object) raw_redirect(target, options = {}, &block)
-
- (Object) redirect(target, options = {}) {|uri| ... }
+target+ should be anything responding to #to_s.
-
- (Object) redirect_body(target)
-
- (Object) redirect_referrer(fallback = Innate.options.prefix)
(also: #redirect_referer)
-
- (Object) respond(body, status = 200, header = {})
-
- (Object) respond!(body, status = 200, header = {})
Instance Method Details
- (Object) raw_redirect(target, options = {}, &block)
59 60 61 62 63 64 65 66 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/helper/redirect.rb', line 59 def raw_redirect(target, = {}, &block) header = response.header.merge('Location' => target.to_s) status = [:status] || 302 body = [:body] || redirect_body(target) Log.debug "Redirect to: #{target}" throw(:redirect, Response.new(body, status, header, &block)) end |
- (Object) redirect(target, options = {}) {|uri| ... }
+target+ should be anything responding to #to_s. To check or modify the URI the redirect will go to you may pass a block, the result value of the block is ignored:
redirect("/"){|uri| uri.scheme = 'http' } redirect("/"){|uri| uri.host = 'secure.com' if uri.scheme =~ /s/ }
+options+ may contain:
:scheme => "http" | "https" | "ftp" | ... :host => "localhost" | "foo.com" | "123.123.123.123" | ... :port => 7000 | "80" | 80 | ...
:status => 302 | 300 | 303 | ... :body => "This is a redirect, hold on while we teleport" | ...
:raw! => true | false | nil | ...
Note that all options are optional and you may just pass a +target+.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/helper/redirect.rb', line 38 def redirect(target, = {}) target = target.to_s case target when /^http/, /^\// uri = URI(target) else uri = URI("/#{target}") end uri.scheme ||= [:scheme] || request.scheme uri.host ||= [:host] || request.host uri.port ||= [:port] || request.port uri = URI(uri.to_s) yield(uri) if block_given? raw_redirect(uri, ) end |
- (Object) redirect_body(target)
68 69 70 71 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/helper/redirect.rb', line 68 def redirect_body(target) "You are being redirected, please follow this link to: " + "<a href='#{target}'>#{h target}</a>!" end |
- (Object) redirect_referrer(fallback = Innate.options.prefix) Also known as: redirect_referer
73 74 75 76 77 78 79 80 81 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/helper/redirect.rb', line 73 def redirect_referrer(fallback = Innate..prefix) if (referer = request.env['HTTP_REFERER']) && (url = request.url) referer_uri, request_uri = URI(referer), URI(url) redirect(referer) unless referer_uri == request_uri end redirect(fallback) end |
- (Object) respond(body, status = 200, header = {})
4 5 6 7 8 9 10 11 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/helper/redirect.rb', line 4 def respond(body, status = 200, header = {}) response.write body response.status = status header['Content-Type'] ||= Response.mime_type header.each{|key, value| response[key] = value } throw(:respond, response) end |
- (Object) respond!(body, status = 200, header = {})
13 14 15 16 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/helper/redirect.rb', line 13 def respond!(body, status = 200, header = {}) header['Content-Type'] ||= Response.mime_type throw(:respond, Response.new(body, status, header)) end |