module Collins::Api::Util::Responses

Protected Instance Methods

parse_response(response, options) { |json| ... } click to toggle source
# File lib/collins/api/util/responses.rb, line 9
def parse_response response, options
  do_raise = options[:raise] != false
  if options.include?(:expects) && ![options[:expects]].flatten.include?(response.code) then
    handle_error(response) if do_raise
    if options.include?(:default) then
      return options[:default]
    else
      raise UnexpectedResponseError.new("Expected code #{options[:expects]}, got #{response.code}")
    end
  end
  handle_error(response) if do_raise
  json = response.parsed_response
  if options.include?(:as) then
    case options[:as]
    when :asset
      json = Collins::Asset.from_json(json)
    when :bare_asset
      json = Collins::Asset.from_json(json, true)
    when :data
      json = json["data"]
    when :status
      json = json["data"]["SUCCESS"]
    when :message
      json = json["data"]["MESSAGE"]
    when :paginated
      json = json["data"]["Data"]
    end
  end
  if block_given? then
    yield(json)
  else
    json
  end
end