Skip to content

Instantly share code, notes, and snippets.

@sandro
Created November 19, 2009 11:37
Show Gist options
  • Save sandro/238708 to your computer and use it in GitHub Desktop.
Save sandro/238708 to your computer and use it in GitHub Desktop.
# 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