Class: Mocapi::ShowRequest

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

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ ShowRequest

Returns a new instance of ShowRequest.



49
50
51
# File 'lib/mocapi.rb', line 49

def initialize(app = nil)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mocapi.rb', line 53

def call(env)
  request = Rack::Request.new(env)
  response = if @app
    @app.call(env)
  else
    Rack::Response.new(env)
  end

  show_request_detail  request
  show_response_detail response

  response
end

#show(str) ⇒ Object



101
102
103
# File 'lib/mocapi.rb', line 101

def show(str)
  STDOUT.puts str
end

#show_request_detail(request) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mocapi.rb', line 67

def show_request_detail(request)
  show <<-EOS

Request URI  : #{request.fullpath}
HTTP Method  : #{request.request_method}
Path         : #{request.path}
Content Type : #{request.content_type}
Query        : #{request.query_string}
Request Body :
#{request.body.read}
  EOS

  request.body.rewind
end

#show_response_detail(response) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mocapi.rb', line 82

def show_response_detail(response)
  status_code, headers, body = *response
  max_length = (headers.keys + ['Response Body']).map(&:length).max

  show <<-EOS
#{'Status Code'.ljust(max_length)} : #{status_code}
  EOS

  headers.each do |key, value|
    show "    #{key.ljust(max_length)} : #{value}"
  end

  show <<-EOS
#{'Response Body'.ljust(max_length)} :
#{body.join('\n')}

  EOS
end