class WeatherSage::HTTP::Parser

HTTP response body parser.

Constants

JSON_CONTENT_TYPE_REGEX

Regex match for known JSON content types.

Public Class Methods

new(log) click to toggle source

Create an HTTP response body parser.

# File lib/weather-sage/http/parser.rb, line 16
def initialize(log)
  @log = log
end

Public Instance Methods

parse(resp) click to toggle source

Parse HTTP response body.

# 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