Class: Sendgrid::Web::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/web/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code, body) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
# File 'lib/sendgrid/web/response.rb', line 5

def initialize(status_code, body)
  @status_code = status_code.to_i
  @raw_body = body.to_s
  @parsed_body = ::Oj.safe_load(raw_body)
end

Instance Attribute Details

#parsed_bodyObject (readonly)

Returns the value of attribute parsed_body.



3
4
5
# File 'lib/sendgrid/web/response.rb', line 3

def parsed_body
  @parsed_body
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



3
4
5
# File 'lib/sendgrid/web/response.rb', line 3

def raw_body
  @raw_body
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



3
4
5
# File 'lib/sendgrid/web/response.rb', line 3

def status_code
  @status_code
end

Instance Method Details

#error_messagesArray<String>

Fetches an array of error messages from the response.

Returns:

  • (Array<String>)

    A list of any error messages.



24
25
26
27
28
29
30
31
32
# File 'lib/sendgrid/web/response.rb', line 24

def error_messages
  if errors?
    errors = Array(parsed_body['errors'])
    errors << parsed_body['error']
    errors.compact
  else
    []
  end
end

#errors?bool

Checks if the Sengrid response contained errors.

Returns:

  • (bool)

    True if there were errors found.



14
15
16
17
18
19
# File 'lib/sendgrid/web/response.rb', line 14

def errors?
  !parsed_body.nil? &&
  parsed_body.is_a?(Hash) &&
  (parsed_body.has_key?('errors') ||
  parsed_body.has_key?('error'))
end