Class: Ey::Core::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ey-core/response.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

BadRequest =
Class.new(Error)
Conflict =
Class.new(Error)
NotFound =
Class.new(Error)
RateLimitExceeded =
Class.new(Error)
Unauthorized =
Class.new(Error)
Unexpected =
Class.new(Error)
Unprocessable =
Class.new(Error)
EXCEPTION_MAPPING =
{
  400 => BadRequest,
  401 => Unauthorized,
  404 => NotFound,
  409 => Conflict,
  422 => Unprocessable,
  429 => RateLimitExceeded,
  500 => Unexpected,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Response

Returns a new instance of Response.



42
43
44
45
46
47
# File 'lib/ey-core/response.rb', line 42

def initialize(opts = {})
  @status  = opts[:status]
  @headers = opts[:headers]
  @body    = opts[:body]
  @request = opts[:request]
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



40
41
42
# File 'lib/ey-core/response.rb', line 40

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



40
41
42
# File 'lib/ey-core/response.rb', line 40

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



40
41
42
# File 'lib/ey-core/response.rb', line 40

def request
  @request
end

#statusObject (readonly)

Returns the value of attribute status.



40
41
42
# File 'lib/ey-core/response.rb', line 40

def status
  @status
end

Instance Method Details

#raise!Object



53
54
55
56
57
58
# File 'lib/ey-core/response.rb', line 53

def raise!
  if !successful?
    raise (EXCEPTION_MAPPING[self.status] || Error).new(self)
  else self
  end
end

#successful?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ey-core/response.rb', line 49

def successful?
  self.status >= 200 && self.status <= 299 || self.status == 304
end