class Flinks::Error

Attributes

code[RW]
response[RW]

Public Class Methods

build_message(response) click to toggle source

@param [HTTP::Response] response @return [String]

# File lib/flinks/error.rb, line 70
def self.build_message(response)
  message = response.parse['Message']
  message << " - FlinksCode: #{response.parse['FlinksCode']}"
rescue HTTP::Error
  response.reason
end
error_for_202(response) click to toggle source

@param [HTTP::Response] response @return [Flinks::Error]

# File lib/flinks/error.rb, line 40
def self.error_for_202(response)
  if response.parse['FlinksCode'] == 'OPERATION_PENDING'
    Flinks::OperationPending
  else
    Flinks::OperationDispatched
  end
end
error_for_400(response) click to toggle source

@param [HTTP::Response] response @return [Flinks::Error]

# File lib/flinks/error.rb, line 50
def self.error_for_400(response)
  if response.parse['FlinksCode'] == 'SESSION_NONEXISTENT'
    Flinks::SessionNonexistent
  else
    Flinks::BadRequest
  end
end
error_for_403(response) click to toggle source

@param [HTTP::Response] response @return [Flinks::Error]

# File lib/flinks/error.rb, line 60
def self.error_for_403(response)
  if response.parse['FlinksCode'] == 'TOO_MANY_REQUESTS'
    Flinks::TooManyRequests
  else
    Flinks::Forbidden
  end
end
from_response(response) click to toggle source

@param [HTTP::Response] response @return [Flinks::Error]

# File lib/flinks/error.rb, line 9
def self.from_response(response)
  klass = case response.code
          when 202      then error_for_202(response)
          when 400      then error_for_400(response)
          when 401      then Flinks::Unauthorized
          when 403      then error_for_403(response)
          when 404      then Flinks::NotFound
          when 405      then Flinks::MethodNotAllowed
          when 406      then Flinks::NotAcceptable
          when 409      then Flinks::Conflict
          when 415      then Flinks::UnsupportedMediaType
          when 422      then Flinks::UnprocessableEntity
          when 400..499 then Flinks::ClientError
          when 500      then Flinks::InternalServerError
          when 501      then Flinks::NotImplemented
          when 502      then Flinks::BadGateway
          when 503      then Flinks::ServiceUnavailable
          when 500..599 then Flinks::ServerError
          else
            self
          end

  error          = klass.new(build_message(response))
  error.response = response
  error.code     = response.code

  error
end