mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Adding custom parsers
This commit is contained in:
parent
a2a3f8bb41
commit
e5cbceaf00
2 changed files with 77 additions and 64 deletions
|
@ -112,7 +112,6 @@ module HTTParty
|
|||
end
|
||||
end
|
||||
|
||||
# HTTParty.const_get((self.format.to_s || 'text').capitalize)
|
||||
def parse_response(body)
|
||||
return nil if body.nil? or body.empty?
|
||||
if options[:parser].blank?
|
||||
|
@ -144,15 +143,6 @@ module HTTParty
|
|||
options[:headers]['Cookie'] = cookies_hash.to_cookie_string
|
||||
end
|
||||
|
||||
def capture_cookies(response)
|
||||
return unless response['Set-Cookie']
|
||||
cookies_hash = HTTParty::CookieHash.new()
|
||||
cookies_hash.add_cookies(options[:headers]['Cookie']) if options[:headers] && options[:headers]['Cookie']
|
||||
cookies_hash.add_cookies(response['Set-Cookie'])
|
||||
options[:headers] ||= {}
|
||||
options[:headers]['Cookie'] = cookies_hash.to_cookie_string
|
||||
end
|
||||
|
||||
# Uses the HTTP Content-Type header to determine the format of the response
|
||||
# It compares the MIME type returned to the types stored in the AllowedFormats hash
|
||||
def format_from_mimetype(mimetype)
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
||||
|
||||
class CustomParser
|
||||
def self.parse(body)
|
||||
return {:sexy => true}
|
||||
end
|
||||
end
|
||||
|
||||
describe HTTParty do
|
||||
before(:each) do
|
||||
@klass = Class.new
|
||||
|
@ -190,6 +196,23 @@ describe HTTParty do
|
|||
end
|
||||
end
|
||||
|
||||
describe "parser" do
|
||||
before(:each) do
|
||||
@parser = Proc.new{ |data| CustomParser.parse(data) }
|
||||
@klass.parser @parser
|
||||
end
|
||||
|
||||
it "should set parser options" do
|
||||
@klass.default_options[:parser].should == @parser
|
||||
end
|
||||
|
||||
it "should be able parse response with custom parser" do
|
||||
FakeWeb.register_uri(:get, 'http://twitter.com/statuses/public_timeline.xml', :body => 'tweets')
|
||||
custom_parsed_response = @klass.get('http://twitter.com/statuses/public_timeline.xml')
|
||||
custom_parsed_response[:sexy].should == true
|
||||
end
|
||||
end
|
||||
|
||||
describe "format" do
|
||||
it "should allow xml" do
|
||||
@klass.format :xml
|
||||
|
|
Loading…
Reference in a new issue