Class: RubyTubesday::Parser
- Inherits:
-
Object
- Object
- RubyTubesday::Parser
- Defined in:
- lib/ruby_tubesday/parser.rb
Overview
Handles automatic parsing of responses for RubyTubesday.
Class Method Summary collapse
-
.parse(response) ⇒ Object
:nodoc:.
-
.register(meth, *mime_types) ⇒ Object
Registers a parser method for a particular content type.
Class Method Details
.parse(response) ⇒ Object
:nodoc:
25 26 27 28 29 30 31 32 33 |
# File 'lib/ruby_tubesday/parser.rb', line 25 def self.parse(response) # :nodoc: content_type = response['Content-Type'].split(';').first parser_method = @@parser_methods[content_type] if parser_method parser_method.call(response.body) else response.body end end |
.register(meth, *mime_types) ⇒ Object
Registers a parser method for a particular content type. When a RubeTubesday instance receives a response with a registered content type, the response body is passed to the associated parser method. The method should return a parsed representation of the response body. For example, the JSON parser is registered like so:
RubyTubesday::Parser.register(JSON.method(:parse), 'application/json')
You can also specify more than one content type:
RubyTubesday::Parser.register(JSON.method(:parse), 'application/json', 'text/javascript')
If a parser method is registered for a content type that already has a parser, the old method is discarded.
19 20 21 22 23 |
# File 'lib/ruby_tubesday/parser.rb', line 19 def self.register(meth, *mime_types) mime_types.each do |type| @@parser_methods[type] = meth end end |