Class: GcmForChrome
- Inherits:
-
Object
- Object
- GcmForChrome
- 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.2.0"
Instance Method Summary collapse
- #create_notification_payload(channel_id, subchannel_id, payload) ⇒ Object
- #create_refresh_access_token_payload(client_id, client_secret, refresh_token) ⇒ Object
- #get_access_token(client_id, client_secret, refresh_token) ⇒ Object
- #init_header ⇒ Object
-
#initialize(client_id = nil, client_secret = nil, refresh_token = nil) ⇒ GcmForChrome
constructor
A new instance of GcmForChrome.
- #refresh_access_token(client_id, client_secret, refresh_token) ⇒ Object
- #send_notification(channel_ids, subchannel_id, payload) ⇒ Object
- #set_access_token(client_id, client_secret, refresh_token) ⇒ Object
Constructor Details
#initialize(client_id = nil, client_secret = nil, refresh_token = nil) ⇒ GcmForChrome
Returns a new instance of GcmForChrome.
11 12 13 14 15 16 17 |
# File 'lib/gcm_for_chrome.rb', line 11 def initialize(client_id = nil, client_secret = nil, refresh_token = nil) @access_token = nil @access_token_expires_at = nil @client_id = client_id @client_secret = client_secret @refresh_token = refresh_token end |
Instance Method Details
#create_notification_payload(channel_id, subchannel_id, payload) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/gcm_for_chrome.rb', line 61 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
69 70 71 72 73 74 75 76 |
# File 'lib/gcm_for_chrome.rb', line 69 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
44 45 46 47 |
# File 'lib/gcm_for_chrome.rb', line 44 def get_access_token(client_id, client_secret, refresh_token) response = refresh_access_token(client_id, client_secret, refresh_token) return response['access_token'], Time.at(Time.now + response['expires_in'].to_i) end |
#init_header ⇒ Object
54 55 56 57 58 59 |
# File 'lib/gcm_for_chrome.rb', line 54 def init_header return { 'Content-Type' =>'application/json', 'Authorization' => "Bearer #{@access_token}" } end |
#refresh_access_token(client_id, client_secret, refresh_token) ⇒ Object
49 50 51 52 |
# File 'lib/gcm_for_chrome.rb', line 49 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gcm_for_chrome.rb', line 19 def send_notification(channel_ids, subchannel_id, payload) if @access_token_expires_at.nil? or (@access_token_expires_at and @access_token_expires_at < (Time.new + 10)) set_access_token(@client_id, @client_secret, @refresh_token) end 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
37 38 39 40 41 42 |
# File 'lib/gcm_for_chrome.rb', line 37 def set_access_token(client_id, client_secret, refresh_token) @client_id ||= client_id @client_secret ||= client_secret @refresh_token ||= refresh_token @access_token, @access_token_expires_at = get_access_token(@client_id, @client_secret, @refresh_token) end |