Skip to content

Commit

Permalink
extracting base request feature
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 24, 2010
1 parent d86fc4d commit d49aeab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 56 deletions.
56 changes: 0 additions & 56 deletions lib/restfulie/client/feature.rb
Expand Up @@ -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.
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
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}
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
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
55 changes: 55 additions & 0 deletions 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.
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
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}
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
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

0 comments on commit d49aeab

Please sign in to comment.