Class: Zooz::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/zooz/response.rb,
lib/zooz/response/open.rb,
lib/zooz/response/verify.rb

Defined Under Namespace

Classes: Open, Verify

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



14
15
16
# File 'lib/zooz/response.rb', line 14

def initialize
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/zooz/response.rb', line 9

def errors
  @errors
end

#http_responseObject

Returns the value of attribute http_response.



8
9
10
# File 'lib/zooz/response.rb', line 8

def http_response
  @http_response
end

#requestObject

Returns the value of attribute request.



8
9
10
# File 'lib/zooz/response.rb', line 8

def request
  @request
end

Instance Method Details

#error_messageObject

Get the ZooZ error message, returned when status_code is not 0.



35
36
37
# File 'lib/zooz/response.rb', line 35

def error_message
  get_parsed_singular('errorMessage')
end

#get_parsed_singular(offset) ⇒ Object

Gets a singular offset from the response data, useful because CGI returns single parameters embedded in an array.



20
21
22
# File 'lib/zooz/response.rb', line 20

def get_parsed_singular(offset)
  (parsed_response[offset] || []).first
end

#parsed_responseObject

Get a parsed response as an object from the response text.



25
26
27
# File 'lib/zooz/response.rb', line 25

def parsed_response
  CGI::parse(http_body.strip)
end

#status_codeObject

Get the ZooZ status code, 0 for success.



30
31
32
# File 'lib/zooz/response.rb', line 30

def status_code
  get_parsed_singular('statusCode')
end

#success?Boolean

Whether the request was successful, populates the @errors array on error.

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zooz/response.rb', line 40

def success?
  @errors = []
  unless http_code.to_s[0,1] == '2'
    @errors << "HTTP status #{http_code}: #{http_message}"
  end
  unless status_code
    @errors << "Unable to parse ZooZ response"
  end
  unless status_code == '0'
    @errors << "ZooZ error #{status_code}: #{error_message}"
  end
  @errors.empty?
end