Class: WeatherSage::HTTP::Parser
- Inherits:
-
Object
- Object
- WeatherSage::HTTP::Parser
- Defined in:
- lib/weather-sage/http/parser.rb
Overview
HTTP response body parser.
Constant Summary collapse
- JSON_CONTENT_TYPE_REGEX =
Regex match for known JSON content types.
/^application\/json|text\/json|application\/geo\+json/
Instance Method Summary collapse
-
#initialize(log) ⇒ Parser
constructor
Create an HTTP response body parser.
-
#parse(resp) ⇒ Object
Parse HTTP response body.
Constructor Details
#initialize(log) ⇒ Parser
Create an HTTP response body parser.
16 17 18 |
# File 'lib/weather-sage/http/parser.rb', line 16 def initialize(log) @log = log end |
Instance Method Details
#parse(resp) ⇒ Object
Parse HTTP response body.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/weather-sage/http/parser.rb', line 23 def parse(resp) # FIXME: need to extract encoding from content-type resp.body.force_encoding('UTF-8') r = case resp.content_type when JSON_CONTENT_TYPE_REGEX # parse and return json JSON.parse(resp.body) else # return string resp.body end @log.debug('Parser#parse') do JSON.unparse({ type: resp.content_type, data: r, }) end # return response r end |