Module: Kernul::Unracker

Defined in:
lib/kernul/unracker.rb

Class Method Summary collapse

Class Method Details

.generate_url(env) ⇒ Object

Recreates the request URL from the Rack environment data.



31
32
33
34
35
36
37
38
39
# File 'lib/kernul/unracker.rb', line 31

def self.generate_url(env)
  url = env['rack.url_scheme'] + "://"
  url << env['SERVER_NAME']
  url << ":" + env["SERVER_PORT"]
  url << env['SCRIPT_NAME']
  url << env['PATH_INFO'] unless env['PATH_INFO'].nil?
  url << "?" << env['QUERY_STRING'] unless env['QUERY_STRING'].empty?
  url
end

.unrack_headers(env) ⇒ Object

Removes all HTTP headers from the Rack environment data and returns them as a hash.



43
44
45
46
47
48
# File 'lib/kernul/unracker.rb', line 43

def self.unrack_headers(env)
  hash = {}
  http_headers = env.select { |key, value| key[0..4] == "HTTP_" }
  http_headers.each { |t| hash[t[0].dup[5..-1]] = t[1] }
  hash
end

.unrack_request(env) ⇒ Object

Removes all the important data from the Rack request and returns a RestClient::Request with that data for easy access.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kernul/unracker.rb', line 6

def self.unrack_request(env)
  url = generate_url(env)
  
  if ['GET', 'POST', 'PUT', 'DELETE'].include?(env['REQUEST_METHOD'])
    method = env['REQUEST_METHOD']
  else
    method = 'GET'
  end
  
  headers = unrack_headers(env)
  payload = env['rack.input']
  
  if env['HTTP_AUTHORIZATION']
    auth = env['HTTP_AUTHORIZATION'].split(" ")[1].unpack("m*").first.split(/:/, 2)
  else
    auth = ["",""]
  end

  user = auth.first
  password = auth.last

  RestClient::Request.new(:method => method, :url => url, :headers => headers, :user => user, :password => password, :payload => payload)
end