Class HTTParty::Parser
In: lib/httparty/parser.rb
Parent: Object

The default parser used by HTTParty, supports xml, json, html, yaml, 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.

Methods

Constants

SupportedFormats = { 'text/xml' => :xml, 'application/xml' => :xml, 'application/json' => :json, 'text/json' => :json, 'application/javascript' => :json, 'text/javascript' => :json, 'text/html' => :html, 'application/x-yaml' => :yaml, 'text/yaml' => :yaml, 'text/plain' => :plain

Attributes

body  [R]  The response body of the request @return [String]
format  [R]  The intended parsing format for the request @return [Symbol] e.g. :json

Public Class methods

Instantiate the parser and call {parse}. @param [String] body the response body @param [Symbol] format the response format @return parsed response

@param [String] mimetype response MIME type @return [Symbol] @return [nil] mime type not supported

@return [Hash] the SupportedFormats hash

@return [Array<Symbol>] list of supported formats

@param [Symbol] format e.g. :json, :xml @return [Boolean]

Public Instance methods

@return [Object] the parsed body @return [nil] when the response body is nil or an empty string

Protected Instance methods

[Validate]