Module: FIRE

Includes:
HTTParty
Defined in:
lib/pushmeup/fire/core.rb,
lib/pushmeup/fire/notification.rb

Defined Under Namespace

Classes: Notification

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.access_tokenObject

Returns the value of attribute access_token.



16
17
18
# File 'lib/pushmeup/fire/core.rb', line 16

def access_token
  @access_token
end

.access_token_expirationObject

Returns the value of attribute access_token_expiration.



16
17
18
# File 'lib/pushmeup/fire/core.rb', line 16

def access_token_expiration
  @access_token_expiration
end

.client_idObject

Returns the value of attribute client_id.



16
17
18
# File 'lib/pushmeup/fire/core.rb', line 16

def client_id
  @client_id
end

.client_secretObject

Returns the value of attribute client_secret.



16
17
18
# File 'lib/pushmeup/fire/core.rb', line 16

def client_secret
  @client_secret
end

.hostObject

Returns the value of attribute host.



16
17
18
# File 'lib/pushmeup/fire/core.rb', line 16

def host
  @host
end

Class Method Details

.get_access_tokenObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pushmeup/fire/core.rb', line 42

def self.get_access_token
  headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
  body = {grant_type:    'client_credentials',
          scope:         'messaging:push',
          client_id:     self.client_id,
          client_secret: self.client_secret
  }
  params = {headers: headers, body: body}
  res = self.post('https://api.amazon.com/auth/O2/token', params)
  return res.parsed_response if res.response.code.to_i == 200
  raise 'Error getting access token'
end

.prepare_tokenObject



33
34
35
36
37
38
39
40
# File 'lib/pushmeup/fire/core.rb', line 33

def self.prepare_token
  return if Time.now < self.access_token_expiration

  token = self.get_access_token
  self.access_token = token['access_token']
  expires_in_sec    = token['expires_in']
  self.access_token_expiration = Time.now + expires_in_sec - 60
end

.send_notification(device_token, data = {}, options = {}) ⇒ Object



19
20
21
22
# File 'lib/pushmeup/fire/core.rb', line 19

def self.send_notification(device_token, data = {}, options = {})
  n = FIRE::Notification.new(device_token, data, options)
  self.send_notifications([n])
end

.send_notifications(notifications) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/pushmeup/fire/core.rb', line 24

def self.send_notifications(notifications)
  self.prepare_token
  responses = []
  notifications.each do |n|
    responses << self.prepare_and_send(n)
  end
  responses
end