Class: HTTParty::Parser

### Home Classes Methods

### In Files

- lib/httparty/parser.rb

### Parent

Object

### Methods

- ::call
- ::format_from_mimetype
- ::formats
- ::supported_formats
- ::supports_format?
- #html
- #json
- #parse
- #parse_supported_format
- #plain
- #supports_format?
- #xml
- #yaml

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- HTTParty
- HTTParty::AllowedFormatsDeprecation
- HTTParty::ClassMethods
- HTTParty::Parser
- HTTParty::RedirectionTooDeep
- HTTParty::Response
- HTTParty::Response::Headers
- HTTParty::ResponseError
- HTTParty::UnsupportedFormat
- HTTParty::UnsupportedURIScheme
- Net
- Net::HTTPHeader

No matching classes.

# 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 {parse}. @param [String] body the response body @param [Symbol] format the response format @return parsed response

    # File lib/httparty/parser.rb, line 65
    def self.call(body, format)
      new(body, format).parse
    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
    def self.format_from_mimetype(mimetype)
      formats[formats.keys.detect {|k| mimetype.include?(k)}]
    end

formats() click to toggle source

@return [Hash] the SupportedFormats hash

    # File lib/httparty/parser.rb, line 70
    def self.formats
      const_get(:SupportedFormats)
    end

supported_formats() click to toggle source

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

    # File lib/httparty/parser.rb, line 82
    def self.supported_formats
      formats.values.uniq
    end

supports_format?(format) click to toggle source

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

    # File lib/httparty/parser.rb, line 88
    def self.supports_format?(format)
      supported_formats.include?(format)
    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
    def parse
      return nil if body.nil? || body.empty?
      if supports_format?
        parse_supported_format
      else
        body
      end
    end

### Protected Instance Methods

html() click to toggle source

    # File lib/httparty/parser.rb, line 123
    def html
      body
    end

json() click to toggle source

    # File lib/httparty/parser.rb, line 115
    def json
      Crack::JSON.parse(body)
    end

parse_supported_format() click to toggle source

    # File lib/httparty/parser.rb, line 135
    def parse_supported_format
      send(format)
    rescue NoMethodError
      raise NotImplementedError, "#{self.class.name} has not implemented a parsing method for the #{format.inspect} format."
    end

plain() click to toggle source

    # File lib/httparty/parser.rb, line 127
    def plain
      body
    end

supports_format?() click to toggle source

    # File lib/httparty/parser.rb, line 131
    def supports_format?
      self.class.supports_format?(format)
    end

xml() click to toggle source

    # File lib/httparty/parser.rb, line 111
    def xml
      Crack::XML.parse(body)
    end

yaml() click to toggle source

    # File lib/httparty/parser.rb, line 119
    def yaml
      YAML.load(body)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.