Class: Rango::Request
- Inherits:
-
Rack::Request
- Object
- Rack::Request
- Rango::Request
- Defined in:
- lib/rango/rack/request.rb
Instance Attribute Summary collapse
-
#env ⇒ Hash
readonly
Original Rack environment.
-
#path ⇒ Hash
readonly
@example: blog/post/rango-released.
Instance Method Summary collapse
- #base_url ⇒ Object
- #cookies ⇒ Object
-
#domain ⇒ String
@example: “101ideas.cz” FIXME: what about .co.uk? Rewrite in the same way as subdomains.
- #form ⇒ Object
- #GET ⇒ Object
-
#initialize(env) ⇒ Request
constructor
A new instance of Request.
- #params ⇒ Object
- #POST ⇒ Object
- #PUT ⇒ Object
- #session ⇒ Object
-
#subdomains(tld_length = 1) ⇒ String
@example: “blog” or “user.blog”.
-
#tld ⇒ String
@example: “cz”.
Constructor Details
#initialize(env) ⇒ Request
Returns a new instance of Request.
58 59 60 61 62 63 64 65 66 |
# File 'lib/rango/rack/request.rb', line 58 def initialize(env) @env = env # /path will be transformed to path/ @path = env["PATH_INFO"] @path.chomp!("/") if @path && @path.length > 1 # so let the / just if the path is only / @method = (env["REQUEST_METHOD"] || String.new).downcase session.extend(Session) Rango.logger.debug("Session: #{@env['rack.session'].inspect}") end |
Instance Attribute Details
#env ⇒ Hash (readonly)
Returns Original Rack environment.
49 50 51 |
# File 'lib/rango/rack/request.rb', line 49 def env @env end |
#path ⇒ Hash (readonly)
@example: blog/post/rango-released
54 55 56 |
# File 'lib/rango/rack/request.rb', line 54 def path @path end |
Instance Method Details
#base_url ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'lib/rango/rack/request.rb', line 152 def base_url url = scheme + "://" url << host if scheme == "https" && port != 443 || scheme == "http" && port != 80 url << ":#{port}" end url end |
#cookies ⇒ Object
106 107 108 |
# File 'lib/rango/rack/request.rb', line 106 def super.tap { || .delete("rack.session") }.symbolize_keys end |
#domain ⇒ String
@example: “101ideas.cz” FIXME: what about .co.uk? Rewrite in the same way as subdomains
131 132 133 134 135 136 137 |
# File 'lib/rango/rack/request.rb', line 131 def domain if host.match(/^localhost/) return host.split(".").last else return host.split(".").last(2).join(".") end end |
#form ⇒ Object
110 111 112 |
# File 'lib/rango/rack/request.rb', line 110 def form ParamsMixin.convert(env["rack.request.form_hash"] || Hash.new) end |
#GET ⇒ Object
68 69 70 |
# File 'lib/rango/rack/request.rb', line 68 def GET ParamsMixin.convert(super) end |
#params ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rango/rack/request.rb', line 95 def params @params ||= begin input = [self.GET, self.POST, self.PUT] ParamsMixin.convert( input.inject(Hash.new) do |result, hash| hash ? result.merge!(hash) : result end ) end end |
#POST ⇒ Object
72 73 74 |
# File 'lib/rango/rack/request.rb', line 72 def POST ParamsMixin.convert(super) end |
#PUT ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rango/rack/request.rb', line 76 def PUT if self.put? if @env["rack.request.form_input"].eql? @env["rack.input"] @env["rack.request.form_hash"] elsif form_data? @env["rack.request.form_input"] = @env["rack.input"] unless @env["rack.request.form_hash"] = Utils::Multipart.parse_multipart(env) @env["rack.request.form_vars"] = @env["rack.input"].read @env["rack.request.form_hash"] = Utils.parse_query(@env["rack.request.form_vars"]) @env["rack.input"].rewind if @env["rack.input"].respond_to?(:rewind) end ParamsMixin.convert(@env["rack.request.form_hash"]) else {} end end end |
#session ⇒ Object
114 115 116 |
# File 'lib/rango/rack/request.rb', line 114 def session @env['rack.session'] ||= {} end |
#subdomains(tld_length = 1) ⇒ String
@example: “blog” or “user.blog”
142 143 144 145 146 147 148 149 150 |
# File 'lib/rango/rack/request.rb', line 142 def subdomains(tld_length = 1) # we set tld_length to 1, use 2 for co.uk or similar # cache the result so we only compute it once. @env['rack.env.subdomains'] ||= begin # check if the current host is an IP address, if so return an empty array return [] if (host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) host.split('.')[0...(1 - tld_length - 2)] # pull everything except the TLD end end |
#tld ⇒ String
@example: “cz”
123 124 125 |
# File 'lib/rango/rack/request.rb', line 123 def tld host.match(/^localhost/) ? nil : host.split(".").last end |