Class: HTTPDump
- Inherits:
-
Object
- Object
- HTTPDump
- Defined in:
- lib/http-dump.rb,
lib/http-dump/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Attribute Summary collapse
-
.output ⇒ Object
Returns the value of attribute output.
-
.output_encoding ⇒ Object
Returns the value of attribute output_encoding.
-
.quiet_format ⇒ Object
Returns the value of attribute quiet_format.
Class Method Summary collapse
- .disable!(options = {}) ⇒ Object
- .dump(options = {}, &block) ⇒ Object
- .enable!(options = {}) ⇒ Object
- .format(request_signature, response) ⇒ Object
Class Attribute Details
.output ⇒ Object
Returns the value of attribute output.
6 7 8 |
# File 'lib/http-dump.rb', line 6 def output @output end |
.output_encoding ⇒ Object
Returns the value of attribute output_encoding.
6 7 8 |
# File 'lib/http-dump.rb', line 6 def output_encoding @output_encoding end |
.quiet_format ⇒ Object
Returns the value of attribute quiet_format.
6 7 8 |
# File 'lib/http-dump.rb', line 6 def quiet_format @quiet_format end |
Class Method Details
.disable!(options = {}) ⇒ Object
17 18 19 20 21 |
# File 'lib/http-dump.rb', line 17 def disable!( = {}) WebMock.reset_callbacks WebMock.disable!() @enabled = false end |
.dump(options = {}, &block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/http-dump.rb', line 23 def dump( = {}, &block) enabled = @enabled enable!() unless enabled block.call ensure disable!() unless enabled end |
.enable!(options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/http-dump.rb', line 7 def enable!( = {}) require 'webmock' WebMock.enable!() WebMock.allow_net_connect!() WebMock.after_request do |request_signature, response| output.puts self.format(request_signature, response) end @enabled = true end |
.format(request_signature, response) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/http-dump.rb', line 35 def format(request_signature, response) res = [] res << "> #{request_signature}" res << "< #{response.status.join(' ')}" response.headers.each {|key, val| res << "< #{key}: #{val}" } if response.headers body = response.body unless body.empty? res << "<" if quiet_format && body.size > 100 head = body[0..50] res << head.split("\n")[0] res << "* ... Response body is #{body.bytesize} bytes." tail = body[-50..-1] res << tail.split("\n")[-1] else res << body end end if @output_encoding res.map{|d| d.force_encoding @output_encoding }.join("\n") else res.join("\n") end end |