Class: Rango::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/rango/rack/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

Returns a new instance of Request.

Parameters:

  • env (Hash)

    Rack environment.

Since:

  • 0.0.1



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

#envHash (readonly)

Returns Original Rack environment.

Returns:

  • (Hash)

    Original Rack environment.

Since:

  • 0.0.1



49
50
51
# File 'lib/rango/rack/request.rb', line 49

def env
  @env
end

#pathHash (readonly)

@example: blog/post/rango-released

Returns:

  • (Hash)

    Original Rack environment.

Since:

  • 0.0.1



54
55
56
# File 'lib/rango/rack/request.rb', line 54

def path
  @path
end

Instance Method Details

#base_urlObject



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

#cookiesObject



106
107
108
# File 'lib/rango/rack/request.rb', line 106

def cookies
  super.tap { |cookies| cookies.delete("rack.session") }.symbolize_keys
end

#domainString

@example: “101ideas.cz” FIXME: what about .co.uk? Rewrite in the same way as subdomains

Returns:

Since:

  • 0.0.1



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

#formObject



110
111
112
# File 'lib/rango/rack/request.rb', line 110

def form
  ParamsMixin.convert(env["rack.request.form_hash"] || Hash.new)
end

#GETObject



68
69
70
# File 'lib/rango/rack/request.rb', line 68

def GET
  ParamsMixin.convert(super)
end

#paramsObject



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

#POSTObject



72
73
74
# File 'lib/rango/rack/request.rb', line 72

def POST
  ParamsMixin.convert(super)
end

#PUTObject



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

#sessionObject



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”

Returns:

  • (String)

    Subdomain name.

Since:

  • 0.0.1



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

#tldString

@example: “cz”

Returns:

  • (String)

    Top level domain.

Since:

  • 0.0.1



123
124
125
# File 'lib/rango/rack/request.rb', line 123

def tld
  host.match(/^localhost/) ? nil : host.split(".").last
end