Module: Fastr::HTTP

Defined in:
lib/fastr/http.rb

Class Method Summary collapse

Class Method Details

.build_query_string(params) ⇒ String

Builds a query string of parameters.

Parameters:

  • params (Hash)

    The parameters to put in the query string.

Returns:

  • (String)

    Query string with proper URL encoding for values.



25
26
27
28
29
30
31
# File 'lib/fastr/http.rb', line 25

def self.build_query_string(params)
  qs = []
  params.each do |k,v|
    qs << "#{k}=#{CGI::escape(v)}"
  end
  qs.join('&')
end

.method?(env, method) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/fastr/http.rb', line 33

def self.method?(env, method)
  return false if not env['REQUEST_METHOD']
  return env['REQUEST_METHOD'].downcase.to_sym == method
end

.parse_cookies(env) ⇒ Hash

Parses the HTTP cookie.

Parameters:

  • env (Hash)

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fastr/http.rb', line 42

def self.parse_cookies(env)
  if env.has_key? "HTTP_COOKIE"
    cookies = env['HTTP_COOKIE'].split(';')
    c = {}
    cookies.each do |cookie|
      info = cookie.strip.split("=")
      if info.length == 2
        c[info[0].strip] = info[1].strip 
      end
    end
    c
  else
    {}
  end
end

.parse_query_string(qs) ⇒ Hash

Parses the query string.

Parameters:

Returns:

  • (Hash)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fastr/http.rb', line 8

def self.parse_query_string(qs)
  params = {}
  return params if not qs
  CGI::parse(qs).each do |k,v|
    if v.length == 1
      params[k] = v[0]
    else
      params[k] = v
    end
  end
  return params
end