Permalink
Browse files

Added a session class

  • Loading branch information...
1 parent 16d7085 commit 818fa85e3aa74188c2fac002d97e7dde375b5b4a @unders committed Aug 19, 2011
View
@@ -3,16 +3,16 @@
$LOAD_PATH << File.dirname(__FILE__) + "../../lib"
require 'resto'
-msg = "ddddddddd"
-# Resto.url('www.google.com').set_debug_output($stderr).get
-# Resto.url('www.google.com').set_debug_output($stdout).get
-# Resto.url('www.google.com').set_debug_output(msg).get
- # $stderr << msg
- $stderr << "\n"
+session = Resto.url('https://google.com').verify_none.get
+p session.code # => 301
+puts "\n"
-# puts msg.inspect
-puts Resto.url('https://google.com').set_debug_output($stderr).verify_none.get
-# puts Resto.url('https://encrypted.google.com/')
-# .set_debug_output($stderr).verify_peer.get
+session = Resto.url('https://encrypted.google.com/').verify_peer.get
+p session.code # 200
+p session.request
+p session.response
+
+puts "\nsession.print:"
+puts session.print
View
@@ -183,8 +183,8 @@ def request
Copy.request_base(@request)
end
- def response(response)
- base_response.http_response(response)
+ def response(session)
+ base_response.http_session(session)
end
def base_response
@@ -33,7 +33,10 @@ class AssertHash
def self.keys(hash, *valid_keys)
hash ||= {}
- hash = hash.inject({}) { |h, (key, value)| h[key.to_sym] = hash.delete(key); h }
+ hash = hash.inject({}) do |memo, (key, value)|
+ memo[key.to_sym] = hash.delete(key)
+ memo
+ end
known_keys = [valid_keys].flatten
unknown_keys = hash.keys - known_keys
View
@@ -0,0 +1,46 @@
+# encoding: utf-8
+
+module Resto
+ module Log
+ def self.print_request(request, settings)
+ url = ["#{settings.method} #{settings.scheme}://#{settings.read_host}"]
+ url[0] += "#{settings.composed_path}:#{settings.read_port}"
+ out(title("Request"), url, content(request, settings))
+ end
+
+ def self.print_response(response, settings)
+ out(["\n"], title("Response"), content(response, settings))
+ end
+
+ def self.title(title)
+ out = []
+ out << "\n==============================="
+ out << " #{title.upcase}"
+ out << "==============================="
+ end
+
+ def self.content(result, settings)
+ 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
+ if settings.current_formatter == Resto::Format::Json and not body.empty?
+ body = Yajl::Encoder.encode(Format::Json.decode(body),
+ { :pretty => true, :indent => " " })
+ end
+ out << "\n*** Body (#{length}) ***\n#{body}"
+ end
+
+ def self.out(*out)
+ puts (out.fetch(0, []) + out.fetch(1, []) + out.fetch(2, [])).join("\n")
+ end
+ end
+end
@@ -38,6 +38,14 @@ def url(url)
tap { parse_url(url) }
end
+ def method(symbol=nil)
+ if symbol
+ tap { @method = symbol }
+ else
+ @method
+ end
+ end
+
def host(host)
tap { @host = host }
end
@@ -2,6 +2,7 @@
require 'net/https'
require 'forwardable'
+require 'resto/session'
module Resto
module Request
@@ -14,72 +15,67 @@ class Factory
def initialize(request)
@request = request
- @debug_request = false
- end
-
- def head!
- @debug_request = true
- head.tap { |response| print_response(response) }
+ @session = Session.new(@request)
end
def head
- http.start do |http|
+ @request.method(:head)
+
+ response = http.start do |http|
request = Net::HTTP::Head.new(composed_path, composed_headers)
- print_request(request)
+ @session.add_request(request)
http.request(request)
end
- end
- def get!
- @debug_request = true
- get.tap { |response| print_response(response) }
+ @session.add_response(response)
end
def get
- http.start do |http|
+ @request.method(:get)
+
+ response = http.start do |http|
request = Net::HTTP::Get.new(composed_path, composed_headers)
- print_request(request)
+ @session.add_request(request)
http.request(request)
end
- end
- def post!
- @debug_request = true
- post.tap { |response| print_response(response) }
+ @session.add_response(response)
end
def post
- http.start do |http|
+ @request.method(:post)
+
+ response = http.start do |http|
request = Net::HTTP::Post.new(composed_path, composed_headers)
- print_request(request)
+ @session.add_request(request)
http.request(request, read_body)
end
- end
- def put!
- @debug_request = true
- put.tap { |response| print_response(response) }
+ @session.add_response(response)
end
def put
- http.start do |http|
+ @request.method(:put)
+
+ response = http.start do |http|
request = Net::HTTP::Put.new(composed_path, composed_headers)
- print_request(request)
+ @session.add_request(request)
http.request(request, read_body)
end
- end
- def delete!
- @debug_request = true
- delete.tap { |response| print_response(response) }
+ @session.add_response(response)
end
def delete
- http.start do |http|
+ @request.method(:delete)
+
+ response = http.start do |http|
request = Net::HTTP::Delete.new(composed_path, composed_headers)
- print_request(request)
+ @session.add_request(request)
http.request(request)
end
+
+ @session.add_response(response)
end
private
@@ -98,51 +94,6 @@ 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
- if current_formatter == Resto::Format::Json and not body.empty?
- body = Yajl::Encoder.encode(Format::Json.decode(body),
- { :pretty => true, :indent => " " })
- end
- 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
View
@@ -38,27 +38,27 @@ def current_formatter
@formatter ||= Resto::Format.get(@symbol || :default)
end
- def http_response(response)
- tap { @response = response }
+ def http_session(session)
+ tap { @session = session }
end
def read_body
body ? current_formatter.decode(body, :xpath => read_xpath) : nil
end
def body
- @response ? @response.body : nil
+ @session.body
end
def code
- @response ? @response.code : nil
+ @session.code
end
def valid?
- (/\A20\d{1}\z/ =~ code.to_s) == 0
+ @session.valid?
end
- attr_reader :response
+ attr_reader :session
def to_object
return self unless @translator
View
@@ -0,0 +1,46 @@
+# encoding: utf-8
+
+require 'resto/extra/log'
+
+module Resto
+ class Session
+
+ attr_reader :request, :response
+
+ def initialize(settings)
+ @request_settings = settings
+ end
+
+ def add_request(request)
+ tap { @request = request }
+ end
+
+ def add_response(response)
+ tap { @response = response }
+ end
+
+ def code
+ response_message :code
+ end
+
+ def valid?
+ (/\A20\d{1}\z/ =~ code.to_s) == 0
+ end
+
+ def body
+ response_message :body
+ end
+
+ def print
+ Log.print_request(@request, @request_settings)
+ Log.print_response(@response, @request_settings)
+ end
+
+ private
+
+ def response_message(symbol)
+ @response ? @response.send(symbol) : nil
+ end
+
+ end
+end
View
@@ -1,4 +1,4 @@
-# Resto
+# Resto [![Stillmaintained](http://stillmaintained.com/unders/resto.png)](http://stillmaintained.com/unders/resto)
* http://github.com/unders/resto
* [![Build Status](http://travis-ci.org/unders/resto.png)](http://travis-ci.org/unders/resto)
@@ -48,11 +48,11 @@
before do
@response_base = Resto::Response::Base.new.format(:json)
@new_response_base = Resto::Copy.response_base(@response_base).
- http_response('response')
+ http_session('session')
end
it { @response_base.instance_eval { @response }.should == nil }
it { @new_response_base.object_id.should_not == @response_base.object_id }
- it { @new_response_base.instance_eval { @response }.should == 'response' }
+ it { @new_response_base.instance_eval { @session }.should == 'session' }
end
end

0 comments on commit 818fa85

Please sign in to comment.