Class: HTTParty::Parser Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/httparty/parser.rb

Overview

This class is abstract.

Read the Custom Parsers section for more information.

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.

Examples:

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

Constant Summary collapse

SupportedFormats =
{
  'text/xml'                    => :xml,
  'application/xml'             => :xml,
  'application/json'            => :json,
  'application/vnd.api+json'    => :json,
  'application/hal+json'        => :json,
  'text/json'                   => :json,
  'application/javascript'      => :plain,
  'text/javascript'             => :plain,
  'text/html'                   => :html,
  'text/plain'                  => :plain,
  'text/csv'                    => :csv,
  'application/csv'             => :csv,
  'text/comma-separated-values' => :csv
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, format) ⇒ Parser

Returns a new instance of Parser.



97
98
99
100
# File 'lib/httparty/parser.rb', line 97

def initialize(body, format)
  @body = body
  @format = format
end

Instance Attribute Details

#bodyString (readonly)

The response body of the request

Returns:

  • (String)


60
61
62
# File 'lib/httparty/parser.rb', line 60

def body
  @body
end

#formatSymbol (readonly)

The intended parsing format for the request

Returns:

  • (Symbol)

    e.g. :json



64
65
66
# File 'lib/httparty/parser.rb', line 64

def format
  @format
end

Class Method Details

.call(body, format) ⇒ Object

Instantiate the parser and call #parse.

Parameters:

  • body (String)

    the response body

  • format (Symbol)

    the response format

Returns:

  • parsed response



70
71
72
# File 'lib/httparty/parser.rb', line 70

def self.call(body, format)
  new(body, format).parse
end

.format_from_mimetype(mimetype) ⇒ Symbol?

Parameters:

  • mimetype (String)

    response MIME type

Returns:

  • (Symbol)
  • (nil)

    mime type not supported



82
83
84
# File 'lib/httparty/parser.rb', line 82

def self.format_from_mimetype(mimetype)
  formats[formats.keys.detect {|k| mimetype.include?(k)}]
end

.formatsHash

Returns the SupportedFormats hash.

Returns:

  • (Hash)

    the SupportedFormats hash



75
76
77
# File 'lib/httparty/parser.rb', line 75

def self.formats
  const_get(:SupportedFormats)
end

.supported_formatsArray<Symbol>

Returns list of supported formats.

Returns:

  • (Array<Symbol>)

    list of supported formats



87
88
89
# File 'lib/httparty/parser.rb', line 87

def self.supported_formats
  formats.values.uniq
end

.supports_format?(format) ⇒ Boolean

Parameters:

  • format (Symbol)

    e.g. :json, :xml

Returns:

  • (Boolean)


93
94
95
# File 'lib/httparty/parser.rb', line 93

def self.supports_format?(format)
  supported_formats.include?(format)
end

Instance Method Details

#parseObject?

Returns:

  • (Object)

    the parsed body

  • (nil)

    when the response body is nil, an empty string, spaces only or “null”



104
105
106
107
108
109
110
111
112
113
114
115
116
# 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