Class: Latch::LatchResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/latch/latch_response.rb

Overview

This class models a response from any of the endpoints in the Latch API. It consists of a “data” and an “error” elements. Although normally only one of them will be present, they are not mutually exclusive, since errors can be non fatal, and therefore a response could have valid information in the data field and at the same time inform of an error.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jsonString) ⇒ LatchResponse

Returns a new instance of LatchResponse.

Parameters:

  • jsonString

    a json string received from one of the methods of the Latch API



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/latch/latch_response.rb', line 28

def initialize(jsonString)
json = JSON.parse(jsonString)

if(json != nil)
  if (json.has_key?("data"))
    @data = json["data"]
  end

  if (json.has_key?("error"))
    @error = Error.new(json["error"])
  end
end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



24
25
26
# File 'lib/latch/latch_response.rb', line 24

def data
  @data
end

#errorObject

Returns the value of attribute error.



25
26
27
# File 'lib/latch/latch_response.rb', line 25

def error
  @error
end

Instance Method Details

#to_jsonObject

Get JSON String that represents the LatchResponse object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/latch/latch_response.rb', line 43

def to_json
  response = {}

  if (@data != nil)
    response["data"] = @data
  end

  if (@error != nil)
    response["error"] = @error.to_json
  end
  response.to_json
end