Class: Mack::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/mack/controller/request.rb

Defined Under Namespace

Classes: DateTimeParameter, Parameters, UploadedFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

:nodoc:



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

def initialize(env) # :nodoc:
  super(env)
  @mack_params = Mack::Request::Parameters.new
  parse_params(rack_params)
end

Instance Attribute Details

#sessionObject

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



43
44
45
# File 'lib/mack/controller/request.rb', line 43

def session
  @session
end

Instance Method Details

#file(key) ⇒ Object

Returns a Mack::Request::UploadedFile object.



101
102
103
104
105
# File 'lib/mack/controller/request.rb', line 101

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


49
50
51
52
53
54
55
56
57
# File 'lib/mack/controller/request.rb', line 49

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


74
75
76
77
78
79
# File 'lib/mack/controller/request.rb', line 74

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.



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

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"}


89
90
91
# File 'lib/mack/controller/request.rb', line 89

def params
  @mack_params
end

#params=(p) ⇒ Object

:nodoc:



93
94
95
96
# File 'lib/mack/controller/request.rb', line 93

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.



64
65
66
67
68
# File 'lib/mack/controller/request.rb', line 64

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