Skip to content
Browse files

Added methods that print the Request and Response:

head!, get!, post!, put!, delete!
  • Loading branch information...
1 parent 6904e36 commit a5a6b75b11e1ee2b7bcd30d931d4b94884b561e7 Anders Törnqvist committed May 15, 2011
Showing with 86 additions and 8 deletions.
  1. +2 −1 lib/resto/request/base.rb
  2. +84 −7 lib/resto/request/factory.rb
View
3 lib/resto/request/base.rb
@@ -16,7 +16,8 @@ class Base
include Resto::Request::Uri
include Resto::Request::Option
- delegate :head, :get, :post, :put, :delete, :to => :@request
+ delegate :head, :head!, :get, :get!, :post, :post!,
+ :put, :put!, :delete, :delete!, :to => :@request
def initialize(request=Resto::Request::Factory)
@request_klass = request
View
91 lib/resto/request/factory.rb
@@ -13,40 +13,76 @@ class Factory
def initialize(request)
@request = request
+ @debug_request = false
+ end
+
+ def head!
+ @debug_request = true
+ head.tap { |response| print_response(response) }
end
def head
http.start do |http|
- http.request(Net::HTTP::Head.new(composed_path, composed_headers))
+ request = Net::HTTP::Head.new(composed_path, composed_headers)
+ print_request(request)
+ http.request(request)
end
end
+ def get!
+ @debug_request = true
+ get.tap { |response| print_response(response) }
+ end
+
def get
http.start do |http|
- http.request(Net::HTTP::Get.new(composed_path, composed_headers))
+ request = Net::HTTP::Get.new(composed_path, composed_headers)
+ print_request(request)
+ http.request(request)
end
end
+ def post!
+ @debug_request = true
+ post.tap { |response| print_response(response) }
+ end
+
def post
http.start do |http|
- http.request(Net::HTTP::Post.new(composed_path, composed_headers),
- read_body)
+ request = Net::HTTP::Post.new(composed_path, composed_headers)
+ print_request(request)
+ http.request(request, read_body)
end
end
+ def put!
+ @debug_request = true
+ put.tap { |response| print_response(response) }
+ end
+
def put
http.start do |http|
- http.request(Net::HTTP::Put.new(composed_path, composed_headers),
- read_body)
+ request = Net::HTTP::Put.new(composed_path, composed_headers)
+ print_request(request)
+ http.request(request, read_body)
end
end
+ def delete!
+ @debug_request = true
+ delete.tap { |response| print_response(response) }
+ end
+
def delete
http.start do |http|
- http.request(Net::HTTP::Delete.new(composed_path, composed_headers))
+ request = Net::HTTP::Delete.new(composed_path, composed_headers)
+ print_request(request)
+ http.request(request)
end
end
+ private
+
def http
::Net::HTTP.new(read_host, read_port).tap do |http|
@@ -61,6 +97,47 @@ def http
end
end
end
+
+ def print_request(request)
+ if @debug_request
+ url = ["#{request.method} #{scheme}://#{read_host}"]
+ url[0] += "#{request.path}:#{read_port}"
+ out(title("Request"), url, content(request))
+ end
+ end
+
+ def print_response(response)
+ if @debug_request
+ out(["\n"], title("Response"), content(response))
+ end
+ end
+
+ def title(title)
+ out = []
+ out << "\n==============================="
+ out << " #{title.upcase}"
+ out << "==============================="
+ end
+
+ def content(result)
+ out = []
+ result.each_header do |key, value|
+ if key == "status"
+ out.unshift("header['#{key}'] : #{value}")
+ else
+ out.push("header['#{key}'] : #{value}")
+ end
+ end
+ out.unshift("\n*** Header ***")
+
+ body = result.body.to_s
+ length = body.length
+ out << "\n*** Body (#{length}) ***\n#{body}"
+ end
+
+ def out(*out)
+ puts (out.fetch(0, []) + out.fetch(1, []) + out.fetch(2, [])).join("\n")
+ end
end
end
end

0 comments on commit a5a6b75

Please sign in to comment.
Something went wrong with that request. Please try again.