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",
}.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

Instance Method Summary collapse

Instance Attribute Details

#headersHash

The HTTP response headers from the service.

Returns:

  • (Hash)

    the current value of headers



15
16
17
# File 'lib/lowdown/response.rb', line 15

def headers
  @headers
end

#raw_bodyString

The JSON encoded response body from the service.

Returns:

  • (String)

    the current value of raw_body



15
16
17
# File 'lib/lowdown/response.rb', line 15

def raw_body
  @raw_body
end

Instance Method Details

#activity_last_checked_atTime?

Returns in case of an inactive token, the time at which the service last verified it.

Returns:

  • (Time, nil)

    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

#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.



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

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.



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

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.



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.

Returns:

  • (Boolean)

    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

#inspectString

Returns a formatted description of the response used for debugging.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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

#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:



55
56
57
# File 'lib/lowdown/response.rb', line 55

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:



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.

Returns:

  • (Boolean)

    whether or not the notification has been delivered.



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

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
# 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} (#{message})#{reason}#{last_check}"
end