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

SupportedFormats =
{
  'text/xml'               => :xml,
  'application/xml'        => :xml,
  'application/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

- (Parser) initialize(body, format)



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

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

Instance Attribute Details

- (String) body (readonly)

The response body of the request



56
57
58
# File 'lib/httparty/parser.rb', line 56

def body
  @body
end

- (Symbol) format (readonly)

The intended parsing format for the request



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

def format
  @format
end

Class Method Details

+ (Object) call(body, format)

Instantiate the parser and call #parse.



66
67
68
# File 'lib/httparty/parser.rb', line 66

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

+ (Symbol?) format_from_mimetype(mimetype)



78
79
80
# File 'lib/httparty/parser.rb', line 78

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

+ (Hash) formats



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

def self.formats
  const_get(:SupportedFormats)
end

+ (Array<Symbol>) supported_formats



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

def self.supported_formats
  formats.values.uniq
end

+ (Boolean) supports_format?(format)



89
90
91
# File 'lib/httparty/parser.rb', line 89

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

Instance Method Details

- (Object?) parse



100
101
102
103
104
105
106
107
# File 'lib/httparty/parser.rb', line 100

def parse
  return nil if body.nil? || body.strip.empty? || body == "null"
  if supports_format?
    parse_supported_format
  else
    body
  end
end