crusade-gcm 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/crusade-gcm.gemspec +31 -0
- data/lib/crusade/gcm.rb +3 -0
- data/lib/crusade/gcm/access_token.rb +42 -0
- data/lib/crusade/gcm/access_token_fetcher.rb +46 -0
- data/lib/crusade/gcm/cache/in_memory_cache.rb +23 -0
- data/lib/crusade/gcm/configuration.rb +15 -0
- data/lib/crusade/gcm/connection.rb +39 -0
- data/lib/crusade/gcm/notification.rb +36 -0
- data/lib/crusade/gcm/push_notification.rb +32 -0
- data/lib/crusade/gcm/version.rb +5 -0
- data/test/fixtures/access_token/success.json +5 -0
- data/test/fixtures/gcm_responses/invalid_channel.json +13 -0
- data/test/fixtures/gcm_responses/invalid_credentials.json +15 -0
- data/test/fixtures/gcm_responses/no_sub_channel.json +13 -0
- data/test/integration/push_notification_test.rb +85 -0
- data/test/support/fixtures.rb +24 -0
- data/test/test_helper.rb +13 -0
- data/test/unit/access_token_fetcher_test.rb +47 -0
- data/test/unit/access_token_test.rb +62 -0
- data/test/unit/cache/in_memory_cache_test.rb +56 -0
- data/test/unit/configuration_test.rb +34 -0
- data/test/unit/connection_test.rb +32 -0
- data/test/unit/notification_test.rb +70 -0
- metadata +212 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 95c18f773a5e1c90aaacbbd28bb2dcd31311123f
|
4
|
+
data.tar.gz: 9832840070849feb92807e696a9b422beb73b540
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 52492f2b6ab5d3e766cde7c42e01056809d46ad68c0223a0cc85bc8c8bebba2401d97b982c0f1fd6517d8dcdeb11e4814f21794f0babb9f587efbc4f436f0e6b
|
7
|
+
data.tar.gz: a22062fb14789264b28b5032733e576cd06a98734df0ee864e8f068026b5ff3495d7ca714e806f7665b19d3f2443267a2333db64bc87ffd0188ba46427575db6
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 David Rouchy
|
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,29 @@
|
|
1
|
+
# Crusade::Gcm
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'crusade-gcm'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install crusade-gcm
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/crusade-gcm.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'crusade/gcm/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "crusade-gcm"
|
8
|
+
spec.version = Crusade::GCM::VERSION
|
9
|
+
spec.authors = ["David Rouchy"]
|
10
|
+
spec.email = ["drouchy@gmail.com"]
|
11
|
+
spec.description = %q{Google Cloud Messaging plugin for crusade}
|
12
|
+
spec.summary = %q{send push notification to chrome app via GCM}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "crusade", Crusade::GCM::VERSION
|
22
|
+
spec.add_dependency "faraday", "~> 0.8.8"
|
23
|
+
|
24
|
+
spec.add_development_dependency "json_expressions", "~> 0.8.2"
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0.7"
|
27
|
+
spec.add_development_dependency "minitest-great_expectations", "~> 0.0.5"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "coveralls"
|
30
|
+
spec.add_development_dependency "webmock", "~> 1.14.0"
|
31
|
+
end
|
data/lib/crusade/gcm.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
require 'crusade/gcm/access_token_fetcher'
|
4
|
+
|
5
|
+
module Crusade
|
6
|
+
module GCM
|
7
|
+
class AccessToken
|
8
|
+
def initialize(configuration)
|
9
|
+
self.configuration = configuration
|
10
|
+
end
|
11
|
+
|
12
|
+
def get
|
13
|
+
cached = token_in_cache
|
14
|
+
return cached if cached
|
15
|
+
|
16
|
+
request_new_token
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_accessor :configuration
|
22
|
+
|
23
|
+
def cache
|
24
|
+
configuration.token_cache
|
25
|
+
end
|
26
|
+
|
27
|
+
def fetcher
|
28
|
+
AccessTokenFetcher.new(configuration)
|
29
|
+
end
|
30
|
+
|
31
|
+
def request_new_token
|
32
|
+
token = fetcher.request_token
|
33
|
+
cache.write(:access_token, token["access_token"])
|
34
|
+
token["access_token"]
|
35
|
+
end
|
36
|
+
|
37
|
+
def token_in_cache
|
38
|
+
cache.read(:access_token)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Crusade
|
5
|
+
module GCM
|
6
|
+
class AccessTokenFetcher
|
7
|
+
def initialize(configuration)
|
8
|
+
self.configuration = configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def request_token
|
12
|
+
response = connection.post '/o/oauth2/token', params
|
13
|
+
|
14
|
+
JSON.parse response.body
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_accessor :configuration
|
20
|
+
|
21
|
+
def connection
|
22
|
+
Faraday.new(:url => 'https://accounts.google.com') do |faraday|
|
23
|
+
faraday.request :multipart
|
24
|
+
faraday.request :url_encoded
|
25
|
+
faraday.use Faraday::Response::Logger, logger
|
26
|
+
faraday.adapter Faraday.default_adapter
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def logger
|
31
|
+
Crusade::Logger.logger
|
32
|
+
end
|
33
|
+
|
34
|
+
def params
|
35
|
+
{
|
36
|
+
client_id: configuration.client_id,
|
37
|
+
client_secret: configuration.client_secret,
|
38
|
+
refresh_token: configuration.refresh_token,
|
39
|
+
grant_type: 'refresh_token'
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# "application/x-www-form-urlencoded"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Crusade
|
4
|
+
module GCM
|
5
|
+
class InMemoryCache
|
6
|
+
def self.read(key)
|
7
|
+
entry = cache.fetch(key) { {value: nil, valid_until: Time.now} }
|
8
|
+
if entry[:valid_until] > Time.now
|
9
|
+
entry[:value]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.write(key, value, options = {})
|
14
|
+
expires_in = options.fetch(:expires_in) { 3600 }
|
15
|
+
@cache[key] = { value: value, valid_until: (Time.now + expires_in) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.cache
|
19
|
+
@cache ||= {}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
module Crusade
|
3
|
+
module GCM
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :client_id, :client_secret, :refresh_token, :token_cache
|
6
|
+
|
7
|
+
def initialize(args = {})
|
8
|
+
self.client_id = args[:client_id]
|
9
|
+
self.client_secret = args[:client_secret]
|
10
|
+
self.refresh_token = args[:refresh_token]
|
11
|
+
self.token_cache = args.fetch(:token_cache) { InMemoryCache }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'crusade/logger'
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module Crusade
|
6
|
+
module GCM
|
7
|
+
class Connection
|
8
|
+
include Crusade::Logger
|
9
|
+
|
10
|
+
def initialize(configuration)
|
11
|
+
self.configuration = configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def send(payload, access_token)
|
15
|
+
connection.post do |req|
|
16
|
+
req.url '/gcm_for_chrome/v1/messages'
|
17
|
+
req.headers['Content-Type'] = 'application/json'
|
18
|
+
req.headers['Authorization'] = "Bearer #{access_token}"
|
19
|
+
req.body = payload
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_accessor :configuration
|
26
|
+
|
27
|
+
def connection
|
28
|
+
Faraday.new(:url => 'https://www.googleapis.com') do |faraday|
|
29
|
+
faraday.use Faraday::Response::Logger, logger
|
30
|
+
faraday.adapter Faraday.default_adapter
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def logger
|
35
|
+
Crusade::Logger.logger
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
require 'crusade'
|
4
|
+
|
5
|
+
module Crusade
|
6
|
+
module GCM
|
7
|
+
class Notification < SimpleDelegator
|
8
|
+
attr_accessor :channel_id, :subchannel_id
|
9
|
+
|
10
|
+
def initialize(notification = {}, channel_id = nil, subchannel_id = nil)
|
11
|
+
if notification.is_a? Hash
|
12
|
+
channel_id = notification[:channel_id]
|
13
|
+
subchannel_id = notification[:subchannel_id]
|
14
|
+
notification = Crusade::Notification.new notification
|
15
|
+
end
|
16
|
+
super notification
|
17
|
+
self.channel_id = channel_id
|
18
|
+
self.subchannel_id = subchannel_id
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_json
|
22
|
+
payload.to_json
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def payload
|
28
|
+
{
|
29
|
+
channelId: channel_id,
|
30
|
+
subchannelId: subchannel_id,
|
31
|
+
payload: {title: title, body: body}.to_json
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'crusade/logger'
|
2
|
+
|
3
|
+
module Crusade
|
4
|
+
module GCM
|
5
|
+
class PushNotification
|
6
|
+
include Crusade::Logger
|
7
|
+
|
8
|
+
attr_reader :configuration
|
9
|
+
|
10
|
+
def initialize(configuration, connection = nil)
|
11
|
+
self.configuration = configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def perform notification
|
15
|
+
info "sending notification #{notification.inspect}"
|
16
|
+
connection.send notification.to_json, access_token
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_writer :configuration
|
22
|
+
|
23
|
+
def access_token
|
24
|
+
AccessToken.new(configuration).get
|
25
|
+
end
|
26
|
+
|
27
|
+
def connection
|
28
|
+
Connection.new(configuration)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
{
|
2
|
+
"error": {
|
3
|
+
"errors": [
|
4
|
+
{
|
5
|
+
"domain": "gcmForChrome.message",
|
6
|
+
"reason": "forbiddenForNonAppOwner",
|
7
|
+
"message": "A message can only be sent by the owner of the app."
|
8
|
+
}
|
9
|
+
],
|
10
|
+
"code": 403,
|
11
|
+
"message": "A message can only be sent by the owner of the app."
|
12
|
+
}
|
13
|
+
}
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require File.expand_path '../../test_helper.rb', __FILE__
|
2
|
+
|
3
|
+
require 'crusade/gcm/push_notification'
|
4
|
+
|
5
|
+
require 'fixtures'
|
6
|
+
require 'webmock/minitest'
|
7
|
+
|
8
|
+
describe Crusade::GCM::PushNotification do
|
9
|
+
let(:described_class) { Crusade::GCM::PushNotification }
|
10
|
+
|
11
|
+
let(:configuration) { Fixtures.default_configuration }
|
12
|
+
subject { described_class.new configuration }
|
13
|
+
let(:fixture) { Fixtures.access_token 'success' }
|
14
|
+
|
15
|
+
let(:notification) do
|
16
|
+
Crusade::GCM::Notification.new(title: 'the title', body: 'the body',
|
17
|
+
channel_id: '123', subchannel_id: '456')
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:access_token_stub) do
|
21
|
+
stub_request(:post, "https://accounts.google.com/o/oauth2/token").with({
|
22
|
+
headers: {
|
23
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
24
|
+
},
|
25
|
+
params: { client_id: '1111/client',
|
26
|
+
client_secret: '"bbbbbbbbbb"',
|
27
|
+
refresh_token: "aaaaaaaabbbbbbb", grant_type: 'refresh_token' }
|
28
|
+
}).to_return(status: [ 200, 'ok'], body: fixture.to_json)
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:notification_stub) { notification_request_stub }
|
32
|
+
|
33
|
+
def notification_request_stub
|
34
|
+
stub_request(:post, "https://www.googleapis.com/gcm_for_chrome/v1/messages").with({
|
35
|
+
headers: {
|
36
|
+
'Content-Type' => 'application/json',
|
37
|
+
'Authorization' => 'Bearer 1/fFBGRNJru1FQd44AzqT3Zg',
|
38
|
+
},
|
39
|
+
body: { channelId: '123', subchannelId: '456',
|
40
|
+
payload: '{"title":"the title","body":"the body"}'}
|
41
|
+
}).to_return(status: [ 204, 'No Content'])
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'one request' do
|
45
|
+
|
46
|
+
before do
|
47
|
+
configuration.token_cache.cache.delete :access_token
|
48
|
+
end
|
49
|
+
|
50
|
+
before do
|
51
|
+
access_token_stub
|
52
|
+
notification_stub
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'sends the notification' do
|
56
|
+
subject.perform notification
|
57
|
+
|
58
|
+
assert_requested access_token_stub
|
59
|
+
assert_requested notification_stub
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'multiple request' do
|
64
|
+
|
65
|
+
before do
|
66
|
+
configuration.token_cache.cache.delete :access_token
|
67
|
+
end
|
68
|
+
|
69
|
+
before do
|
70
|
+
access_token_stub
|
71
|
+
notification_stub
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'sends the notification' do
|
75
|
+
subject.perform notification
|
76
|
+
WebMock.reset!
|
77
|
+
notification_request_stub
|
78
|
+
|
79
|
+
subject.perform notification
|
80
|
+
|
81
|
+
assert_not_requested access_token_stub
|
82
|
+
assert_requested notification_stub
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class Fixtures
|
4
|
+
|
5
|
+
def self.gcm_response(name)
|
6
|
+
File.read File.join 'test', 'fixtures', "gcm_response", "#{name}.json"
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.default_configuration_attrs
|
10
|
+
{
|
11
|
+
client_id: "1111/client",
|
12
|
+
client_secret: "bbbbbbbbbb",
|
13
|
+
refresh_token: "aaaaaaaabbbbbbb"
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.default_configuration
|
18
|
+
Crusade::GCM::Configuration.new default_configuration_attrs
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.access_token(name)
|
22
|
+
JSON.parse File.read File.join 'test', 'fixtures', "access_token", "#{name}.json"
|
23
|
+
end
|
24
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path '../../test_helper.rb', __FILE__
|
2
|
+
|
3
|
+
require 'crusade/gcm/access_token_fetcher'
|
4
|
+
|
5
|
+
require 'fixtures'
|
6
|
+
require 'webmock/minitest'
|
7
|
+
|
8
|
+
describe Crusade::GCM::AccessTokenFetcher do
|
9
|
+
let(:described_class) { Crusade::GCM::AccessTokenFetcher }
|
10
|
+
|
11
|
+
describe 'request_token' do
|
12
|
+
describe 'successfull' do
|
13
|
+
let(:configuration) { Fixtures.default_configuration }
|
14
|
+
subject { described_class.new configuration }
|
15
|
+
let(:fixture) { Fixtures.access_token 'success' }
|
16
|
+
|
17
|
+
let(:stub) do
|
18
|
+
stub_request(:post, "https://accounts.google.com/o/oauth2/token").with({
|
19
|
+
headers: {
|
20
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
21
|
+
},
|
22
|
+
params: { "a" => 1 , "b" => "2" }
|
23
|
+
}).to_return(status: [ 200, 'ok'], body: fixture.to_json)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'sends a post request to gcm servers' do
|
27
|
+
stub
|
28
|
+
|
29
|
+
subject.request_token
|
30
|
+
|
31
|
+
assert_requested stub #check to create spec style assertion
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns the access token' do
|
35
|
+
stub
|
36
|
+
|
37
|
+
token = subject.request_token
|
38
|
+
|
39
|
+
token.must_equal({
|
40
|
+
"access_token" => "1/fFBGRNJru1FQd44AzqT3Zg",
|
41
|
+
"expires_in" => 3920,
|
42
|
+
"token_type" => "Bearer"
|
43
|
+
})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path '../../test_helper.rb', __FILE__
|
2
|
+
|
3
|
+
require 'crusade/gcm/access_token'
|
4
|
+
require 'crusade/gcm/access_token_fetcher'
|
5
|
+
|
6
|
+
describe Crusade::GCM::AccessToken do
|
7
|
+
class TestCache
|
8
|
+
def initialize
|
9
|
+
@cache = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def read(key)
|
13
|
+
@cache[:access_token]
|
14
|
+
end
|
15
|
+
|
16
|
+
def write(key, value, opts = {})
|
17
|
+
@cache[:access_token] = value
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear
|
21
|
+
@cache = {}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:configuration) { OpenStruct.new(token_cache: TestCache.new ) }
|
26
|
+
let(:described_class) { Crusade::GCM::AccessToken }
|
27
|
+
subject { described_class.new configuration }
|
28
|
+
|
29
|
+
describe 'get' do
|
30
|
+
let(:cached_token) { 'aaaaaa' }
|
31
|
+
let(:new_token) { "1/fFBGRNJru1FQd44AzqT3Zg" }
|
32
|
+
|
33
|
+
describe 'a cached token is available' do
|
34
|
+
before do
|
35
|
+
configuration.token_cache.write(:access_token, cached_token)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'returns the cached value if available' do
|
39
|
+
subject.get.must_equal cached_token
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'cached value unavailable' do
|
44
|
+
let(:fetcher) { OpenStruct.new(request_token: fetched)}
|
45
|
+
let(:fetched) { Fixtures.access_token 'success' }
|
46
|
+
|
47
|
+
before do
|
48
|
+
Crusade::GCM::AccessTokenFetcher.stub(:new, fetcher) do
|
49
|
+
@token = subject.get
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'fetches a new token' do
|
54
|
+
@token.must_equal new_token
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'saves the value in the cache' do
|
58
|
+
configuration.token_cache.read(:access_token).must_equal new_token
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.expand_path '../../../test_helper.rb', __FILE__
|
2
|
+
|
3
|
+
require 'crusade/gcm/cache/in_memory_cache'
|
4
|
+
|
5
|
+
describe Crusade::GCM::InMemoryCache do
|
6
|
+
subject { Crusade::GCM::InMemoryCache }
|
7
|
+
|
8
|
+
let(:key) { 'a_key' }
|
9
|
+
let(:value) { 'a value' }
|
10
|
+
|
11
|
+
after do
|
12
|
+
subject.cache.delete(key)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'read' do
|
16
|
+
before do
|
17
|
+
subject.cache[key] = { value: value, valid_until: (Time.now + 10) }
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns the value if the cache contains an entry for the key' do
|
21
|
+
subject.read(key).must_equal value
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns nil if the cache does not contain an entry for the key' do
|
25
|
+
subject.read('a other key').must_be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns nil if the cache contains an entry for the key but is outdated' do
|
29
|
+
subject.cache[key] = { value: value, valid_until: (Time.now - 10) }
|
30
|
+
|
31
|
+
subject.read(key).must_be_nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'write' do
|
36
|
+
let(:valid_until) { Time.now + 10 }
|
37
|
+
|
38
|
+
before do
|
39
|
+
subject.write(key, value)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'sets an entry in the cache' do
|
43
|
+
subject.cache[key][:value].must_equal value
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'sets default valid_until' do
|
47
|
+
Integer(subject.cache[key][:valid_until]).must_be_close_to (Time.now.to_i + 3600), 1
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'sets a valid_until if a expires_in provided' do
|
51
|
+
subject.write(key, value, expires_in: 30)
|
52
|
+
|
53
|
+
Integer(subject.cache[key][:valid_until]).must_be_close_to (Time.now.to_i + 30), 1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.expand_path '../../test_helper.rb', __FILE__
|
2
|
+
|
3
|
+
require 'crusade/gcm/configuration'
|
4
|
+
|
5
|
+
describe Crusade::GCM::Configuration do
|
6
|
+
let(:described_class) { Crusade::GCM::Configuration }
|
7
|
+
|
8
|
+
describe 'initialize' do
|
9
|
+
let(:attributes) do
|
10
|
+
{
|
11
|
+
client_id: 'client id',
|
12
|
+
client_secret: 'client secret',
|
13
|
+
refresh_token: 'refresh token',
|
14
|
+
token_cache: 'token_cache'
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
subject { described_class.new attributes }
|
19
|
+
|
20
|
+
it { subject.client_id.must_equal 'client id' }
|
21
|
+
it { subject.client_secret.must_equal 'client secret' }
|
22
|
+
it { subject.refresh_token.must_equal 'refresh token' }
|
23
|
+
it { subject.token_cache.must_equal 'token_cache' }
|
24
|
+
|
25
|
+
describe 'no attributes' do
|
26
|
+
subject { described_class.new }
|
27
|
+
|
28
|
+
it { subject.client_id.must_be_nil }
|
29
|
+
it { subject.client_secret.must_be_nil }
|
30
|
+
it { subject.refresh_token.must_be_nil }
|
31
|
+
it { subject.token_cache.must_equal Crusade::GCM::InMemoryCache }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path '../../test_helper.rb', __FILE__
|
2
|
+
|
3
|
+
require 'crusade/gcm/connection'
|
4
|
+
require 'fixtures'
|
5
|
+
|
6
|
+
require 'webmock/minitest'
|
7
|
+
|
8
|
+
describe Crusade::GCM::Connection do
|
9
|
+
let(:described_class) { Crusade::GCM::Connection }
|
10
|
+
|
11
|
+
describe 'send' do
|
12
|
+
describe 'successfull sending' do
|
13
|
+
let(:configuration) { Fixtures.default_configuration }
|
14
|
+
subject { described_class.new configuration }
|
15
|
+
let(:payload) { '{"a": 1, "b" : "2"}' }
|
16
|
+
|
17
|
+
it 'sends a post request to gcm servers' do
|
18
|
+
stub = stub_request(:post, "https://www.googleapis.com/gcm_for_chrome/v1/messages").with({
|
19
|
+
headers: {
|
20
|
+
'Content-Type' => 'application/json',
|
21
|
+
'Authorization' => 'Bearer ACCESS_TOKEN',
|
22
|
+
},
|
23
|
+
body: { "a" => 1 , "b" => "2" }
|
24
|
+
}).to_return(status: [ 204, 'No Content'])
|
25
|
+
|
26
|
+
subject.send payload, 'ACCESS_TOKEN'
|
27
|
+
|
28
|
+
assert_requested stub #check to create spec style assertion
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.expand_path '../../test_helper.rb', __FILE__
|
2
|
+
|
3
|
+
require 'crusade/notification'
|
4
|
+
require 'crusade/gcm/notification'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe Crusade::GCM::Notification do
|
8
|
+
let(:described_class) { Crusade::GCM::Notification }
|
9
|
+
let(:notification) { Crusade::Notification.new attrs }
|
10
|
+
let(:channel_id) { '1234a' }
|
11
|
+
let(:subchannel_id) { 'abcde' }
|
12
|
+
|
13
|
+
let(:attrs) do
|
14
|
+
{
|
15
|
+
body: 'the body',
|
16
|
+
title: 'the title',
|
17
|
+
channel_id: channel_id,
|
18
|
+
subchannel_id: subchannel_id
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'initialize' do
|
23
|
+
describe 'with a generic notification' do
|
24
|
+
subject { described_class.new notification, channel_id, subchannel_id }
|
25
|
+
|
26
|
+
it 'delegates the generic properties' do
|
27
|
+
subject.body.must_equal 'the body'
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'has a channel id' do
|
31
|
+
subject.channel_id.must_equal channel_id
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'has a subchannel id' do
|
35
|
+
subject.subchannel_id.must_equal subchannel_id
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'with a hash of attributes' do
|
40
|
+
subject { described_class.new attrs }
|
41
|
+
|
42
|
+
it 'internally builds a generic notification' do
|
43
|
+
subject.body.must_equal 'the body'
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'has a channel id' do
|
47
|
+
subject.channel_id.must_equal channel_id
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'has a subchannel id' do
|
51
|
+
subject.subchannel_id.must_equal subchannel_id
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'payload and co' do
|
57
|
+
subject { described_class.new attrs }
|
58
|
+
|
59
|
+
let(:parsed_payload) { JSON.parse(subject.to_json) }
|
60
|
+
let(:expected_payload) do
|
61
|
+
{
|
62
|
+
'channelId' => channel_id,
|
63
|
+
'subchannelId' => subchannel_id,
|
64
|
+
'payload' => '{"title":"the title","body":"the body"}'
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
it { parsed_payload.must_equal expected_payload }
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crusade-gcm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Rouchy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: crusade
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.8
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.8.8
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json_expressions
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.2
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.2
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 5.0.7
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 5.0.7
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest-great_expectations
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.0.5
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.0.5
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: coveralls
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: webmock
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ~>
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.14.0
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ~>
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 1.14.0
|
139
|
+
description: Google Cloud Messaging plugin for crusade
|
140
|
+
email:
|
141
|
+
- drouchy@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- .gitignore
|
147
|
+
- .travis.yml
|
148
|
+
- Gemfile
|
149
|
+
- LICENSE.txt
|
150
|
+
- README.md
|
151
|
+
- Rakefile
|
152
|
+
- crusade-gcm.gemspec
|
153
|
+
- lib/crusade/gcm.rb
|
154
|
+
- lib/crusade/gcm/access_token.rb
|
155
|
+
- lib/crusade/gcm/access_token_fetcher.rb
|
156
|
+
- lib/crusade/gcm/cache/in_memory_cache.rb
|
157
|
+
- lib/crusade/gcm/configuration.rb
|
158
|
+
- lib/crusade/gcm/connection.rb
|
159
|
+
- lib/crusade/gcm/notification.rb
|
160
|
+
- lib/crusade/gcm/push_notification.rb
|
161
|
+
- lib/crusade/gcm/version.rb
|
162
|
+
- test/fixtures/access_token/success.json
|
163
|
+
- test/fixtures/gcm_responses/invalid_channel.json
|
164
|
+
- test/fixtures/gcm_responses/invalid_credentials.json
|
165
|
+
- test/fixtures/gcm_responses/no_sub_channel.json
|
166
|
+
- test/integration/push_notification_test.rb
|
167
|
+
- test/support/fixtures.rb
|
168
|
+
- test/test_helper.rb
|
169
|
+
- test/unit/access_token_fetcher_test.rb
|
170
|
+
- test/unit/access_token_test.rb
|
171
|
+
- test/unit/cache/in_memory_cache_test.rb
|
172
|
+
- test/unit/configuration_test.rb
|
173
|
+
- test/unit/connection_test.rb
|
174
|
+
- test/unit/notification_test.rb
|
175
|
+
homepage: ''
|
176
|
+
licenses:
|
177
|
+
- MIT
|
178
|
+
metadata: {}
|
179
|
+
post_install_message:
|
180
|
+
rdoc_options: []
|
181
|
+
require_paths:
|
182
|
+
- lib
|
183
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - '>='
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
|
+
requirements:
|
190
|
+
- - '>='
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
requirements: []
|
194
|
+
rubyforge_project:
|
195
|
+
rubygems_version: 2.0.3
|
196
|
+
signing_key:
|
197
|
+
specification_version: 4
|
198
|
+
summary: send push notification to chrome app via GCM
|
199
|
+
test_files:
|
200
|
+
- test/fixtures/access_token/success.json
|
201
|
+
- test/fixtures/gcm_responses/invalid_channel.json
|
202
|
+
- test/fixtures/gcm_responses/invalid_credentials.json
|
203
|
+
- test/fixtures/gcm_responses/no_sub_channel.json
|
204
|
+
- test/integration/push_notification_test.rb
|
205
|
+
- test/support/fixtures.rb
|
206
|
+
- test/test_helper.rb
|
207
|
+
- test/unit/access_token_fetcher_test.rb
|
208
|
+
- test/unit/access_token_test.rb
|
209
|
+
- test/unit/cache/in_memory_cache_test.rb
|
210
|
+
- test/unit/configuration_test.rb
|
211
|
+
- test/unit/connection_test.rb
|
212
|
+
- test/unit/notification_test.rb
|