gcm_for_chrome 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Google Cloud Messaging for Chrome (GCM for Chrome)
2
- [![Build Status](https://travis-ci.org/henteko/gcm_for_chrome.png?branch=master)](https://travis-ci.org/henteko/gcm_for_chrome)
2
+ [![Build Status](https://travis-ci.org/henteko/gcm_for_chrome.png?branch=master)](https://travis-ci.org/henteko/gcm_for_chrome)
3
3
 
4
4
  GCM for Chrome sends notifications to Chrome extensions via [GCM for Chrome](http://developer.chrome.com/apps/cloudMessaging.html)
5
5
 
@@ -21,9 +21,7 @@ Or install it yourself as:
21
21
  ## Usage
22
22
 
23
23
  ```ruby
24
- gcmc = GcmForChrome.new
25
- # Set up access_token
26
- gcmc.set_access_token('your_client_id', 'your_client_secret', 'your_refresh_token')
24
+ gcmc = GcmForChrome.new('your_client_id', 'your_client_secret', 'your_refresh_token')
27
25
 
28
26
  share_data = {
29
27
  :title => 'test',
@@ -8,32 +8,42 @@ class GcmForChrome
8
8
  NOTIFICATION_URL = 'https://www.googleapis.com/gcm_for_chrome/v1/messages'
9
9
  REFRESH_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'
10
10
 
11
- def initialize()
12
- @access_token = ''
11
+ def initialize(client_id = nil, client_secret = nil, refresh_token = nil)
12
+ @access_token = nil
13
+ @access_token_expires_at = nil
14
+ @client_id = client_id
15
+ @client_secret = client_secret
16
+ @refresh_token = refresh_token
13
17
  end
14
18
 
15
19
  def send_notification(channel_ids, subchannel_id, payload)
20
+ if @access_token_expires_at.nil? or (@access_token_expires_at and @access_token_expires_at < (Time.new + 10))
21
+ set_access_token(@client_id, @client_secret, @refresh_token)
22
+ end
16
23
  check_notification_value(channel_ids, subchannel_id, payload)
17
- initheader = init_header()
24
+ initheader = init_header
18
25
 
19
26
  responses = []
20
27
  channel_ids.each do |channel_id|
21
28
  _payload = create_notification_payload(
22
29
  channel_id, subchannel_id, payload
23
- ).to_json
30
+ ).to_json
24
31
  responses.push(restclient_post(NOTIFICATION_URL, _payload, initheader))
25
32
  end
26
-
33
+
27
34
  return responses
28
35
  end
29
36
 
30
37
  def set_access_token(client_id, client_secret, refresh_token)
31
- @access_token = get_access_token(client_id, client_secret, refresh_token)
38
+ @client_id ||= client_id
39
+ @client_secret ||= client_secret
40
+ @refresh_token ||= refresh_token
41
+ @access_token, @access_token_expires_at = get_access_token(@client_id, @client_secret, @refresh_token)
32
42
  end
33
43
 
34
44
  def get_access_token(client_id, client_secret, refresh_token)
35
45
  response = refresh_access_token(client_id, client_secret, refresh_token)
36
- return response['access_token']
46
+ return response['access_token'], Time.at(Time.now + response['expires_in'].to_i)
37
47
  end
38
48
 
39
49
  def refresh_access_token(client_id, client_secret, refresh_token)
@@ -54,7 +64,7 @@ class GcmForChrome
54
64
  "subchannelId" => subchannel_id,
55
65
  "payload" => payload
56
66
  }
57
- end
67
+ end
58
68
 
59
69
  def create_refresh_access_token_payload(client_id, client_secret, refresh_token)
60
70
  return {
@@ -69,7 +79,7 @@ class GcmForChrome
69
79
 
70
80
  def check_notification_value(channel_ids, subchannel_id, payload)
71
81
  raise 'Not access_token. Please set_access_token.' if @access_token == '' || @access_token == nil
72
- raise 'channel_ids blank.' if channel_ids == [] || channel_ids == nil ||channel_ids == ''
82
+ raise 'channel_ids blank.' if channel_ids == [] || channel_ids == nil ||channel_ids == ''
73
83
  raise 'subchannel_id blank.' if subchannel_id == '' || subchannel_id == nil
74
84
  raise 'payload blank.' if payload == '' || payload == nil
75
85
  end
@@ -1,3 +1,3 @@
1
1
  class GcmForChrome
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,21 +3,21 @@ require 'json'
3
3
 
4
4
  describe GcmForChrome do
5
5
  ACCESS_TOKEN = 'TESTACCESS_TOKEN'
6
- CLIENT_ID = 'TESTCLIENT_ID'
6
+ CLIENT_ID = 'TESTCLIENT_ID'
7
7
  CLIENT_SECRET = 'TESTCLIENT_SECRET'
8
8
  REFRESH_TOKEN = 'TESTREFRESH_TOKEN'
9
9
 
10
10
  before do
11
11
  RestClient.stub(:post) do |url, payload, header|
12
12
  {
13
- access_token: ACCESS_TOKEN
13
+ access_token: ACCESS_TOKEN
14
14
  }.to_json
15
15
  end
16
16
  @gcmc = GcmForChrome.new
17
17
  end
18
18
 
19
19
  it "get_access_token" do
20
- access_token = @gcmc.get_access_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
20
+ access_token, access_token_expires_at = @gcmc.get_access_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
21
21
  access_token.should == ACCESS_TOKEN
22
22
  end
23
23
 
@@ -68,11 +68,6 @@ describe GcmForChrome do
68
68
  subchannel_id = '0'
69
69
  payload = {title: 'test'}.to_json
70
70
 
71
- it "access_token blank" do
72
- proc {
73
- @gcmc.send_notification(channel_ids, subchannel_id, payload)
74
- }.should raise_error
75
- end
76
71
  it "channel_ids blank" do
77
72
  proc {
78
73
  @gcmc.send_notification([], subchannel_id, payload)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcm_for_chrome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-04 00:00:00.000000000Z
12
+ date: 2013-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &70301666421900 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70301666421900
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: json
27
- requirement: &70301666421480 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70301666421480
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rake
38
- requirement: &70301666415080 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: 0.9.2.2
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70301666415080
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2.2
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rdoc
49
- requirement: &70301666414540 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ~>
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '3.12'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70301666414540
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.12'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: rspec
60
- requirement: &70301666414160 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ! '>='
@@ -65,7 +85,12 @@ dependencies:
65
85
  version: '0'
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *70301666414160
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
69
94
  description: Google Cloud Message for Chrome gem
70
95
  email:
71
96
  - henteko07@gmail.com
@@ -105,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
130
  version: '0'
106
131
  requirements: []
107
132
  rubyforge_project:
108
- rubygems_version: 1.8.10
133
+ rubygems_version: 1.8.23
109
134
  signing_key:
110
135
  specification_version: 3
111
136
  summary: Google Cloud Message for Chrome gem