Class: Maia::FCM::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/maia/fcm/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_responseObject (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

#tokensObject (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

#errorObject



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

Returns:

  • (Boolean)


19
20
21
# File 'lib/maia/fcm/response.rb', line 19

def fail?
  !success?
end

#resultsObject



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_afterObject



44
45
46
# File 'lib/maia/fcm/response.rb', line 44

def retry_after
  http_response.headers['Retry-After']
end

#statusObject



11
12
13
# File 'lib/maia/fcm/response.rb', line 11

def status
  http_response.code
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  (200..399).cover? status
end

#to_hObject



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