Skip to content

Commit

Permalink
breaking into files
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 20, 2010
1 parent 15409fc commit 84c3390
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 43 deletions.
4 changes: 2 additions & 2 deletions lib/restfulie/client/http/request_adapter.rb
Expand Up @@ -17,7 +17,7 @@ class RequestAdapter
attr_writer :default_headers

def initialize
@response_handler = CatchAndThrow.new
@response_handler = Restfulie::Client::Response::CatchAndThrow.new
end

#Set host
Expand Down Expand Up @@ -81,7 +81,7 @@ def headers
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
def request(method, path, *args)
@response_handler = IgnoreError.new(@response_handler)
@response_handler = Restfulie::Client::Response::IgnoreError.new(@response_handler)
request!(method, path, *args)
end

Expand Down
42 changes: 1 addition & 41 deletions lib/restfulie/client/http/request_marshaller.rb
@@ -1,54 +1,14 @@
module Restfulie
module Client
module HTTP

class CacheHandler

def initialize(requester)
@requester = requester
end

def parse(host, path, http_request, request, response)
Restfulie::Client.cache_provider.put([host, path], http_request, response)
@requester.parse(host, path, http_request, request, response)
end
end

class UnmarshallHandler

def initialize(config, requester)
@config = config
@requester = requester
end

# parses the http response.
# first checks if its a 201, redirecting to the resource location.
# otherwise check if its a raw request, returning the content itself.
# finally, tries to parse the content with a mediatype handler or returns the response itself.
def parse(host, path, http_request, request, response)
response = @requester.parse(host, path, http_request, request, response)
if response.code == 201
request = Restfulie.at(response.headers['location'])
request.accepts(@config.acceptable_mediatypes) if @config.acceptable_mediatypes
request.get!
elsif @config.raw?
response
elsif (!response.body.nil?) && !response.body.empty?
representation = RequestMarshaller.content_type_for(response.headers['content-type']) || Restfulie::Common::Representation::Generic.new
representation.unmarshal(response.body)
else
response
end
end
end

class RequestMarshaller < MasterDelegator

attr_reader :acceptable_mediatypes

def initialize(requester)
@requester = requester
@requester.response_handler= UnmarshallHandler.new(self, CacheHandler.new(@requester.response_handler))
@requester.response_handler= Restfulie::Client::Response::UnmarshallHandler.new(self, Restfulie::Client::Response::CacheHandler.new(@requester.response_handler))
@raw = false
end

Expand Down
2 changes: 2 additions & 0 deletions lib/restfulie/client/response.rb
Expand Up @@ -4,6 +4,8 @@ module Response#:nodoc:
autoload :EnhanceResponse, 'restfulie/client/response/enhance_response'
autoload :IgnoreError, 'restfulie/client/response/ignore_error'
autoload :CatchAndThrow, 'restfulie/client/response/catch_and_throw'
autoload :CacheHandler, 'restfulie/client/response/cache_handler'
autoload :UnmarshalHandler, 'restfulie/client/response/unmarshal_handler'
end
end
end
17 changes: 17 additions & 0 deletions lib/restfulie/client/response/cache_handler.rb
@@ -0,0 +1,17 @@
module Restfulie
module Client
module Response
class CacheHandler

def initialize(requester)
@requester = requester
end

def parse(host, path, http_request, request, response)
Restfulie::Client.cache_provider.put([host, path], http_request, response)
@requester.parse(host, path, http_request, request, response)
end
end
end
end
end
33 changes: 33 additions & 0 deletions lib/restfulie/client/response/unmarshal_handler.rb
@@ -0,0 +1,33 @@
module Restfulie
module Client
module Response
class UnmarshallHandler

def initialize(config, requester)
@config = config
@requester = requester
end

# parses the http response.
# first checks if its a 201, redirecting to the resource location.
# otherwise check if its a raw request, returning the content itself.
# finally, tries to parse the content with a mediatype handler or returns the response itself.
def parse(host, path, http_request, request, response)
response = @requester.parse(host, path, http_request, request, response)
if response.code == 201
request = Restfulie.at(response.headers['location'])
request.accepts(@config.acceptable_mediatypes) if @config.acceptable_mediatypes
request.get!
elsif @config.raw?
response
elsif (!response.body.nil?) && !response.body.empty?
representation = RequestMarshaller.content_type_for(response.headers['content-type']) || Restfulie::Common::Representation::Generic.new
representation.unmarshal(response.body)
else
response
end
end
end
end
end
end

0 comments on commit 84c3390

Please sign in to comment.