Class: Lowdown::Response

Inherits:
Struct
  • Object
show all
Defined in:
lib/lowdown/response.rb

Overview

An object that represents a response from the Apple Push Notification service for a single notification delivery.

Constant Summary collapse

STATUS_CODES =

The possible HTTP status codes and their associated messages.

{
  200 => "Success",
  400 => "Bad request",
  403 => "There was an error with the certificate",
  405 => "The request used a bad :method value. Only POST requests are supported",
  410 => "The device token is no longer active for the topic",
  413 => "The notification payload was too large",
  429 => "The server received too many requests for the same device token",
  500 => "Internal server error",
  503 => "The server is shutting down and unavailable"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headersHash

The HTTP response headers from the service.

Returns:

  • (Hash)

    the current value of headers



13
14
15
# File 'lib/lowdown/response.rb', line 13

def headers
  @headers
end

#raw_bodyString

The JSON encoded response body from the service.

Returns:

  • (String)

    the current value of raw_body



13
14
15
# File 'lib/lowdown/response.rb', line 13

def raw_body
  @raw_body
end

Instance Method Details

#bodyHash?

Returns the response payload from the service, which is empty in the case of a successful delivery.

Returns:

  • (Hash, nil)

    the response payload from the service, which is empty in the case of a successful delivery.



76
77
78
# File 'lib/lowdown/response.rb', line 76

def body
  JSON.parse(raw_body) if raw_body
end

#failure_reasonString?

Returns the reason for a failed delivery.

Returns:

  • (String, nil)

    the reason for a failed delivery.



83
84
85
# File 'lib/lowdown/response.rb', line 83

def failure_reason
  body["reason"] unless success?
end

#idString

Returns either the Notification#id or, if none was provided, an ID generated by the service.

Returns:

  • (String)

    either the Notification#id or, if none was provided, an ID generated by the service.



31
32
33
# File 'lib/lowdown/response.rb', line 31

def id
  headers["apns-id"]
end

#inspectString

Returns a formatted description of the response used for debugging.

Returns:

  • (String)

    a formatted description of the response used for debugging.



114
115
116
# File 'lib/lowdown/response.rb', line 114

def inspect
  "#<Lowdown::Connection::Response #{to_s}>"
end

#invalid_token?Boolean

Returns whether or not the delivery has failed due to a token no longer being valid.

Returns:

  • (Boolean)

    whether or not the delivery has failed due to a token no longer being valid.



90
91
92
# File 'lib/lowdown/response.rb', line 90

def invalid_token?
  status == 410
end

#messageString

Returns the message belonging to the #status returned by the service.

Returns:

  • (String)

    the message belonging to the #status returned by the service.

See Also:



62
63
64
# File 'lib/lowdown/response.rb', line 62

def message
  STATUS_CODES[status]
end

#statusInteger

Returns the HTTP status returned by the service.

Returns:

  • (Integer)

    the HTTP status returned by the service.

See Also:



53
54
55
# File 'lib/lowdown/response.rb', line 53

def status
  headers[":status"].to_i
end

#success?Boolean

Returns whether or not the notification has been delivered.

Returns:

  • (Boolean)

    whether or not the notification has been delivered.



69
70
71
# File 'lib/lowdown/response.rb', line 69

def success?
  status == 200
end

#to_sString

Returns a formatted description of the response.

Returns:

  • (String)

    a formatted description of the response.



104
105
106
107
108
109
# File 'lib/lowdown/response.rb', line 104

def to_s
  s = "#{status} (#{message})"
  s << ": #{failure_reason}" unless success?
  s << " last checked at #{validity_last_checked_at}" if invalid_token?
  s
end

#unformatted_id(unformatted_id_length = nil) ⇒ String

Tries to convert the ID back to the Notification Notification#id by removing leading zeroes.

Parameters:

  • unformatted_id_length (Integer) (defaults to: nil)

    the expected length of an ID, which ensures that required leading zeroes are not removed.

Returns:

  • (String)

    the ID that was assigned to the Notification.



43
44
45
46
# File 'lib/lowdown/response.rb', line 43

def unformatted_id(unformatted_id_length = nil)
  id = self.id.tr('-', '')
  unformatted_id_length ? id[32-unformatted_id_length,unformatted_id_length] : id.gsub(/\A0*/, '')
end

#validity_last_checked_atTime?

Returns in case of an invalid token, the time at which the service last checked it.

Returns:

  • (Time, nil)

    in case of an invalid token, the time at which the service last checked it.



97
98
99
# File 'lib/lowdown/response.rb', line 97

def validity_last_checked_at
  Time.at(body["timestamp"].to_i) if invalid_token?
end