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, 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.
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
-
#body ⇒ String
readonly
The response body of the request.
-
#format ⇒ Symbol
readonly
The intended parsing format for the request.
Class Method Summary collapse
-
.call(body, format) ⇒ Object
Instantiate the parser and call #parse.
- .format_from_mimetype(mimetype) ⇒ Symbol?
-
.formats ⇒ Hash
The SupportedFormats hash.
-
.supported_formats ⇒ Array<Symbol>
List of supported formats.
- .supports_format?(format) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(body, format) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object?
Constructor Details
#initialize(body, format) ⇒ Parser
Returns a new instance of Parser.
95 96 97 98 |
# File 'lib/httparty/parser.rb', line 95 def initialize(body, format) @body = body @format = format end |
Instance Attribute Details
#body ⇒ String (readonly)
The response body of the request
58 59 60 |
# File 'lib/httparty/parser.rb', line 58 def body @body end |
#format ⇒ Symbol (readonly)
The intended parsing format for the request
62 63 64 |
# File 'lib/httparty/parser.rb', line 62 def format @format end |
Class Method Details
.call(body, format) ⇒ Object
Instantiate the parser and call #parse.
68 69 70 |
# File 'lib/httparty/parser.rb', line 68 def self.call(body, format) new(body, format).parse end |
.format_from_mimetype(mimetype) ⇒ Symbol?
80 81 82 |
# File 'lib/httparty/parser.rb', line 80 def self.format_from_mimetype(mimetype) formats[formats.keys.detect {|k| mimetype.include?(k)}] end |
.formats ⇒ Hash
Returns the SupportedFormats hash.
73 74 75 |
# File 'lib/httparty/parser.rb', line 73 def self.formats const_get(:SupportedFormats) end |
.supported_formats ⇒ Array<Symbol>
Returns list of supported formats.
85 86 87 |
# File 'lib/httparty/parser.rb', line 85 def self.supported_formats formats.values.uniq end |
.supports_format?(format) ⇒ Boolean
91 92 93 |
# File 'lib/httparty/parser.rb', line 91 def self.supports_format?(format) supported_formats.include?(format) end |
Instance Method Details
#parse ⇒ Object?
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/httparty/parser.rb', line 102 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 |