class RevereMobile::Error

Attributes

body[R]
errors[R]
response[R]
status[R]

Public Class Methods

from_response(response) click to toggle source

Returns a RevereMobile::Error subclass, depending on status and response message.

@param [Hash] response HTTP response @return [RevereMobile::Error]

# File lib/revere_mobile/error.rb, line 15
def self.from_response(response)
  status = response[:status].to_i

  error_klass =
    case status
    when 400 then RevereMobile::BadRequest
    when 401 then RevereMobile::Unauthorized
    when 403 then RevereMobile::Forbidden
    when 405 then RevereMobile::MethodNotAllowed
    when 406 then RevereMobile::NotAcceptable
    when 408 then RevereMobile::RequestTimeout
    when 409 then RevereMobile::Conflict
    when 413 then RevereMobile::RequestEntityTooLarge
    when 415 then RevereMobile::UnsupportedMediaType
    when 422 then RevereMobile::UnprocessableEntity
    when 429 then RevereMobile::TooManyRequests
    when 404 then RevereMobile::NotFound
    when 400..499 then RevereMobile::ClientError
    when 500 then RevereMobile::InternalServerError
    when 501 then RevereMobile::NotImplemented
    when 502 then RevereMobile::BadGateway
    when 503 then RevereMobile::ServiceUnavailable
    when 504 then RevereMobile::GatewayTimeout
    when 500..599 then RevereMobile::ServerError
    end

  error_klass.new(response) if error_klass
end
new(response = nil) click to toggle source
Calls superclass method
# File lib/revere_mobile/error.rb, line 44
def initialize(response = nil)
  @response = response
  @body = response[:body]
  @status = response[:status]
  @errors = body.delete('errors')

  super(build_error)
end

Private Instance Methods

build_error() click to toggle source
# File lib/revere_mobile/error.rb, line 55
def build_error
  return nil if response.nil?

  {
    status: status,
    errors: errors
  }
end