public
Fork of markbates/mack
Description: A Ruby web application framework
Homepage: http://www.mackframework.com
Clone URL: git://github.com/juretta/mack.git
Search Repo:
markbates (author)
Wed Mar 26 16:41:21 -0700 2008
commit  6744fba6ffdf0debeb7b65d2fc7f68457d17e5c9
tree    2a66df25cdb937ea0a0302fd4f9a1d588a374230
parent  f84a519e696f2a2631facf16c75233d25f40d7af
mack / lib / sea_level / request.rb
100644 83 lines (73 sloc) 2.255 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module Mack
  class Request < Rack::Request
    
    def initialize(env) # :nodoc:
      super(env)
      @mack_params = {}
      parse_params(rack_params)
    end
    
    alias_method :rack_params, :params # :nodoc:
    
    # Returns all parameters associated with this request.
    def all_params
      @mack_params
    end
    
    # Merges another Hash with the parameters for this request.
    def merge_params(opts = {})
      parse_params(opts)
    end
    
    # Gives access to the session. See Mack::Session for more information.
    attr_accessor :session
    
    # Examples:
    # http://example.org
    # https://example.org
    # http://example.org:8080
    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
    
    # Examples:
    # http://example.org:80
    # https://example.org:443
    # http://example.org:8080
    def full_host_with_port
      full_host << ":#{self.port}"
    end
    
    # 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"}
    def params(key)
      p = (@mack_params[key.to_sym] || @mack_params[key.to_s])
      unless p.nil?
        p = p.to_s if p.is_a?(Symbol)
        if p.is_a?(String)
          p = p.to_s.uri_unescape
        elsif p.is_a?(Hash)
          p.each_pair {|k,v| p[k] = v.to_s.uri_unescape}
        end
      end
      p
    end
    
    private
    def parse_params(ps)
      ps.each_pair do |k, v|
        if k.to_s.match(/.+\[.+\]/)
          nv = k.to_s.match(/.+\[(.+)\]/).captures.first
          nk = k.to_s.match(/(.+)\[.+\]/).captures.first
          @mack_params[nk.to_sym] = {} if @mack_params[nk.to_sym].nil?
          @mack_params[nk.to_sym].merge!(nv.to_sym => v)
        else
          @mack_params[k.to_sym] = v
        end
      end
    end
    
  end
end