Class: Marcopolo::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/marcopolo/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/marcopolo/middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/marcopolo/middleware.rb', line 7

def call(env)
  req = Rack::Request.new(env)

  req_headers = env.select {|k,v| k.start_with? 'HTTP_'}
    .collect {|pair| [pair[0].sub(/^HTTP_/, '').split('_').map(&:titleize).join('-'), pair[1]]}
    .sort

  req_hash = {
    "REQUEST" => "",
    "Remote Address" => req.ip,
    "Request URL" => req.url,
    "Request Method" => req.request_method,
    "REQUEST HEADERS" => ""
  }

  req_headers.to_a.each {|i| req_hash["\t" + i.first] = i.last }

  req_hash.merge!({
    "Request Body" => req.body.gets
  })

  Marcopolo.log req_hash.to_a.map {|o| o.join(': ') }.join("\n") + "\n"

  status, headers, response = @app.call(env)

  resp_hash = {
    "RESPONSE" => "",
    "Response Status" => response.status,
    "Response Headers" => ""
  }

  response.headers.to_a.each {|i| resp_hash["\t" + i.first] = i.last }

  resp_hash.merge!({
    "Response Body" => response.body
  })

  Marcopolo.log resp_hash.to_a.map {|o| o.join(': ') }.join("\n") + "\n"

  return [status, headers, response]
end