Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixing put example
  • Loading branch information
guilhermesilveira committed Oct 20, 2010
1 parent e1e2a11 commit 64df00f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 28 deletions.
5 changes: 4 additions & 1 deletion lib/restfulie/client/http/request_marshaller.rb
Expand Up @@ -8,7 +8,9 @@ class RequestMarshaller < MasterDelegator

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

Expand Down Expand Up @@ -54,6 +56,7 @@ def request!(method, path, *args)
type = headers['Content-Type']
raise Restfulie::Common::Error::RestfulieError, "Missing content type related to the data to be submitted" unless type
marshaller = RequestMarshaller.content_type_for(type)
raise Restfulie::Common::Error::RestfulieError, "Missing content type for #{type} related to the data to be submitted" unless marshaller
payload = marshaller.marshal(payload, { :rel => rel, :recipe => recipe }) unless payload.nil? || (payload.kind_of?(String) && payload.empty?)
args = set_marshalled_payload(method, path, payload, *args)
args = add_representation_headers(method, path, marshaller, *args)
Expand Down
1 change: 1 addition & 0 deletions lib/restfulie/client/response.rb
Expand Up @@ -5,6 +5,7 @@ module Response#:nodoc:
autoload :IgnoreError, 'restfulie/client/response/ignore_error'
autoload :CatchAndThrow, 'restfulie/client/response/catch_and_throw'
autoload :CacheHandler, 'restfulie/client/response/cache_handler'
autoload :CreatedRedirect, 'restfulie/client/response/created_redirect'
autoload :UnmarshallHandler, 'restfulie/client/response/unmarshall_handler'
end
end
Expand Down
28 changes: 14 additions & 14 deletions lib/restfulie/client/response/catch_and_throw.rb
Expand Up @@ -7,33 +7,33 @@ def parse(host, path, http_request, request, response)
when 100..299
response
when 300..399
raise Error::Redirection.new(request, response)
raise Restfulie::Client::HTTP::Error::Redirection.new(request, response)
when 400
raise Error::BadRequest.new(request, response)
raise Restfulie::Client::HTTP::Error::BadRequest.new(request, response)
when 401
raise Error::Unauthorized.new(request, response)
raise Restfulie::Client::HTTP::Error::Unauthorized.new(request, response)
when 403
raise Error::Forbidden.new(request, response)
raise Restfulie::Client::HTTP::Error::Forbidden.new(request, response)
when 404
raise Error::NotFound.new(request, response)
raise Restfulie::Client::HTTP::Error::NotFound.new(request, response)
when 405
raise Error::MethodNotAllowed.new(request, response)
raise Restfulie::Client::HTTP::Error::MethodNotAllowed.new(request, response)
when 407
raise Error::ProxyAuthenticationRequired.new(request, response)
raise Restfulie::Client::HTTP::Error::ProxyAuthenticationRequired.new(request, response)
when 409
raise Error::Conflict.new(request, response)
raise Restfulie::Client::HTTP::Error::Conflict.new(request, response)
when 410
raise Error::Gone.new(request, response)
raise Restfulie::Client::HTTP::Error::Gone.new(request, response)
when 412
raise Error::PreconditionFailed.new(request, response)
raise Restfulie::Client::HTTP::Error::PreconditionFailed.new(request, response)
when 402, 406, 408, 411, 413..499
raise Error::ClientError.new(request, response)
raise Restfulie::Client::HTTP::Error::ClientError.new(request, response)
when 501
raise Error::NotImplemented.new(request, response)
raise Restfulie::Client::HTTP::Error::NotImplemented.new(request, response)
when 500, 502..599
raise Error::ServerError.new(request, response)
raise Restfulie::Client::HTTP::Error::ServerError.new(request, response)
else
raise Error::UnknownError.new(request, response)
raise Restfulie::Client::HTTP::Error::UnknownError.new(request, response)
end
end

Expand Down
25 changes: 25 additions & 0 deletions lib/restfulie/client/response/created_redirect.rb
@@ -0,0 +1,25 @@
module Restfulie
module Client
module Response
class CreatedRedirect

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

def parse(host, path, http_request, request, response)
result = @requester.parse(host, path, http_request, request, response)
response = result.respond_to?(:response) ? result.response : result
if response.respond_to?(:code) && response.code == 201
request = Restfulie.at(response.headers['location'])
request.accepts(@config.acceptable_mediatypes) if @config.acceptable_mediatypes
request.get!
else
response
end
end
end
end
end
end
5 changes: 2 additions & 3 deletions lib/restfulie/client/response/enhance_response.rb
@@ -1,16 +1,15 @@
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)
unless resp.kind_of? ::Restfulie::Client::HTTP::ResponseHolder
resp.extend(::Restfulie::Client::HTTP::ResponseHolder)
resp.response = response
end
resp
Expand Down
2 changes: 1 addition & 1 deletion lib/restfulie/client/response/ignore_error.rb
Expand Up @@ -10,7 +10,7 @@ def initialize(requester)
def parse(host, path, http_request, request, response)
begin
@requester.parse(host, path, http_request, request, response)
rescue Error::RESTError => se
rescue Restfulie::Client::HTTP::Error::RESTError => se
se
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/restfulie/client/response/unmarshall_handler.rb
Expand Up @@ -14,11 +14,7 @@ def initialize(config, requester)
# 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?
if @config.raw?
response
elsif (!response.body.nil?) && !response.body.empty?
representation = Restfulie::Client::HTTP::RequestMarshaller.content_type_for(response.headers['content-type']) || Restfulie::Common::Representation::Generic.new
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/requests/fake_server.rb
Expand Up @@ -40,7 +40,7 @@ class FakeServer < Sinatra::Base
end

put "/test/?" do
'OK'
'<content>OK</content>'
end

delete "/test/?" do
Expand Down
9 changes: 6 additions & 3 deletions tests/spec/requests/http/adapter_spec.rb
Expand Up @@ -30,11 +30,15 @@
end

it "should post! and respond 201 code" do
@client.at("/test").as("text/plain").post!("<test></test>").should respond_with_status(201)
Restfulie.using{
recipe
request_marshaller
verb_request
}.at(@host).at("/test/redirect/songs").as("application/xml").post!("<test></text>").should respond_with_status(201)
end

it "should put! and respond 200 code" do
@client.at("/test").as("text/plain").put!("<test></test>").response.code.should == 200
@client.at("/test").as("application/xml").put!("<test></test>").response.code.should == 200
end

it "should delete! and respond 200 code" do
Expand Down Expand Up @@ -223,7 +227,6 @@ class FakeResponse < ::Restfulie::Client::HTTP::Response
context "redirection" do

let(:resp) {
debugger
Restfulie.at("http://localhost:4567/test_redirection").follow.get!
}

Expand Down

0 comments on commit 64df00f

Please sign in to comment.