class Sawyer::Response

Public Class Methods

new(agent, res, options = {}) click to toggle source

rubocop:disable MethodLength

# File lib/aptible/billforward/response.rb, line 7
def initialize(agent, res, options = {})
  fail Aptible::BillForward::ResponseError.new(
    @status.to_s,
    response: @res,
    body: @data,
    cause: @status
  ) if res.status >= 400

  @res = res
  @agent = agent
  @status = res.status
  @headers = res.headers
  @env = res.env
  @rels = process_rels
  @started = options[:sawyer_started]
  @ended = options[:sawyer_ended]
  @data = if @headers[:content_type] =~ /json|msgpack/
            process_data(@agent.decode_body(res.body))
          else
            res.body
          end
end

Public Instance Methods

klass_from_type(result) click to toggle source
# File lib/aptible/billforward/response.rb, line 41
def klass_from_type(result)
  "Aptible::BillForward::#{result[:@type].classify}".constantize
end
process_data(data) click to toggle source

rubocop:enable MethodLength

# File lib/aptible/billforward/response.rb, line 31
def process_data(data)
  data = data[:results] if data.key? :results
  case data
  when Hash  then klass_from_type(data).new(agent, data)
  when Array then data.map { |hash| process_data(hash) }
  when nil   then nil
  else data
  end
end