Parent

Files

HTTParty::Parser

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.

Constants

SupportedFormats

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

call(body, format) click to toggle source

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
format_from_mimetype(mimetype) click to toggle source

@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
formats() click to toggle source

@return [Hash] the SupportedFormats hash

    # File lib/httparty/parser.rb, line 70
70:     def self.formats
71:       const_get(:SupportedFormats)
72:     end
supported_formats() click to toggle source

@return [Array] list of supported formats

    # File lib/httparty/parser.rb, line 82
82:     def self.supported_formats
83:       formats.values.uniq
84:     end
supports_format?(format) click to toggle source

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

    # File lib/httparty/parser.rb, line 88
88:     def self.supports_format?(format)
89:       supported_formats.include?(format)
90:     end

Private Class Methods

new(body, format) click to toggle source
    # File lib/httparty/parser.rb, line 92
92:     def initialize(body, format)
93:       @body = body
94:       @format = format
95:     end

Public Instance Methods

parse() click to toggle source

@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

Protected Instance Methods

html() click to toggle source
     # File lib/httparty/parser.rb, line 123
123:     def html
124:       body
125:     end
json() click to toggle source
     # File lib/httparty/parser.rb, line 115
115:     def json
116:       Crack::JSON.parse(body)
117:     end
parse_supported_format() click to toggle source
     # 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
plain() click to toggle source
     # File lib/httparty/parser.rb, line 127
127:     def plain
128:       body
129:     end
supports_format?() click to toggle source
     # File lib/httparty/parser.rb, line 131
131:     def supports_format?
132:       self.class.supports_format?(format)
133:     end
xml() click to toggle source
     # File lib/httparty/parser.rb, line 111
111:     def xml
112:       Crack::XML.parse(body)
113:     end
yaml() click to toggle source
     # File lib/httparty/parser.rb, line 119
119:     def yaml
120:       YAML.load(body)
121:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.