Class: Mocapi::ShowRequest
- Inherits:
-
Object
- Object
- Mocapi::ShowRequest
- Defined in:
- lib/mocapi.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app = nil) ⇒ ShowRequest
constructor
A new instance of ShowRequest.
- #show(str) ⇒ Object
- #show_request_detail(request) ⇒ Object
- #show_response_detail(response) ⇒ Object
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 "\nRequest URI : \#{request.fullpath}\nHTTP Method : \#{request.request_method}\nPath : \#{request.path}\nContent Type : \#{request.content_type}\nQuery : \#{request.query_string}\nRequest Body :\n\#{request.body.read}\n EOS\n\n request.body.rewind\nend\n" |
#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 "\#{'Status Code'.ljust(max_length)} : \#{status_code}\n EOS\n\n headers.each do |key, value|\n show \" \#{key.ljust(max_length)} : \#{value}\"\n end\n\n show <<-EOS\n\#{'Response Body'.ljust(max_length)} :\n\#{body.join('\\n')}\n\n EOS\nend\n" |