File: request.rb

package info (click to toggle)
ruby-ramaze 2012.12.08-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,060 kB
  • ctags: 1,200
  • sloc: ruby: 10,446; makefile: 8
file content (126 lines) | stat: -rw-r--r-- 4,008 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#          Copyright (c) 2009 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the MIT license.
module Ramaze
  ##
  # The purpose of this class is to act as a simple wrapper for Rack::Request
  # and provide some convinient methods for our own use.
  class Request < Innate::Request
    ##
    # you can access the original @request via this method_missing,
    # first it tries to match your method with any of the HTTP parameters
    # then, in case that fails, it will relay to @request
    def method_missing meth, *args
      key = meth.to_s.upcase
      return env[key] if env.has_key?(key)
      super
    end

    ##
    # Sets any arguments passed as @instance_variables for the current action.
    #
    # Usage:
    #
    #     request.params # => {'name' => 'manveru', 'q' => 'google', 'lang' => 'de'}
    #     request.to_ivs(:name, :q)
    #
    #     @q    # => 'google'
    #     @name # => 'manveru'
    #     @lang # => nil
    #
    def to_instance_variables(*args)
      instance = Current.action.instance
      args.each do |arg|
        next unless value = self[arg]
        instance.instance_variable_set("@#{arg}", value)
      end
    end
    alias to_ivs to_instance_variables

    def accept_charset(default = 'UTF-8')
      return default unless charsets = env['HTTP_ACCEPT_CHARSET']
      charset = charsets.split(',', 2).first
      charset == '*' ? default : charset
    end

    ##
    # Try to find out which languages the client would like to have and sort
    # them by weight, (most wanted first).
    #
    # Returns and array of locales from env['HTTP_ACCEPT_LANGUAGE].
    # e.g. ["fi", "en", "ja", "fr", "de", "es", "it", "nl", "sv"]
    #
    # Usage:
    #
    #     request.accept_language # => ['en-us', 'en', 'de-at', 'de']
    #
    # @param [String #to_s] string the value of HTTP_ACCEPT_LANGUAGE
    # @return [Array] list of locales
    # @see Request#accept_language_with_weight
    # @author manveru
    #
    def accept_language(string = env['HTTP_ACCEPT_LANGUAGE'])
      return [] unless string

      accept_language_with_weight(string).map{|lang, weight| lang }
    end
    alias locales accept_language

    ##
    # Transform the HTTP_ACCEPT_LANGUAGE header into an Array with:
    #
    #     [[lang, weight], [lang, weight], ...]
    #
    # This algorithm was taken and improved from the locales library.
    #
    # Usage:
    #
    #     request.accept_language_with_weight
    #     # => [["en-us", 1.0], ["en", 0.8], ["de-at", 0.5], ["de", 0.3]]
    #
    # @param [String #to_s] string the value of HTTP_ACCEPT_LANGUAGE
    # @return [Array] array of [lang, weight] arrays
    # @see Request#accept_language
    # @author manveru
    #
    def accept_language_with_weight(string = env['HTTP_ACCEPT_LANGUAGE'])
      string.to_s.gsub(/\s+/, '').split(',').
            map{|chunk|        chunk.split(';q=', 2) }.
            map{|lang, weight| [lang, weight ? weight.to_f : 1.0] }.
        sort_by{|lang, weight| -weight }
    end

    INTERESTING_HTTP_VARIABLES =
      (/USER|HOST|REQUEST|REMOTE|FORWARD|REFER|PATH|QUERY|VERSION|KEEP|CACHE/)

    # Interesting HTTP variables from env
    def http_variables
      env.reject{|key, value| key.to_s !~ INTERESTING_HTTP_VARIABLES }
    end
    alias http_vars http_variables

    REQUEST_STRING_FORMAT = "#<%s params=%p cookies=%p env=%p>"

    def to_s
      REQUEST_STRING_FORMAT % [self.class, params, cookies, http_variables]
    end
    alias inspect to_s

    # Pretty prints current action with parameters, cookies and enviroment
    # variables.
    def pretty_print(pp)
      pp.object_group(self) do
        group = {
          'params'  => params,
          'cookies' => cookies,
          'env'     => http_variables
        }

        group.each do |name, hash|
          pp.breakable
          pp.text " @#{name}="
          pp.nest(name.size + 3){ pp.pp_hash(hash) }
        end
      end
    end
  end # Request
end # Ramaze