class HTTParty::Parser
The default parser used by HTTParty
, supports xml, json, html, csv and plain text.
Custom Parsers¶ ↑
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.
Constants
- SupportedFormats
- UTF8_BOM
Attributes
The response body of the request @return [String]
The intended parsing format for the request @return [Symbol] e.g. :json
Public Class Methods
Source
# File lib/httparty/parser.rb, line 70 def self.call(body, format) new(body, format).parse end
Instantiate the parser and call {#parse}. @param [String] body the response body @param [Symbol] format the response format @return parsed response
Source
# File lib/httparty/parser.rb, line 82 def self.format_from_mimetype(mimetype) formats[formats.keys.detect {|k| mimetype.include?(k)}] end
@param [String] mimetype response MIME type @return [Symbol] @return [nil] mime type not supported
Source
# File lib/httparty/parser.rb, line 75 def self.formats const_get(:SupportedFormats) end
@return [Hash] the SupportedFormats
hash
Source
# File lib/httparty/parser.rb, line 97 def initialize(body, format) @body = body @format = format end
Source
# File lib/httparty/parser.rb, line 87 def self.supported_formats formats.values.uniq end
@return [Array<Symbol>] list of supported formats
Source
# File lib/httparty/parser.rb, line 93 def self.supports_format?(format) supported_formats.include?(format) end
@param [Symbol] format e.g. :json, :xml @return [Boolean]
Public Instance Methods
Source
# File lib/httparty/parser.rb, line 104 def parse return nil if body.nil? return nil if body == 'null' return nil if body.valid_encoding? && body.strip.empty? if body.valid_encoding? && body.encoding == Encoding::UTF_8 @body = body.gsub(/\A#{UTF8_BOM}/, '') end if supports_format? parse_supported_format else body end end
@return [Object] the parsed body @return [nil] when the response body is nil, an empty string, spaces only or “null”
Protected Instance Methods
Source
# File lib/httparty/parser.rb, line 126 def json JSON.parse(body, :quirks_mode => true, :allow_nan => true) end
Source
# File lib/httparty/parser.rb, line 146 def parse_supported_format if respond_to?(format, true) send(format) else raise NotImplementedError, "#{self.class.name} has not implemented a parsing method for the #{format.inspect} format." end end
Source
# File lib/httparty/parser.rb, line 142 def supports_format? self.class.supports_format?(format) end