From 309cd09ebe98054d34f89dc94c76a286fcecc5af Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Thu, 23 Apr 2009 11:28:36 -0400 Subject: [PATCH] Added message to the response object. --- History | 6 +++++- examples/basic.rb | 4 ++-- lib/httparty/request.rb | 2 +- lib/httparty/response.rb | 5 +++-- lib/httparty/version.rb | 2 +- spec/httparty/response_spec.rb | 17 +++++++++++------ 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/History b/History index bd8458f..ddf987e 100644 --- a/History +++ b/History @@ -1,4 +1,8 @@ -== 0.4.2 209-03-30 +== 0.4.5 2009-04-23 +* 1 minor update + * added message to the response object + +== 0.4.2 2009-03-30 * 2 minor changes * response code now returns an integer instead of a string (jqr) * rubyforge project setup for crack so i'm now depending on that instead of jnunemaker-crack diff --git a/examples/basic.rb b/examples/basic.rb index 3f85ba2..767589b 100644 --- a/examples/basic.rb +++ b/examples/basic.rb @@ -4,8 +4,8 @@ require 'pp' # You can also use post, put, delete in the same fashion response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') -puts response.body, response.code, response.headers.inspect +puts response.body, response.code, response.message, response.headers.inspect response.each do |item| puts item['user']['screen_name'] -end \ No newline at end of file +end diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index c749098..7370f7b 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -99,7 +99,7 @@ module HTTParty perform else parsed_response = parse_response(response.body) - Response.new(parsed_response, response.body, response.code, response.to_hash) + Response.new(parsed_response, response.body, response.code, response.message, response.to_hash) end end diff --git a/lib/httparty/response.rb b/lib/httparty/response.rb index 0f79caa..2670b51 100644 --- a/lib/httparty/response.rb +++ b/lib/httparty/response.rb @@ -1,12 +1,13 @@ module HTTParty class Response < BlankSlate #:nodoc: - attr_accessor :body, :code, :headers + attr_accessor :body, :code, :message, :headers attr_reader :delegate - def initialize(delegate, body, code, headers={}) + def initialize(delegate, body, code, message, headers={}) @delegate = delegate @body = body @code = code.to_i + @message = message @headers = headers end diff --git a/lib/httparty/version.rb b/lib/httparty/version.rb index 5aee1d2..fefa37a 100644 --- a/lib/httparty/version.rb +++ b/lib/httparty/version.rb @@ -1,3 +1,3 @@ module HTTParty #:nodoc: - Version = '0.4.2' + Version = '0.4.3' end diff --git a/spec/httparty/response_spec.rb b/spec/httparty/response_spec.rb index b1ffc13..e3ede68 100644 --- a/spec/httparty/response_spec.rb +++ b/spec/httparty/response_spec.rb @@ -6,7 +6,8 @@ describe HTTParty::Response do @response_object = {'foo' => 'bar'} @body = "{foo:'bar'}" @code = '200' - @response = HTTParty::Response.new(@response_object, @body, @code) + @message = 'OK' + @response = HTTParty::Response.new(@response_object, @body, @code, @message) end it "should set delegate" do @@ -24,20 +25,24 @@ describe HTTParty::Response do it "should set code as a Fixnum" do @response.code.should be_an_instance_of(Fixnum) end + + it "should set body" do + @response.body.should == @body + end end it "should be able to set headers during initialization" do - response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, {'foo' => 'bar'}) + response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK', {'foo' => 'bar'}) response.headers.should == {'foo' => 'bar'} end it "should send missing methods to delegate" do - response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200) + response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK') response['foo'].should == 'bar' end it "should be able to iterate delegate if it is array" do - response = HTTParty::Response.new([{'foo' => 'bar'}, {'foo' => 'baz'}], "[{foo:'bar'}, {foo:'baz'}]", 200) + response = HTTParty::Response.new([{'foo' => 'bar'}, {'foo' => 'baz'}], "[{foo:'bar'}, {foo:'baz'}]", 200, 'OK') response.size.should == 2 lambda { response.each { |item| } @@ -45,12 +50,12 @@ describe HTTParty::Response do end xit "should allow hashes to be accessed with dot notation" do - response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200) + response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK') response.foo.should == 'bar' end xit "should allow nested hashes to be accessed with dot notation" do - response = HTTParty::Response.new({'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200) + response = HTTParty::Response.new({'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200, 'OK') response.foo.should == {'bar' => 'baz'} response.foo.bar.should == 'baz' end