You can customize how HTTParty parses response bodies by subclassing HTTParty::Parser. This allows you to intercept parsing for all formats or add support for new MIME types.
Intercept all parsing
Override the parse method to control the entire parsing lifecycle.
Add a new format
To add a new format (e.g., atom), merge the new MIME type into SupportedFormats and implement a corresponding method named after the format symbol.
Restrict to specific formats
You can override SupportedFormats to ensure your parser only handles specific MIME types.
# Intercept the parsing for all formats
class SimpleParser < HTTParty::Parser
def parse
perform_parsing
end
end
# Add the atom format and parsing method to the default parser
class AtomParsingIncluded < HTTParty::Parser
SupportedFormats.merge!(
{"application/atom+xml" => :atom}
)
def atom
perform_atom_parsing
end
end
# Only support the atom format
class ParseOnlyAtom < HTTParty::Parser
SupportedFormats = {"application/atom+xml" => :atom}
def atom
perform_atom_parsing
end
end