Class: HTTParty::Parser Abstract
- Inherits:
-
Object
- Object
- HTTParty::Parser
- 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, 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.
Constant Summary
- SupportedFormats =
{ 'text/xml' => :xml, 'application/xml' => :xml, 'application/json' => :json, 'text/json' => :json, 'application/javascript' => :json, 'text/javascript' => :json, 'text/html' => :html, 'text/plain' => :plain }
Instance Attribute Summary (collapse)
-
- (String) body
readonly
The response body of the request.
-
- (Symbol) format
readonly
The intended parsing format for the request.
Class Method Summary (collapse)
-
+ (Object) call(body, format)
Instantiate the parser and call #parse.
- + (Symbol?) format_from_mimetype(mimetype)
-
+ (Hash) formats
The SupportedFormats hash.
-
+ (Array<Symbol>) supported_formats
List of supported formats.
- + (Boolean) supports_format?(format)
Instance Method Summary (collapse)
-
- (Parser) initialize(body, format)
constructor
A new instance of Parser.
- - (Object?) parse
Constructor Details
- (Parser) initialize(body, format)
A new instance of Parser
90 91 92 93 |
# File 'lib/httparty/parser.rb', line 90 def initialize(body, format) @body = body @format = format end |
Instance Attribute Details
- (String) body (readonly)
The response body of the request
53 54 55 |
# File 'lib/httparty/parser.rb', line 53 def body @body end |
- (Symbol) format (readonly)
The intended parsing format for the request
57 58 59 |
# File 'lib/httparty/parser.rb', line 57 def format @format end |
Class Method Details
+ (Object) call(body, format)
Instantiate the parser and call #parse.
63 64 65 |
# File 'lib/httparty/parser.rb', line 63 def self.call(body, format) new(body, format).parse end |
+ (Symbol?) format_from_mimetype(mimetype)
75 76 77 |
# File 'lib/httparty/parser.rb', line 75 def self.format_from_mimetype(mimetype) formats[formats.keys.detect {|k| mimetype.include?(k)}] end |
+ (Hash) formats
The SupportedFormats hash
68 69 70 |
# File 'lib/httparty/parser.rb', line 68 def self.formats const_get(:SupportedFormats) end |
+ (Array<Symbol>) supported_formats
List of supported formats
80 81 82 |
# File 'lib/httparty/parser.rb', line 80 def self.supported_formats formats.values.uniq end |
+ (Boolean) supports_format?(format)
86 87 88 |
# File 'lib/httparty/parser.rb', line 86 def self.supports_format?(format) supported_formats.include?(format) end |
Instance Method Details
- (Object?) parse
97 98 99 100 101 102 103 104 |
# File 'lib/httparty/parser.rb', line 97 def parse return nil if body.nil? || body.strip.empty? || body == "null" if supports_format? parse_supported_format else body end end |