Class: Easy::Api::Result

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/easy/api/result.rb

Overview

Encapsulates the response data of an API call

Expected values to be added are: #status_code success error (see Easy::Api::Error#new)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorEasy::Api::Error?

An instance of Easy::Api::Error or nil if there is no error

Returns:



15
16
17
# File 'lib/easy/api/result.rb', line 15

def error
  @error
end

#status_codeInteger

The HTTP status code to respond with

Returns:

  • (Integer)

    status_code (e.g. 200, 400, 500)

Raises:

  • StandardError if status_code has not been set



28
29
30
# File 'lib/easy/api/result.rb', line 28

def status_code
  @status_code || raise("Easy::Api::Result needs a status_code!")
end

#successtrue, false

Represents whether the request succeeded or not

Returns:

  • (true, false)


21
22
23
# File 'lib/easy/api/result.rb', line 21

def success
  @success || false
end

Instance Method Details

#as_json(options = {}) ⇒ Hash

Used by Rails to render the result as json

Will always contain ‘success’, the error if there is one, and any dynamic attributes.

Returns:

  • (Hash)


36
37
38
39
40
# File 'lib/easy/api/result.rb', line 36

def as_json(options={})
  hash = marshal_dump.merge(success: success)
  hash[:error] = error unless error.nil?
  hash
end