Class: Maia::FCM::Response
- Inherits:
-
Object
- Object
- Maia::FCM::Response
- Defined in:
- lib/maia/fcm/response.rb
Instance Attribute Summary collapse
-
#http_response ⇒ Object
readonly
Returns the value of attribute http_response.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #error ⇒ Object
- #fail? ⇒ Boolean
-
#initialize(http_response, tokens = []) ⇒ Response
constructor
A new instance of Response.
- #results ⇒ Object
- #retry_after ⇒ Object
- #status ⇒ Object
- #success? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(http_response, tokens = []) ⇒ Response
Returns a new instance of Response.
6 7 8 9 |
# File 'lib/maia/fcm/response.rb', line 6 def initialize(http_response, tokens = []) @http_response = http_response @tokens = tokens end |
Instance Attribute Details
#http_response ⇒ Object (readonly)
Returns the value of attribute http_response.
4 5 6 |
# File 'lib/maia/fcm/response.rb', line 4 def http_response @http_response end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
4 5 6 |
# File 'lib/maia/fcm/response.rb', line 4 def tokens @tokens end |
Instance Method Details
#error ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/maia/fcm/response.rb', line 33 def error case status when 400 'Invalid JSON was sent to FCM.' when 401 'Authentication error with FCM. Check the server whitelist and the validity of your project key.' when 500..599 'FCM Internal server error.' end end |
#fail? ⇒ Boolean
19 20 21 |
# File 'lib/maia/fcm/response.rb', line 19 def fail? !success? end |
#results ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/maia/fcm/response.rb', line 23 def results @_results ||= begin results = to_h.fetch 'results', [] results.map!.with_index do |attributes, i| Result.new attributes, tokens[i] end ResultCollection.new(results) end end |
#retry_after ⇒ Object
44 45 46 |
# File 'lib/maia/fcm/response.rb', line 44 def retry_after http_response.headers['Retry-After'] end |
#status ⇒ Object
11 12 13 |
# File 'lib/maia/fcm/response.rb', line 11 def status http_response.code end |
#success? ⇒ Boolean
15 16 17 |
# File 'lib/maia/fcm/response.rb', line 15 def success? (200..399).cover? status end |
#to_h ⇒ Object
48 49 50 51 52 |
# File 'lib/maia/fcm/response.rb', line 48 def to_h JSON.parse http_response.body rescue JSON::ParserError {} end |