Class: Lowdown::Response
- Inherits:
-
Struct
- Object
- Struct
- Lowdown::Response
- 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
-
#headers ⇒ Hash
The HTTP response headers from the service.
-
#raw_body ⇒ String
The JSON encoded response body from the service.
Instance Method Summary collapse
-
#body ⇒ Hash?
The response payload from the service, which is empty in the case of a successful delivery.
-
#failure_reason ⇒ String?
The reason for a failed delivery.
-
#id ⇒ String
Either the Notification#id or, if none was provided, an ID generated by the service.
-
#inspect ⇒ String
A formatted description of the response used for debugging.
-
#invalid_token? ⇒ Boolean
Whether or not the delivery has failed due to a token no longer being valid.
-
#message ⇒ String
The message belonging to the #status returned by the service.
-
#status ⇒ Integer
The HTTP status returned by the service.
-
#success? ⇒ Boolean
Whether or not the notification has been delivered.
-
#to_s ⇒ String
A formatted description of the response.
-
#unformatted_id(unformatted_id_length = nil) ⇒ String
Tries to convert the ID back to the Notification Notification#id by removing leading zeroes.
-
#validity_last_checked_at ⇒ Time?
In case of an invalid token, the time at which the service last checked it.
Instance Attribute Details
#headers ⇒ Hash
The HTTP response headers from the service.
13 14 15 |
# File 'lib/lowdown/response.rb', line 13 def headers @headers end |
#raw_body ⇒ String
The JSON encoded response body from the service.
13 14 15 |
# File 'lib/lowdown/response.rb', line 13 def raw_body @raw_body end |
Instance Method Details
#body ⇒ Hash?
Returns 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_reason ⇒ String?
Returns the reason for a failed delivery.
83 84 85 |
# File 'lib/lowdown/response.rb', line 83 def failure_reason body["reason"] unless success? end |
#id ⇒ String
Returns 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 |
#inspect ⇒ String
Returns 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.
90 91 92 |
# File 'lib/lowdown/response.rb', line 90 def invalid_token? status == 410 end |
#message ⇒ String
Returns the message belonging to the #status returned by the service.
62 63 64 |
# File 'lib/lowdown/response.rb', line 62 def STATUS_CODES[status] end |
#status ⇒ Integer
Returns the HTTP status returned by the service.
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.
69 70 71 |
# File 'lib/lowdown/response.rb', line 69 def success? status == 200 end |
#to_s ⇒ String
Returns a formatted description of the response.
104 105 106 107 108 109 |
# File 'lib/lowdown/response.rb', line 104 def to_s s = "#{status} (#{})" 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.
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_at ⇒ Time?
Returns 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 |