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 6eb3361 commit 15409fc
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 68 deletions.
1 change: 1 addition & 0 deletions lib/restfulie/client.rb
Expand Up @@ -4,6 +4,7 @@ module Restfulie
module Client
autoload :MasterDelegator, 'restfulie/client/master_delegator'
autoload :HTTP, 'restfulie/client/http'
autoload :Response, 'restfulie/client/response'
autoload :Configuration, 'restfulie/client/configuration'
autoload :EntryPoint, 'restfulie/client/entry_point'
autoload :Base, 'restfulie/client/base'
Expand Down
68 changes: 0 additions & 68 deletions lib/restfulie/client/http/request_adapter.rb
Expand Up @@ -15,75 +15,7 @@ class RequestAdapter

attr_accessor :cookies, :response_handler
attr_writer :default_headers

class EnhanceResponse
def initialize(requester)
@requester = requester
end

def parse(host, path, http_request, request, response)
resp = @requester.parse(host, path, http_request, request, response)
unless resp.kind_of? ResponseHolder
resp.extend(ResponseHolder)
resp.response = response
end
resp
end
end

class IgnoreError

def initialize(requester)
@requester = requester
end

def parse(host, path, http_request, request, response)
begin
@requester.parse(host, path, http_request, request, response)
rescue Error::RESTError => se
se
end
end

end
class CatchAndThrow
def parse(host, path, http_request, request, response)
case response.code
when 100..299
response
when 300..399
raise Error::Redirection.new(request, response)
when 400
raise Error::BadRequest.new(request, response)
when 401
raise Error::Unauthorized.new(request, response)
when 403
raise Error::Forbidden.new(request, response)
when 404
raise Error::NotFound.new(request, response)
when 405
raise Error::MethodNotAllowed.new(request, response)
when 407
raise Error::ProxyAuthenticationRequired.new(request, response)
when 409
raise Error::Conflict.new(request, response)
when 410
raise Error::Gone.new(request, response)
when 412
raise Error::PreconditionFailed.new(request, response)
when 402, 406, 408, 411, 413..499
raise Error::ClientError.new(request, response)
when 501
raise Error::NotImplemented.new(request, response)
when 500, 502..599
raise Error::ServerError.new(request, response)
else
raise Error::UnknownError.new(request, response)
end
end

end

def initialize
@response_handler = CatchAndThrow.new
end
Expand Down
9 changes: 9 additions & 0 deletions lib/restfulie/client/response.rb
@@ -0,0 +1,9 @@
module Restfulie
module Client
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'
end
end
end
43 changes: 43 additions & 0 deletions lib/restfulie/client/response/catch_and_throw.rb
@@ -0,0 +1,43 @@
module Restfulie
module Client
module Response
class CatchAndThrow
def parse(host, path, http_request, request, response)
case response.code
when 100..299
response
when 300..399
raise Error::Redirection.new(request, response)
when 400
raise Error::BadRequest.new(request, response)
when 401
raise Error::Unauthorized.new(request, response)
when 403
raise Error::Forbidden.new(request, response)
when 404
raise Error::NotFound.new(request, response)
when 405
raise Error::MethodNotAllowed.new(request, response)
when 407
raise Error::ProxyAuthenticationRequired.new(request, response)
when 409
raise Error::Conflict.new(request, response)
when 410
raise Error::Gone.new(request, response)
when 412
raise Error::PreconditionFailed.new(request, response)
when 402, 406, 408, 411, 413..499
raise Error::ClientError.new(request, response)
when 501
raise Error::NotImplemented.new(request, response)
when 500, 502..599
raise Error::ServerError.new(request, response)
else
raise Error::UnknownError.new(request, response)
end
end

end
end
end
end
21 changes: 21 additions & 0 deletions lib/restfulie/client/response/enhance_response.rb
@@ -0,0 +1,21 @@
module Restfulie
module Client
module Response

class EnhanceResponse
def initialize(requester)
@requester = requester
end

def parse(host, path, http_request, request, response)
resp = @requester.parse(host, path, http_request, request, response)
unless resp.kind_of? ResponseHolder
resp.extend(ResponseHolder)
resp.response = response
end
resp
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/restfulie/client/response/ignore_error.rb
@@ -0,0 +1,21 @@
module Restfulie
module Client
module Response
class IgnoreError

def initialize(requester)
@requester = requester
end

def parse(host, path, http_request, request, response)
begin
@requester.parse(host, path, http_request, request, response)
rescue Error::RESTError => se
se
end
end

end
end
end
end

0 comments on commit 15409fc

Please sign in to comment.