Class: GCM::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/mercurius/gcm/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, tokens = []) ⇒ Response



5
6
7
8
# File 'lib/mercurius/gcm/response.rb', line 5

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.



3
4
5
# File 'lib/mercurius/gcm/response.rb', line 3

def http_response
  @http_response
end

#tokensObject (readonly)

Returns the value of attribute tokens.



3
4
5
# File 'lib/mercurius/gcm/response.rb', line 3

def tokens
  @tokens
end

Instance Method Details

#errorObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mercurius/gcm/response.rb', line 32

def error
  case status
  when 401
    'Authentication error with GCM. Check the server whitelist and the validity of your project key.'
  when 400
    'Invalid JSON was sent to GCM.'
  when 500..599
    'GCM Internal server error.'
  else
    nil
  end
end

#fail?Boolean



18
19
20
# File 'lib/mercurius/gcm/response.rb', line 18

def fail?
  !success?
end

#resultsObject



22
23
24
25
26
27
28
29
30
# File 'lib/mercurius/gcm/response.rb', line 22

def results
  @_results ||= begin
    results = to_h.fetch 'results', []
    results.map!.with_index do |attributes, i|
      GCM::Result.new attributes, tokens[i]
    end
    ResultCollection.new(results)
  end
end

#retry_afterObject



45
46
47
# File 'lib/mercurius/gcm/response.rb', line 45

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

#statusObject



10
11
12
# File 'lib/mercurius/gcm/response.rb', line 10

def status
  http_response.status
end

#success?Boolean



14
15
16
# File 'lib/mercurius/gcm/response.rb', line 14

def success?
  http_response.success?
end

#to_hObject



49
50
51
52
53
# File 'lib/mercurius/gcm/response.rb', line 49

def to_h
  JSON.parse http_response.body
rescue JSON::ParserError
  {}
end