Class: Mack::Request

Inherits:
Rack::Request show all
Defined in:
lib/mack/controller/request.rb,
lib/mack/controller/request/parameters.rb,
lib/mack/controller/request/date_time_parameter.rb

Defined Under Namespace

Classes: DateTimeParameter, Parameters, UploadedFile

Constant Summary

Constants inherited from Rack::Request

Rack::Request::FORM_DATA_MEDIA_TYPES

Instance Attribute Summary collapse

Attributes inherited from Rack::Request

#env

Instance Method Summary collapse

Methods inherited from Rack::Request

#GET, #POST, #[], #[]=, #accept_encoding, #body, #content_charset, #content_length, #content_type, #cookies, #delete?, #form_data?, #fullpath, #get?, #head?, #host, #ip, #media_type, #media_type_params, #path_info, #path_info=, #port, #post?, #put?, #query_string, #referer, #request_method, #scheme, #script_name, #script_name=, #url, #values_at, #xhr?

Constructor Details

#initialize(env) ⇒ Request

:nodoc:



10
11
12
# File 'lib/mack/controller/request.rb', line 10

def initialize(env) # :nodoc:
  super(env)
end

Instance Attribute Details

#sessionObject

Gives access to the session. See Mack::Session for more information.



15
16
17
# File 'lib/mack/controller/request.rb', line 15

def session
  @session
end

Instance Method Details

#file(key) ⇒ Object

Returns a Mack::Request::UploadedFile object.



44
45
46
47
48
# File 'lib/mack/controller/request.rb', line 44

def file(key)
  ivar_cache("file_#{key}") do
    Mack::Request::UploadedFile.new(params[key] ||= {})
  end
end

#full_hostObject

Examples:

http://example.org
https://example.org
http://example.org:8080


54
55
56
57
58
59
60
61
62
# File 'lib/mack/controller/request.rb', line 54

def full_host
  u = self.scheme.dup
  u << "://"
  u << self.host.dup
  unless self.port == 80 || self.port == 443
    u << ":#{self.port}"
  end
  u
end

#full_host_with_portObject

Examples:

http://example.org:80
https://example.org:443
http://example.org:8080


79
80
81
82
83
84
# File 'lib/mack/controller/request.rb', line 79

def full_host_with_port
  unless full_host.match(/:#{self.port}/)
    return full_host + ":#{self.port}"
  end
  return full_host
end

#merge_params(opts = {}) ⇒ Object

Merges another Hash with the parameters for this request.



18
19
20
# File 'lib/mack/controller/request.rb', line 18

def merge_params(opts = {})
  parse_params(opts)
end

#paramsObject

Gives access to the request parameters. This includes ‘get’ parameters, ‘post’ parameters as well as parameters from the routing process. The parameter will also be ‘unescaped’ when it is returned.

Example:

uri: '/users/1?foo=bar'
route: '/users/:id' => {:controller => 'users', :action => 'show'}
parameters: {:controller => 'users', :action => 'show', :id => 1, :foo => "bar"}


30
31
32
33
34
35
36
# File 'lib/mack/controller/request.rb', line 30

def params
  unless @mack_params
    @mack_params = Mack::Request::Parameters.new
    parse_params(original_parameters)
  end
  @mack_params
end

#params=(p) ⇒ Object

:nodoc:



38
39
40
41
# File 'lib/mack/controller/request.rb', line 38

def params=(p) # :nodoc:
  @mack_params = Mack::Request::Parameters.new
  parse_params(p)
end

#subdomains(tld_length = 1) ⇒ Object

Returns all the subdomains as an array, so [“dev”, “www”] would be returned for “dev.www.mackframework.com”. You can specify a different tld_length, such as 2 to catch [“www”] instead of [“www”, “mackframework”] in “www.mackframework.co.uk”.

Thanks Ruby on Rails for this.



69
70
71
72
73
# File 'lib/mack/controller/request.rb', line 69

def subdomains(tld_length = 1)
  return [] unless named_host?(host)
  parts = host.split('.')
  parts[0..-(tld_length+2)]
end