gcm_for_chrome 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ notifications:
7
+ emails:
8
+ - henteko07@gmail.com
9
+ on_success: always # default: change
10
+ on_failure: always
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gcm_for_chrome.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 henteko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
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)
3
+
4
+ GCM for Chrome sends notifications to Chrome extensions via [GCM for Chrome](http://developer.chrome.com/apps/cloudMessaging.html)
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'gcm_for_chrome'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gcm_for_chrome
20
+
21
+ ## Usage
22
+
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')
27
+
28
+ share_data = {
29
+ :title => 'test',
30
+ :data => 'hello! Chrome!!'
31
+ }.to_json
32
+ channel_ids = ['your_channel_id', 'your_channel_id2']
33
+
34
+ # Sends notification
35
+ gcmc.send_notification(channel_ids, 'subchannel_id', share_data)
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
45
+
46
+ ## Testing
47
+
48
+ ```sh
49
+ $ bundle exec rake
50
+ ```
51
+
52
+ ## License and copyright
53
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => [:spec]
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ spec.rspec_opts = ['-cfs']
9
+ end
10
+ rescue LoadError => e
11
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gcm_for_chrome/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "gcm_for_chrome"
8
+ gem.version = GcmForChrome::VERSION
9
+ gem.authors = ["henteko"]
10
+ gem.email = ["henteko07@gmail.com"]
11
+ gem.description = %q{Google Cloud Message for Chrome gem}
12
+ gem.summary = %q{Google Cloud Message for Chrome gem}
13
+ gem.homepage = "https://github.com/henteko/gcm_for_chrome"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rest-client"
21
+ gem.add_dependency "json"
22
+ gem.add_development_dependency 'rake', '~> 0.9.2.2'
23
+ gem.add_development_dependency 'rdoc', '~> 3.12'
24
+ gem.add_development_dependency 'rspec'
25
+ end
@@ -0,0 +1,3 @@
1
+ class GcmForChrome
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,81 @@
1
+ require 'gcm_for_chrome/version'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'rest_client'
6
+
7
+ class GcmForChrome
8
+ NOTIFICATION_URL = 'https://www.googleapis.com/gcm_for_chrome/v1/messages'
9
+ REFRESH_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'
10
+
11
+ def initialize()
12
+ @access_token = ''
13
+ end
14
+
15
+ def send_notification(channel_ids, subchannel_id, payload)
16
+ check_notification_value(channel_ids, subchannel_id, payload)
17
+ initheader = init_header()
18
+
19
+ responses = []
20
+ channel_ids.each do |channel_id|
21
+ _payload = create_notification_payload(
22
+ channel_id, subchannel_id, payload
23
+ ).to_json
24
+ responses.push(restclient_post(NOTIFICATION_URL, _payload, initheader))
25
+ end
26
+
27
+ return responses
28
+ end
29
+
30
+ def set_access_token(client_id, client_secret, refresh_token)
31
+ @access_token = get_access_token(client_id, client_secret, refresh_token)
32
+ end
33
+
34
+ def get_access_token(client_id, client_secret, refresh_token)
35
+ response = refresh_access_token(client_id, client_secret, refresh_token)
36
+ return response['access_token']
37
+ end
38
+
39
+ def refresh_access_token(client_id, client_secret, refresh_token)
40
+ payload = create_refresh_access_token_payload(client_id, client_secret, refresh_token)
41
+ return JSON.parse(restclient_post(REFRESH_TOKEN_URL, payload))
42
+ end
43
+
44
+ def init_header
45
+ return {
46
+ 'Content-Type' =>'application/json',
47
+ 'Authorization' => "Bearer #{@access_token}"
48
+ }
49
+ end
50
+
51
+ def create_notification_payload(channel_id, subchannel_id, payload)
52
+ return {
53
+ "channelId" => channel_id,
54
+ "subchannelId" => subchannel_id,
55
+ "payload" => payload
56
+ }
57
+ end
58
+
59
+ def create_refresh_access_token_payload(client_id, client_secret, refresh_token)
60
+ return {
61
+ 'client_id' => client_id,
62
+ 'client_secret' => client_secret,
63
+ 'refresh_token' => refresh_token,
64
+ 'grant_type' => 'refresh_token'
65
+ }
66
+ end
67
+
68
+ private
69
+
70
+ def check_notification_value(channel_ids, subchannel_id, payload)
71
+ 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 == ''
73
+ raise 'subchannel_id blank.' if subchannel_id == '' || subchannel_id == nil
74
+ raise 'payload blank.' if payload == '' || payload == nil
75
+ end
76
+
77
+ def restclient_post(url, payload, header = {})
78
+ response = RestClient.post(url, payload, header)
79
+ return response
80
+ end
81
+ end
@@ -0,0 +1,97 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+ require 'json'
3
+
4
+ describe GcmForChrome do
5
+ ACCESS_TOKEN = 'TESTACCESS_TOKEN'
6
+ CLIENT_ID = 'TESTCLIENT_ID'
7
+ CLIENT_SECRET = 'TESTCLIENT_SECRET'
8
+ REFRESH_TOKEN = 'TESTREFRESH_TOKEN'
9
+
10
+ before do
11
+ RestClient.stub(:post) do |url, payload, header|
12
+ {
13
+ access_token: ACCESS_TOKEN
14
+ }.to_json
15
+ end
16
+ @gcmc = GcmForChrome.new
17
+ end
18
+
19
+ it "get_access_token" do
20
+ access_token = @gcmc.get_access_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
21
+ access_token.should == ACCESS_TOKEN
22
+ end
23
+
24
+ it "refresh_access_token" do
25
+ response = @gcmc.refresh_access_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
26
+ response.instance_of?(JSON)
27
+ end
28
+
29
+ describe "init_header" do
30
+ it "access_token blank" do
31
+ @gcmc.init_header.should == {
32
+ 'Content-Type' =>'application/json',
33
+ 'Authorization' => "Bearer "
34
+ }
35
+ end
36
+
37
+ it "set_access_token" do
38
+ @gcmc.set_access_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
39
+ @gcmc.init_header.should == {
40
+ 'Content-Type' =>'application/json',
41
+ 'Authorization' => "Bearer #{ACCESS_TOKEN}"
42
+ }
43
+ end
44
+ end
45
+
46
+ it "create_notification_payload" do
47
+ channel_id = 'test'
48
+ subchannel_id = '0'
49
+ payload = {test: 'hoge'}
50
+ @gcmc.create_notification_payload(channel_id, subchannel_id, payload).should == {
51
+ "channelId" => channel_id,
52
+ "subchannelId" => subchannel_id,
53
+ "payload" => payload
54
+ }
55
+ end
56
+
57
+ it "create_refresh_access_token_payload" do
58
+ @gcmc.create_refresh_access_token_payload(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN).should == {
59
+ 'client_id' => CLIENT_ID,
60
+ 'client_secret' => CLIENT_SECRET,
61
+ 'refresh_token' => REFRESH_TOKEN,
62
+ 'grant_type' => 'refresh_token'
63
+ }
64
+ end
65
+
66
+ describe "sent_notification" do
67
+ channel_ids = ['test1', 'test2']
68
+ subchannel_id = '0'
69
+ payload = {title: 'test'}.to_json
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
+ it "channel_ids blank" do
77
+ proc {
78
+ @gcmc.send_notification([], subchannel_id, payload)
79
+ }.should raise_error
80
+ end
81
+ it "subchannel_id blank" do
82
+ proc {
83
+ @gcmc.send_notification(channel_ids, '', payload)
84
+ }.should raise_error
85
+ end
86
+ it "payload blank" do
87
+ proc {
88
+ @gcmc.send_notification(channel_ids, subchannel_id, '')
89
+ }.should raise_error
90
+ end
91
+ it "success" do
92
+ @gcmc.set_access_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
93
+ responses = @gcmc.send_notification(channel_ids, subchannel_id, payload)
94
+ responses.instance_of?(Array)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1 @@
1
+ require 'gcm_for_chrome'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gcm_for_chrome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - henteko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-04 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: &70301666421900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70301666421900
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &70301666421480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70301666421480
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70301666415080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.2.2
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70301666415080
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc
49
+ requirement: &70301666414540 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70301666414540
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &70301666414160 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70301666414160
69
+ description: Google Cloud Message for Chrome gem
70
+ email:
71
+ - henteko07@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - gcm_for_chrome.gemspec
84
+ - lib/gcm_for_chrome.rb
85
+ - lib/gcm_for_chrome/version.rb
86
+ - spec/gcm_for_chrome_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/henteko/gcm_for_chrome
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.10
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Google Cloud Message for Chrome gem
112
+ test_files:
113
+ - spec/gcm_for_chrome_spec.rb
114
+ - spec/spec_helper.rb