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", }.freeze
- INVALID_TOKEN_REASONS =
The reasons that indicate a device token not being valid besides just being unregistered.
%( Unregistered BadDeviceToken DeviceTokenNotForTopic ).freeze
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
-
#activity_last_checked_at ⇒ Time?
In case of an inactive token, the time at which the service last verified it.
-
#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.
-
#inactive_token? ⇒ Boolean
Whether or not the delivery has failed due to a token no longer being active.
-
#inspect ⇒ String
A formatted description of the response used for debugging.
-
#invalid_token? ⇒ Boolean
Whether or not the token is invalid for any of the reasons listed in INVALID_TOKEN_REASONS.
-
#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.
Instance Attribute Details
#headers ⇒ Hash
The HTTP response headers from the service.
15 16 17 |
# File 'lib/lowdown/response.rb', line 15 def headers @headers end |
#raw_body ⇒ String
The JSON encoded response body from the service.
15 16 17 |
# File 'lib/lowdown/response.rb', line 15 def raw_body @raw_body end |
Instance Method Details
#activity_last_checked_at ⇒ Time?
Returns in case of an inactive token, the time at which the service last verified it.
97 98 99 |
# File 'lib/lowdown/response.rb', line 97 def activity_last_checked_at Time.at(body["timestamp"].to_i / 1000) if inactive_token? end |
#body ⇒ Hash?
Returns the response payload from the service, which is empty in the case of a successful delivery.
69 70 71 |
# File 'lib/lowdown/response.rb', line 69 def body JSON.parse(raw_body) if raw_body end |
#failure_reason ⇒ String?
Returns the reason for a failed delivery.
76 77 78 |
# File 'lib/lowdown/response.rb', line 76 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.
37 38 39 |
# File 'lib/lowdown/response.rb', line 37 def id headers["apns-id"] end |
#inactive_token? ⇒ Boolean
Returns whether or not the delivery has failed due to a token no longer being active.
90 91 92 |
# File 'lib/lowdown/response.rb', line 90 def inactive_token? status == 410 end |
#inspect ⇒ String
Returns a formatted description of the response used for debugging.
113 114 115 |
# File 'lib/lowdown/response.rb', line 113 def inspect "#<Lowdown::Connection::Response #{to_s}>" end |
#invalid_token? ⇒ Boolean
Returns whether or not the token is invalid for any of the reasons listed in INVALID_TOKEN_REASONS.
83 84 85 |
# File 'lib/lowdown/response.rb', line 83 def invalid_token? !success? && INVALID_TOKEN_REASONS.include?(failure_reason) end |
#message ⇒ String
Returns the message belonging to the #status returned by the service.
55 56 57 |
# File 'lib/lowdown/response.rb', line 55 def STATUS_CODES[status] end |
#status ⇒ Integer
Returns the HTTP status returned by the service.
46 47 48 |
# File 'lib/lowdown/response.rb', line 46 def status headers[":status"].to_i end |
#success? ⇒ Boolean
Returns whether or not the notification has been delivered.
62 63 64 |
# File 'lib/lowdown/response.rb', line 62 def success? status == 200 end |
#to_s ⇒ String
Returns a formatted description of the response.
104 105 106 107 108 |
# File 'lib/lowdown/response.rb', line 104 def to_s reason = ": #{failure_reason}" unless success? last_check = " last checked at #{activity_last_checked_at}" if inactive_token? "#{status} (#{})#{reason}#{last_check}" end |