Class: GcmForChrome

Inherits:
Object
  • Object
show all
Defined in:
lib/gcm_for_chrome.rb,
lib/gcm_for_chrome/version.rb

Constant Summary collapse

NOTIFICATION_URL =
'https://www.googleapis.com/gcm_for_chrome/v1/messages'
REFRESH_TOKEN_URL =
'https://accounts.google.com/o/oauth2/token'
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initializeGcmForChrome

Returns a new instance of GcmForChrome.



11
12
13
# File 'lib/gcm_for_chrome.rb', line 11

def initialize()
  @access_token = '' 
end

Instance Method Details

#create_notification_payload(channel_id, subchannel_id, payload) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/gcm_for_chrome.rb', line 51

def create_notification_payload(channel_id, subchannel_id, payload)
  return {
    "channelId" => channel_id,
    "subchannelId" => subchannel_id,
    "payload" => payload
  }
end

#create_refresh_access_token_payload(client_id, client_secret, refresh_token) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/gcm_for_chrome.rb', line 59

def create_refresh_access_token_payload(client_id, client_secret, refresh_token)
  return {
    'client_id' => client_id,
    'client_secret' => client_secret,
    'refresh_token' => refresh_token,
    'grant_type' => 'refresh_token'
  }
end

#get_access_token(client_id, client_secret, refresh_token) ⇒ Object



34
35
36
37
# File 'lib/gcm_for_chrome.rb', line 34

def get_access_token(client_id, client_secret, refresh_token)
  response = refresh_access_token(client_id, client_secret, refresh_token)
  return response['access_token']
end

#init_headerObject



44
45
46
47
48
49
# File 'lib/gcm_for_chrome.rb', line 44

def init_header
  return {
    'Content-Type' =>'application/json',
    'Authorization' => "Bearer #{@access_token}"
  }
end

#refresh_access_token(client_id, client_secret, refresh_token) ⇒ Object



39
40
41
42
# File 'lib/gcm_for_chrome.rb', line 39

def refresh_access_token(client_id, client_secret, refresh_token)
  payload = create_refresh_access_token_payload(client_id, client_secret, refresh_token)
  return JSON.parse(restclient_post(REFRESH_TOKEN_URL, payload))
end

#send_notification(channel_ids, subchannel_id, payload) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gcm_for_chrome.rb', line 15

def send_notification(channel_ids, subchannel_id, payload)
  check_notification_value(channel_ids, subchannel_id, payload)
  initheader = init_header()

  responses = []
  channel_ids.each do |channel_id|
    _payload = create_notification_payload(
      channel_id, subchannel_id, payload
    ).to_json 
    responses.push(restclient_post(NOTIFICATION_URL, _payload, initheader))
  end
  
  return responses
end

#set_access_token(client_id, client_secret, refresh_token) ⇒ Object



30
31
32
# File 'lib/gcm_for_chrome.rb', line 30

def set_access_token(client_id, client_secret, refresh_token)
  @access_token = get_access_token(client_id, client_secret, refresh_token)
end