From d49aeab3ca556a4db86b6fea8826953ce47a120a Mon Sep 17 00:00:00 2001 From: guilherme silveira Date: Sun, 24 Oct 2010 11:46:31 -0200 Subject: [PATCH] extracting base request feature --- lib/restfulie/client/feature.rb | 56 -------------------- lib/restfulie/client/feature/base_request.rb | 55 +++++++++++++++++++ 2 files changed, 55 insertions(+), 56 deletions(-) create mode 100644 lib/restfulie/client/feature/base_request.rb diff --git a/lib/restfulie/client/feature.rb b/lib/restfulie/client/feature.rb index ed1caa43..fa2ca2eb 100644 --- a/lib/restfulie/client/feature.rb +++ b/lib/restfulie/client/feature.rb @@ -3,59 +3,3 @@ module Feature Dir["#{File.dirname(__FILE__)}/http/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f } end end - -module Restfulie::Client::Feature - - class BaseRequest - - def execute(flow, request, response, env) - request!(request.verb, request.host, request.path, request, flow, env) - end - - # Executes a request against your server and return a response instance. - # * method: :get,:post,:delete,:head,:put - # * path: '/posts' - # * args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'} - def request!(method, host, path, request, flow, env, *args) - headers = request.default_headers.merge(args.extract_options!) - unless host.user.blank? && host.password.blank? - headers["Authorization"] = "Basic " + ["#{host.user}:#{host.password}"].pack("m").delete("\r\n") - end - headers.delete :recipe - headers['cookie'] = request.cookies if request.cookies - args << headers - - ::Restfulie::Common::Logger.logger.info(request.http_to_s(method, path, *args)) if ::Restfulie::Common::Logger.logger - begin - http_request = get_connection_provider(host) - - cached = Restfulie::Client.cache_provider.get([host, path], http_request, method) - return cached if cached - - response = http_request.send(method, path, *args) - rescue Exception => e - response = e - end - - flow.continue(request, response, env) - - # Restfulie::Client::Response::EnhanceResponse.new(response_handler).parse(host, path, http_request, self, response, method) - - end - - # Executes a request against your server and return a response instance without {Error} - # * method: :get,:post,:delete,:head,:put - # * path: '/posts' - # * args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'} - def request(method = nil, path = nil, *args) - @response_handler = Restfulie::Client::Response::IgnoreError.new(@response_handler) - request!(method, path, *args) - end - - def get_connection_provider(host) - @connection ||= ::Net::HTTP.new(host.host, host.port) - end - - end - -end \ No newline at end of file diff --git a/lib/restfulie/client/feature/base_request.rb b/lib/restfulie/client/feature/base_request.rb new file mode 100644 index 00000000..e8d40450 --- /dev/null +++ b/lib/restfulie/client/feature/base_request.rb @@ -0,0 +1,55 @@ +module Restfulie::Client::Feature + + class BaseRequest + + def execute(flow, request, response, env) + request!(request.verb, request.host, request.path, request, flow, env) + end + + # Executes a request against your server and return a response instance. + # * method: :get,:post,:delete,:head,:put + # * path: '/posts' + # * args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'} + def request!(method, host, path, request, flow, env, *args) + headers = request.default_headers.merge(args.extract_options!) + unless host.user.blank? && host.password.blank? + headers["Authorization"] = "Basic " + ["#{host.user}:#{host.password}"].pack("m").delete("\r\n") + end + headers.delete :recipe + headers['cookie'] = request.cookies if request.cookies + args << headers + + ::Restfulie::Common::Logger.logger.info(request.http_to_s(method, path, *args)) if ::Restfulie::Common::Logger.logger + begin + http_request = get_connection_provider(host) + + cached = Restfulie::Client.cache_provider.get([host, path], http_request, method) + return cached if cached + + response = http_request.send(method, path, *args) + rescue Exception => e + response = e + end + + flow.continue(request, response, env) + + # Restfulie::Client::Response::EnhanceResponse.new(response_handler).parse(host, path, http_request, self, response, method) + + end + + # Executes a request against your server and return a response instance without {Error} + # * method: :get,:post,:delete,:head,:put + # * path: '/posts' + # * args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'} + def request(method = nil, path = nil, *args) + @response_handler = Restfulie::Client::Response::IgnoreError.new(@response_handler) + request!(method, path, *args) + end + + def get_connection_provider(host) + @connection ||= ::Net::HTTP.new(host.host, host.port) + end + + end + +end \ No newline at end of file