gcm_middleware 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +11 -0
- data/gcm_middleware.gemspec +27 -0
- data/lib/gcm_middleware/authentication.rb +21 -0
- data/lib/gcm_middleware/canonical_id.rb +33 -0
- data/lib/gcm_middleware/version.rb +3 -0
- data/lib/gcm_middleware.rb +17 -0
- data/spec/gcm_middleware_spec.rb +36 -0
- data/spec/middleware/authentication_spec.rb +18 -0
- data/spec/middleware/canonical_id_spec.rb +75 -0
- data/spec/spec_helper.rb +11 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fc7668b6b374a402eb48242976980c89934f56f5
|
4
|
+
data.tar.gz: 23873b9160246094485e34826fa579af29e3b2ed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a4eff5600ad2e560fe4ebbe0eeb7f6c1affa7152abbe64a97e606a7cc009bbd3c85976cf499276e5304e887299b0469809d9ca083ff56de2e4f971d28689fc0
|
7
|
+
data.tar.gz: a0df42a0aafd8228d73b480c1ea238e3d3d12b6cfe02fc7bb6f67c4bd4518b2e80ecd8cb718adf4faa69174fbd4069fea2acf20b0753c2968a3ac38fbf0d8d6d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Trevor John
|
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,45 @@
|
|
1
|
+
# GcmMiddleware
|
2
|
+
|
3
|
+
Faraday ( https://github.com/lostisland/faraday ) middleware for Google Cloud Messaging.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'gcm_middleware'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gcm_middleware
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
There are two separate Middleware files, the first is for authorization and adds the request header with your api key.
|
24
|
+
The second is for storing your original device registration ids. Because GCM does not return your original id, and only returns the new canonical id. This piece will inject the original id into the response body.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
Faraday.new('https://android.googleapis.com/') do |f|
|
28
|
+
f.request :gcm_authentication, key: '<your api key'
|
29
|
+
|
30
|
+
f.use :gcm_canonical_id
|
31
|
+
f.use :json
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## TODO
|
36
|
+
|
37
|
+
* GCMMiddleware::CanonicalId requires the body to have been parsed into a hash. It should handle both cases where it is not already parsed, thus removing the dependency on faraday_middleware.
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it ( https://github.com/[my-github-username]/gcm_middleware/fork )
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gcm_middleware/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gcm_middleware"
|
8
|
+
spec.version = GCMMiddleware::VERSION
|
9
|
+
spec.authors = ["Trevor John"]
|
10
|
+
spec.email = ["trevor@john.tj"]
|
11
|
+
spec.summary = %q{A collection of faraday middleware for Google Cloud Messaging}
|
12
|
+
spec.description = %q{Adds authentication key and stores original device registration ids on requests}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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 'faraday'
|
22
|
+
spec.add_dependency 'faraday_middleware'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module GCMMiddleware
|
2
|
+
class Authentication
|
3
|
+
def initialize(app, options = {})
|
4
|
+
@app, @options = app, options
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
env.request_headers['Authorization'] = auth_key
|
9
|
+
|
10
|
+
app.call(env).on_complete { |env| }
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
attr_reader :app, :options
|
16
|
+
|
17
|
+
def auth_key
|
18
|
+
@auth_key ||= "key=#{options[:key]}".freeze
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module GCMMiddleware
|
2
|
+
class CanonicalId
|
3
|
+
def initialize(app, options = {})
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
save_ids(env.body)
|
9
|
+
|
10
|
+
app.call(env).on_complete do |env|
|
11
|
+
inject_original_ids(env.body) if has_registration_ids && env.body
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :registration_ids, :app
|
18
|
+
|
19
|
+
def inject_original_ids(body)
|
20
|
+
body['results'].each_with_index do |result, i|
|
21
|
+
result['original_id'] = registration_ids[i]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def save_ids(body)
|
26
|
+
@registration_ids = body.fetch('registration_ids', []) if body
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_registration_ids
|
30
|
+
registration_ids && registration_ids.any?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'gcm_middleware/version'
|
3
|
+
|
4
|
+
module GCMMiddleware
|
5
|
+
autoload :Authentication, 'gcm_middleware/authentication'
|
6
|
+
autoload :CanonicalId, 'gcm_middleware/canonical_id'
|
7
|
+
|
8
|
+
if Faraday::Middleware.respond_to? :register_middleware
|
9
|
+
Faraday::Request.register_middleware \
|
10
|
+
:gcm_authentication => lambda { Authentication }
|
11
|
+
|
12
|
+
Faraday::Middleware.register_middleware \
|
13
|
+
:gcm_canonical_id => lambda { CanonicalId }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'faraday_middleware/backwards_compatibility'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
describe GCMMiddleware do
|
2
|
+
context 'authentication' do
|
3
|
+
let(:faraday) do
|
4
|
+
Faraday.new('http://www.example.com') do |f|
|
5
|
+
f.request :gcm_authentication, key: 'my-api-key'
|
6
|
+
|
7
|
+
f.adapter :test do |stub|
|
8
|
+
stub.get('/test') { |env| [ 200, {}, {} ] }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'registers :gcm_authentication' do
|
14
|
+
response = faraday.get('test')
|
15
|
+
expect(response.env.request_headers['Authorization']).to eq 'key=my-api-key'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'canonical id' do
|
20
|
+
let(:faraday) do
|
21
|
+
Faraday.new('http://www.example.com') do |f|
|
22
|
+
f.use :gcm_canonical_id
|
23
|
+
|
24
|
+
f.adapter :test do |stub|
|
25
|
+
stub.post('/test') { |env| [ 200, {}, {'results' => [{}]} ] }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'registers :gcm_canonical_id' do
|
31
|
+
response = faraday.post('test', { 'registration_ids' => ['1'] })
|
32
|
+
|
33
|
+
expect(response.body['results'][0]['original_id']).to eq '1'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
describe GCMMiddleware::Authentication do
|
2
|
+
let(:faraday) do
|
3
|
+
Faraday.new('http://www.example.com') do |builder|
|
4
|
+
builder.use GCMMiddleware::Authentication, key: 'auth-key'
|
5
|
+
|
6
|
+
builder.adapter :test do |stub|
|
7
|
+
stub.get('/test') { |env| [ 200, {}, {} ] }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'injects the key into the header' do
|
13
|
+
response = faraday.get '/test'
|
14
|
+
|
15
|
+
expect(response.env.request_headers['Authorization']).to eq('key=auth-key')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,75 @@
|
|
1
|
+
describe GCMMiddleware::CanonicalId do
|
2
|
+
let(:faraday) do
|
3
|
+
Faraday.new('http://www.example.com') do |builder|
|
4
|
+
builder.use GCMMiddleware::CanonicalId
|
5
|
+
|
6
|
+
builder.adapter :test do |stub|
|
7
|
+
stub.post('/test') { |env| [ 200, {}, {'results' => raw_results} ] }
|
8
|
+
stub.post('/error') { |env| [ 200, {}, nil ] }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
let(:raw_results) { [] }
|
13
|
+
|
14
|
+
context 'with no body' do
|
15
|
+
it 'does not modify request' do
|
16
|
+
response = faraday.post '/test', nil
|
17
|
+
|
18
|
+
expect(response.body).to eq({'results' => []})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when no registration ids' do
|
23
|
+
let(:body) { {'foo' => 'bar'} }
|
24
|
+
|
25
|
+
it 'does nothing with the request' do
|
26
|
+
response = faraday.post '/test', body
|
27
|
+
|
28
|
+
expect(response.body).to eq({'results' => []})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with registration ids' do
|
33
|
+
let(:raw_results) do
|
34
|
+
[
|
35
|
+
{
|
36
|
+
'message_id' => 'message-1',
|
37
|
+
'registration_id' => 'registration-1',
|
38
|
+
'error' => nil
|
39
|
+
},
|
40
|
+
{
|
41
|
+
'message_id' => 'message-2',
|
42
|
+
'registration_id' => 'registration-2',
|
43
|
+
'error' => nil
|
44
|
+
}
|
45
|
+
]
|
46
|
+
end
|
47
|
+
let(:body) { {'registration_ids' => ['first-id', 'second-id']} }
|
48
|
+
|
49
|
+
it 'stores the original ids in the response' do
|
50
|
+
response = faraday.post '/test', body
|
51
|
+
|
52
|
+
results = response.body['results']
|
53
|
+
expect(results[0]).to eq({
|
54
|
+
'message_id' => 'message-1',
|
55
|
+
'registration_id' => 'registration-1',
|
56
|
+
'error' => nil,
|
57
|
+
'original_id' => 'first-id'
|
58
|
+
})
|
59
|
+
expect(results[1]).to eq({
|
60
|
+
'message_id' => 'message-2',
|
61
|
+
'registration_id' => 'registration-2',
|
62
|
+
'error' => nil,
|
63
|
+
'original_id' => 'second-id'
|
64
|
+
})
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'with no response body' do
|
68
|
+
it 'does not throw exception' do
|
69
|
+
response = faraday.post '/error', body
|
70
|
+
|
71
|
+
expect(response.body).to be_nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'gcm_middleware'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
config.expect_with :rspec do |expectations|
|
5
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
6
|
+
end
|
7
|
+
|
8
|
+
config.mock_with :rspec do |mocks|
|
9
|
+
mocks.verify_partial_doubles = true
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gcm_middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Trevor John
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.1'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.1'
|
83
|
+
description: Adds authentication key and stores original device registration ids on
|
84
|
+
requests
|
85
|
+
email:
|
86
|
+
- trevor@john.tj
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- gcm_middleware.gemspec
|
98
|
+
- lib/gcm_middleware.rb
|
99
|
+
- lib/gcm_middleware/authentication.rb
|
100
|
+
- lib/gcm_middleware/canonical_id.rb
|
101
|
+
- lib/gcm_middleware/version.rb
|
102
|
+
- spec/gcm_middleware_spec.rb
|
103
|
+
- spec/middleware/authentication_spec.rb
|
104
|
+
- spec/middleware/canonical_id_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
homepage: ''
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.2.2
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: A collection of faraday middleware for Google Cloud Messaging
|
130
|
+
test_files:
|
131
|
+
- spec/gcm_middleware_spec.rb
|
132
|
+
- spec/middleware/authentication_spec.rb
|
133
|
+
- spec/middleware/canonical_id_spec.rb
|
134
|
+
- spec/spec_helper.rb
|