class FastBound::Response

Attributes

success[RW]

Public Class Methods

new(response) click to toggle source
# File lib/fastbound-ruby/response.rb, line 6
def initialize(response)
  @response = response

  case @response
  when Net::HTTPUnauthorized
    raise FastBound::Error::NotAuthorized.new(@response.body)
  when Net::HTTPNotFound
    raise FastBound::Error::NotFound.new(@response.body)
  when Net::HTTPOK, Net::HTTPSuccess, Net::HTTPNoContent, Net::HTTPCreated
    self.success = true
    _data = (JSON.parse(@response.body) if @response.body.present?)

    @data = case
    when _data.is_a?(Hash)
      _data.deep_symbolize_keys
    when _data.is_a?(Array)
      _data.map(&:deep_symbolize_keys)
    end
  else
    if FastBound.config.full_debug?
      puts "-- DEBUG: #{self}: RequestError: #{@response.inspect}"
    end

    error_message = begin
      JSON.parse(@response.body)['errors'].map { |error| error['message'].chomp('.') }.join('. ')
    rescue
      [@response.message, @response.body].reject(&:blank?).join(" | ")
    end

    raise FastBound::Error::RequestError.new(error_message)
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/fastbound-ruby/response.rb, line 39
def [](key)
  @data[key]
end
body() click to toggle source
# File lib/fastbound-ruby/response.rb, line 43
def body
  @data
end
fetch(key) click to toggle source
# File lib/fastbound-ruby/response.rb, line 47
def fetch(key)
  @data.fetch(key)
end
success?() click to toggle source
# File lib/fastbound-ruby/response.rb, line 51
def success?
  !!success
end