Object
The default parser used by HTTParty, supports xml, json, html, yaml, and plain text.
If you’d like to do your own custom parsing, subclassing HTTParty::Parser will make that process much easier. There are a few different ways you can utilize HTTParty::Parser as a superclass.
@example Intercept the parsing for all formats
class SimpleParser < HTTParty::Parser
def parse
perform_parsing
end
end
@example 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
@example Only support the atom format
class ParseOnlyAtom < HTTParty::Parser
SupportedFormats = {"application/atom+xml" => :atom}
def atom
perform_atom_parsing
end
end
@abstract Read the Custom Parsers section for more information.
Instantiate the parser and call {#}. @param [String] body the response body @param [Symbol] format the response format @return parsed response
# File lib/httparty/parser.rb, line 65
65: def self.call(body, format)
66: new(body, format).parse
67: end
@param [String] mimetype response MIME type @return [Symbol] @return [nil] mime type not supported
# File lib/httparty/parser.rb, line 77
77: def self.format_from_mimetype(mimetype)
78: formats[formats.keys.detect {|k| mimetype.include?(k)}]
79: end
@return [Hash] the SupportedFormats hash
# File lib/httparty/parser.rb, line 70
70: def self.formats
71: const_get(:SupportedFormats)
72: end
@return [Object] the parsed body @return [nil] when the response body is nil or an empty string
# File lib/httparty/parser.rb, line 100
100: def parse
101: return nil if body.nil? || body.empty?
102: if supports_format?
103: parse_supported_format
104: else
105: body
106: end
107: end
# File lib/httparty/parser.rb, line 123
123: def html
124: body
125: end
# File lib/httparty/parser.rb, line 115
115: def json
116: Crack::JSON.parse(body)
117: end
# File lib/httparty/parser.rb, line 135
135: def parse_supported_format
136: send(format)
137: rescue NoMethodError
138: raise NotImplementedError, "#{self.class.name} has not implemented a parsing method for the #{format.inspect} format."
139: end
# File lib/httparty/parser.rb, line 127
127: def plain
128: body
129: end
# File lib/httparty/parser.rb, line 131
131: def supports_format?
132: self.class.supports_format?(format)
133: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.