Class: Sift::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/sift/client.rb

Overview

Represents the payload returned from a call through the track API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, http_response_code, http_raw_response) ⇒ Response

Constructor

Parameters:

http_response

The HTTP body text returned from the API call. The body is expected to be a JSON object that can be decoded into status, message and request sections.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sift/client.rb', line 31

def initialize(http_response, http_response_code, http_raw_response)
  @http_status_code = http_response_code
  @http_raw_response = http_raw_response

  # only set these variables if a message-body is expected.
  if not @http_raw_response.kind_of? Net::HTTPNoContent
    @body = MultiJson.load(http_response) unless http_response.nil?
    @request = MultiJson.load(@body["request"].to_s) if @body["request"]
    @api_status = @body["status"].to_i if @body["status"]
    @api_error_message = @body["error_message"]

    if @body["error"]
      @api_error_message = @body["error"]
      @api_error_description = @body["description"]
      @api_error_issues = @body["issues"] || {}
    end
  end
end

Instance Attribute Details

#api_error_descriptionObject (readonly)

Returns the value of attribute api_error_description.



13
14
15
# File 'lib/sift/client.rb', line 13

def api_error_description
  @api_error_description
end

#api_error_issuesObject (readonly)

Returns the value of attribute api_error_issues.



13
14
15
# File 'lib/sift/client.rb', line 13

def api_error_issues
  @api_error_issues
end

#api_error_messageObject (readonly)

Returns the value of attribute api_error_message.



13
14
15
# File 'lib/sift/client.rb', line 13

def api_error_message
  @api_error_message
end

#api_statusObject (readonly)

Returns the value of attribute api_status.



13
14
15
# File 'lib/sift/client.rb', line 13

def api_status
  @api_status
end

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/sift/client.rb', line 13

def body
  @body
end

#http_classObject (readonly)

Returns the value of attribute http_class.



13
14
15
# File 'lib/sift/client.rb', line 13

def http_class
  @http_class
end

#http_status_codeObject (readonly)

Returns the value of attribute http_status_code.



13
14
15
# File 'lib/sift/client.rb', line 13

def http_status_code
  @http_status_code
end

#requestObject (readonly)

Returns the value of attribute request.



13
14
15
# File 'lib/sift/client.rb', line 13

def request
  @request
end

Instance Method Details

#jsonObject

DEPRECATED Getter method for deprecated ‘json’ member variable.



70
71
72
# File 'lib/sift/client.rb', line 70

def json
  @body
end

#ok?Boolean

Helper method returns true if and only if the response from the API call was successful

Returns:

true on success; false otherwise

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
# File 'lib/sift/client.rb', line 57

def ok?
  if @http_raw_response.kind_of? Net::HTTPNoContent
    #if there is no content expected, use HTTP code
    204 == @http_status_code
  else
    # otherwise use API status
    @http_raw_response.kind_of? Net::HTTPOK and 0 == @api_status.to_i
  end
end

#original_requestObject

DEPRECATED Getter method for deprecated ‘original_request’ member variable.



76
77
78
# File 'lib/sift/client.rb', line 76

def original_request
  @request
end