Class: Rango::Request

  • Rack::Request
    • Rango::Request

Attributes

Instance Attributes

env [RW] public

Returns: Hash

path [RW] public

Returns: Hash

Constructor Summary

public initialize(env)

Meta Tags

Parameters:

[Hash] env

Rack environment.

Since:

0.0.1

[View source]


58
59
60
61
62
63
64
65
# 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.length > 1 # so let the / just if the path is only /
  @method = env["REQUEST_METHOD"].downcase
  self.extend_session
end

Public Visibility

Public Instance Method Summary

#ajax?
#base_url
#cookies
#DELETE
#domain

Returns: String

#extend_session

TODO: use extend method.

#form
#GET
#params
#POST
#PUT
#session
#subdomain

Returns: String

#tld

Returns: String

Public Instance Method Details

ajax?

public ajax?
[View source]


120
121
122
# File 'lib/rango/rack/request.rb', line 120

def ajax?
  env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
end

base_url

public base_url
[View source]


158
159
160
161
162
163
164
# File 'lib/rango/rack/request.rb', line 158

def base_url
  require "uri"
  fragments = URI.split("http://localhost:2000/foo/bar?q=foo")
  fragments = fragments[0..4]
  5.times { fragments.push(nil) }
  URI::HTTP.new(*fragments).to_s
end

cookies

public cookies
[View source]


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

def cookies
  super.except("rack.session").symbolize_keys
end

DELETE

public DELETE
[View source]


94
95
96
# File 'lib/rango/rack/request.rb', line 94

def DELETE
  {}.deep_symbolize_keys.except(:_method)
end

domain

public String domain

Meta Tags

Parameters:

Returns:

[String]

Domain name.

Since:

0.0.1

[View source]


141
142
143
144
145
146
147
# File 'lib/rango/rack/request.rb', line 141

def domain
  if host.match(/^localhost/)
    return host.split(".").last
  else
    return host.split(".").last(2).join(".")
  end
end

extend_session

public extend_session

TODO: use extend method

[View source]


125
126
127
128
129
# File 'lib/rango/rack/request.rb', line 125

def extend_session
  class << session
    include Rango::Session
  end
end

form

public form
[View source]


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

def form
  data = env["rack.request.form_hash"] || Hash.new
  data.deep_symbolize_keys.except(:_method)
end

GET

public GET
[View source]


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

def GET
  super.deep_symbolize_keys.except(:_method)
end

params

public params
[View source]


98
99
100
101
102
103
104
# File 'lib/rango/rack/request.rb', line 98

def params
  @params = Hash.new
  [self.GET, self.POST, self.PUT, self.DELETE].each do |data|
    @params.merge!(data) if data.respond_to?(:merge) # Hash, Mash and friends
  end
  @params.deep_symbolize_keys.except(:msg, :_method)
end

POST

public POST
[View source]


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

def POST
  super.deep_symbolize_keys.except(:_method) if self.post?
end

PUT

public PUT
[View source]


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rango/rack/request.rb', line 75

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
      @env["rack.request.form_hash"].deep_symbolize_keys.except(:_method)
    else
      {}
    end
  end
end

session

public session
[View source]


115
116
117
118
# File 'lib/rango/rack/request.rb', line 115

def session
  # Rango.logger.inspect(session: @env['rack.session'])
  @env['rack.session'] ||= {}
end

subdomain

public String subdomain

Meta Tags

Parameters:

Returns:

[String]

Subdomain name.

Since:

0.0.1

[View source]


152
153
154
155
156
# File 'lib/rango/rack/request.rb', line 152

def subdomain
  parts = host.split(".")
  index = parts.index(self.domain)
  parts[0..(index - 1)]
end

tld

public String tld

Meta Tags

Parameters:

Returns:

[String]

Top level domain.

Since:

0.0.1

[View source]


134
135
136
# File 'lib/rango/rack/request.rb', line 134

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