Created
November 19, 2009 11:37
-
-
Save sandro/238708 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# override the parsing for all formats, always return :sexy => true | |
class CustomParser < HTTParty::Parser | |
def parse | |
{:sexy => true} | |
end | |
end | |
# only change the way json is parsed | |
class CustomJsonParser < HTTParty::Parser | |
def json | |
super.merge(:time_parsed => Time.now) | |
end | |
end | |
# continue allowing Proc objects to do all the parsing | |
class A | |
include HTTParty | |
parser lambda {|body, format| {:sexy => true} } | |
end | |
# use a class to do the parsing | |
class A | |
include HTTParty | |
parser CustomJsonParser | |
end | |
# replace httparty/parsers.rb with this base class. | |
# replace the case statement in #parse_response with our custom parsing implementation... HTTParty::Parser.call(body, format) | |
class HTTParty::Parser | |
def self.call(body, format) | |
new(body, format).parse | |
end | |
def initialize(body,format) | |
@body = body | |
@format = format | |
end | |
private :new | |
def parse | |
send format | |
rescue NoMethodError | |
raise "HTTParty::Parser does not support the #{format.inspect} format." | |
end | |
def xml | |
Crack::XML.parse(body) | |
end | |
def json | |
Crack::JSON.parse(body) | |
end | |
def yaml | |
::YAML.load(str) | |
end | |
def html | |
body | |
end | |
def text | |
body | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment