nuge 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 621e407ad8f5cd31a3f7a321b40c770286904065
4
+ data.tar.gz: fc6e9f6a73fb5ec6d6579e38e1c689c12c0b9d5a
5
+ SHA512:
6
+ metadata.gz: 002b0082d6f5b08254331d0e4b96afc96f550c4dd740b084c2f98464a5306f920062000d73eb99fe0d55e8a576e4f1864cf4ae0574ac0d02e41198af482a9f56
7
+ data.tar.gz: 86514737fee19dfb4ffd5b34d20b00fd6d0ee62ed49f0bac6e41c48ebefb4dfaf862cc36a073bc7338f8db03dbc2fbd1d9b6cc30c410d94b4eb1628a7a894cad
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ Gemfile.lock
6
+ bin/
7
+ pkg/
8
+ TODO
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format progress
3
+ -I spec/spec_helper
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script: bundle exec rspec spec
@@ -0,0 +1,3 @@
1
+ # v0.1.0
2
+
3
+ * Initial release, supporting Grocer and Urbanairship clients.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nuge.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dscout, Inc
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.
@@ -0,0 +1,70 @@
1
+ [![Build Status](https://travis-ci.org/dscout/nuge.png?branch=master)](https://travis-ci.org/dscout/nuge)
2
+ [![Code Climate](https://codeclimate.com/github/dscout/nuge.png)](https://codeclimate.com/github/dscout/nuge)
3
+
4
+ # Nuge
5
+
6
+ The biz of pushing to multiple notification clients from a single source. Nuge
7
+ presents a uniform interface for pushing out messages from a single source.
8
+
9
+ Supported services (some pending):
10
+
11
+ * Grocer
12
+ * Urbanairship
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'nuge'
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ By default Nuge is a no-op. You'll need to configure any clients that you wish
25
+ to push with.
26
+
27
+ ### Grocer Client
28
+
29
+ ```ruby
30
+ require 'nuge/clients/grocer'
31
+
32
+ Nuge::Pusher.clients << Nuge::Clients::Grocer(certificate: '/path/to/cert.pem')
33
+ ```
34
+
35
+ ### Urbanairship Client
36
+
37
+ ```ruby
38
+ require 'nuge/clients/urbanairship'
39
+
40
+ # Using the alternate config block style
41
+ Nuge.configure do |config|
42
+ config.clients << Nuge::Clients::Urbanairship.new(
43
+ application_key: ENV['URBANAIRSHIP_APPLICATION_KEY'],
44
+ application_secret: ENV['URBANAIRSHIP_APPLICATION_SECRET'],
45
+ master_secret: ENV['URBANAIRSHIP_MASTER_SECRET']
46
+ )
47
+ end
48
+ ```
49
+
50
+ You can then register devices:
51
+
52
+ ```ruby
53
+ pusher = Nuge.pusher
54
+ pusher.register(token)
55
+ pusher.register(token, provider: 'android')
56
+ ```
57
+
58
+ Notification pushing can be configured with custom expanders and formatters:
59
+
60
+ ```ruby
61
+ Nuge.pusher.push(['1234abcd'], alert: 'hello!')
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,13 @@
1
+ require 'nuge/client'
2
+ require 'nuge/pusher'
3
+ require 'nuge/version'
4
+
5
+ module Nuge
6
+ def self.configure
7
+ yield Nuge::Pusher
8
+ end
9
+
10
+ def self.pusher(*args)
11
+ Nuge::Pusher.new(args)
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Nuge
2
+ class Client
3
+ def register(id, options = {})
4
+ end
5
+
6
+ def unregister(id, options = {})
7
+ end
8
+
9
+ def push(ids, message)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,36 @@
1
+ require 'grocer'
2
+ require 'nuge/client'
3
+
4
+ module Nuge
5
+ module Clients
6
+ class Grocer < Nuge::Client
7
+ attr_reader :options
8
+
9
+ def initialize(options = {})
10
+ @options = options
11
+ end
12
+
13
+ def pusher
14
+ @pusher ||= ::Grocer.pusher(options)
15
+ end
16
+
17
+ def push(ids, message)
18
+ notifications(ids, message).each do |notification|
19
+ pusher.push(notification)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def notifications(ids, message)
26
+ ids.map do |token|
27
+ ::Grocer::Notification.new(
28
+ device_token: token,
29
+ alert: message.delete(:alert),
30
+ custom: message
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,45 @@
1
+ require 'urbanairship'
2
+
3
+ module Nuge
4
+ module Clients
5
+ class Urbanairship
6
+ attr_reader :options
7
+ attr_writer :expander
8
+
9
+ def initialize(options = {})
10
+ @expander = options[:expander]
11
+ @options = options
12
+ end
13
+
14
+ def pusher
15
+ @pusher ||= ::Urbanairship.tap do |serv|
16
+ serv.application_key = options[:application_key]
17
+ serv.application_secret = options[:application_secret]
18
+ serv.master_secret = options[:master_secret]
19
+ end
20
+ end
21
+
22
+ def register(id, options = {})
23
+ pusher.register_device(id, options)
24
+ end
25
+
26
+ def unregister(id, options = {})
27
+ pusher.unregister_device(id, options)
28
+ end
29
+
30
+ def push(ids, message)
31
+ formatter = message.delete(:formatter) || Nuge::Formatters::Aps
32
+
33
+ pusher.push(formatter.call(ids, message))
34
+ end
35
+ end
36
+ end
37
+
38
+ module Formatters
39
+ module Aps
40
+ def self.call(ids, message)
41
+ { device_tokens: ids, aps: message }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,40 @@
1
+ require 'set'
2
+
3
+ module Nuge
4
+ class Pusher
5
+ def self.clients=(clients)
6
+ @clients = Set.new(clients)
7
+ end
8
+
9
+ def self.clients
10
+ @clients ||= Set.new
11
+ end
12
+
13
+ attr_reader :clients
14
+ attr_writer :expander
15
+
16
+ def initialize(clients = nil)
17
+ @clients = Set.new(clients || self.class.clients)
18
+ end
19
+
20
+ def push(ids, message)
21
+ clients.each { |client| client.push(expanded(ids), message) }
22
+ end
23
+
24
+ def register(id, options = {})
25
+ clients.each { |client| client.register(expanded(id).first, options) }
26
+ end
27
+
28
+ def unregister(id, options = {})
29
+ clients.each { |client| client.unregister(expanded(id).first, options) }
30
+ end
31
+
32
+ def expander
33
+ @expander ||= -> (obj) { obj }
34
+ end
35
+
36
+ def expanded(*objects)
37
+ objects.flatten.map { |obj| expander.call(obj) }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Nuge
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nuge/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'nuge'
8
+ spec.version = Nuge::VERSION
9
+ spec.authors = ['Parker Selbert']
10
+ spec.email = ["parker@sorentwo.com"]
11
+ spec.homepage = 'https://github.com/dscout/nuge'
12
+ spec.license = "MIT"
13
+ spec.description = 'Uniform interface for pushing notifications to multiple clients'
14
+ spec.summary = 'Uniform interface for pushing notifications to multiple clients'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(%r{^(spec)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.0'
21
+ spec.add_development_dependency 'rake', '~> 10.1'
22
+ spec.add_development_dependency 'rspec', '~> 2.14'
23
+ spec.add_development_dependency 'grocer', '~> 0.4'
24
+ spec.add_development_dependency 'urbanairship', '~> 2.3'
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'nuge/client'
2
+
3
+ describe Nuge::Client do
4
+ it 'defines empty methods for the interface' do
5
+ client = Nuge::Client.new
6
+
7
+ expect(client).to respond_to(:register)
8
+ expect(client).to respond_to(:unregister)
9
+ expect(client).to respond_to(:push)
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ require 'nuge/clients/grocer'
2
+
3
+ describe Nuge::Clients::Grocer do
4
+ describe '#initialize' do
5
+ it 'passes initialization options directly to the service class' do
6
+ client = Nuge::Clients::Grocer.new(certificate: '/path/to/cert.pem')
7
+
8
+ expect(::Grocer).to receive(:pusher).with(certificate: '/path/to/cert.pem')
9
+
10
+ client.pusher
11
+ end
12
+ end
13
+
14
+ describe '#push' do
15
+ it 'forwards formatted payloads through the service' do
16
+ client = Nuge::Clients::Grocer.new(certificate: '/path/to/cert.pem')
17
+
18
+ expect(client.pusher).to receive(:push).twice
19
+
20
+ client.push(['1234', '5678'], alert: 'Hello!')
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,69 @@
1
+ require 'nuge/clients/urbanairship'
2
+
3
+ describe Nuge::Clients::Urbanairship do
4
+ describe '#initialize' do
5
+ it 'passes initialization options directly to the pusher class' do
6
+ client = Nuge::Clients::Urbanairship.new(
7
+ application_key: 'applicationkey',
8
+ application_secret: 'applicationsecret',
9
+ master_secret: 'mastersecret'
10
+ )
11
+
12
+ expect(Urbanairship).to receive(:application_key=).with('applicationkey')
13
+ expect(Urbanairship).to receive(:application_secret=).with('applicationsecret')
14
+ expect(Urbanairship).to receive(:master_secret=).with('mastersecret')
15
+
16
+ client.pusher
17
+ end
18
+ end
19
+
20
+ describe '#register' do
21
+ it 'forwards the registration call through the pusher' do
22
+ client = Nuge::Clients::Urbanairship.new
23
+
24
+ expect(client.pusher).to receive(:register_device).with('1234', {})
25
+
26
+ client.register('1234')
27
+ end
28
+ end
29
+
30
+ describe '#unregister' do
31
+ it 'forwards the unregistration call through the pusher' do
32
+ client = Nuge::Clients::Urbanairship.new
33
+
34
+ expect(client.pusher).to receive(:unregister_device).with('1234', {})
35
+
36
+ client.unregister('1234')
37
+ end
38
+ end
39
+
40
+ describe '#push' do
41
+ it 'forwards client configured payloads through the pusher, assuming apns' do
42
+ client = Nuge::Clients::Urbanairship.new
43
+ message = { 'alert' => 'hello', 'some_id' => '99' }
44
+
45
+ expect(client.pusher).to receive(:push).with(
46
+ device_tokens: ['1234'],
47
+ aps: message
48
+ )
49
+
50
+ client.push(['1234'], message)
51
+ end
52
+
53
+ it 'allows the payload to be configured with a formatter' do
54
+ client = Nuge::Clients::Urbanairship.new
55
+ message = { 'alert' => 'hello', 'some_id' => '99' }
56
+
57
+ expect(client.pusher).to receive(:push).with(
58
+ ids: ['1234'],
59
+ message: message
60
+ )
61
+
62
+ formatter = -> (ids, messsage) do
63
+ { ids: ids, message: message }
64
+ end
65
+
66
+ client.push(['1234'], message.merge(formatter: formatter))
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,80 @@
1
+ require 'nuge/pusher'
2
+ require 'nuge/client'
3
+
4
+ describe Nuge::Pusher do
5
+ before do
6
+ @clients = Nuge::Pusher.clients
7
+ end
8
+
9
+ after do
10
+ Nuge::Pusher.clients = @clients
11
+ end
12
+
13
+ describe '.clients' do
14
+ it 'is an empty set if it has not been defined' do
15
+ expect(Nuge::Pusher.clients).to be_empty
16
+ end
17
+ end
18
+
19
+ describe '.clients=' do
20
+ it 'persists client instances at the class level' do
21
+ client = Nuge::Client.new
22
+
23
+ Nuge::Pusher.clients = [client]
24
+
25
+ expect(Nuge::Pusher.clients).to include(client)
26
+ expect(Nuge::Pusher.new.clients).to include(client)
27
+ end
28
+ end
29
+
30
+ describe '#clients' do
31
+ it 'defaults to an empty set of clients' do
32
+ expect(Nuge::Pusher.new).to respond_to(:clients)
33
+ expect(Nuge::Pusher.new.clients).to be_empty
34
+ end
35
+
36
+ it 'accepts changes to the client set' do
37
+ nuge = Nuge::Pusher.new
38
+ client = Nuge::Client.new
39
+
40
+ nuge.clients << client
41
+ expect(nuge.clients).to include(client)
42
+ end
43
+ end
44
+
45
+ describe 'forwarding' do
46
+ it 'forwards client methods to all instantiated clients' do
47
+ nuge = Nuge::Pusher.new([double(:client), double(:client)])
48
+
49
+ nuge.clients.each do |client|
50
+ expect(client).to receive(:push).with(['1234'], message: 'hello')
51
+ expect(client).to receive(:register).with('1234', option: true)
52
+ expect(client).to receive(:unregister).with('1234', option: true)
53
+ end
54
+
55
+ nuge.push(['1234'], message: 'hello')
56
+ nuge.register('1234', option: true)
57
+ nuge.unregister('1234', option: true)
58
+ end
59
+ end
60
+
61
+ describe 'expanders' do
62
+ it 'applies an identity function against id objects by default' do
63
+ nuge = Nuge::Pusher.new
64
+
65
+ expect(nuge.expanded('abc')).to eq(['abc'])
66
+ end
67
+
68
+ it 'applies expansion against the all supplied id objects' do
69
+ nuge = Nuge::Pusher.new
70
+ nuge.expander = -> (obj) { obj[:token] }
71
+
72
+ reg_a = { token: 'abc' }
73
+ reg_b = { token: 'def' }
74
+
75
+ expect(nuge.expanded(reg_a)).to eq(%w[abc])
76
+ expect(nuge.expanded(reg_a, reg_b)).to eq(%w[abc def])
77
+ expect(nuge.expanded([reg_a, reg_b])).to eq(%w[abc def])
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,21 @@
1
+ require 'nuge'
2
+
3
+ describe Nuge do
4
+ describe '.configure 'do
5
+ it 'applies the provided block to the pusher class' do
6
+ client = Object.new
7
+
8
+ Nuge.configure do |config|
9
+ config.clients = [client]
10
+ end
11
+
12
+ expect(Nuge::Pusher.clients).to include(client)
13
+ end
14
+ end
15
+
16
+ describe '.pusher' do
17
+ it 'provides a shortcut to creating a pusher instance' do
18
+ expect(Nuge.pusher).to be_instance_of(Nuge::Pusher)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ RSpec.configure do |config|
2
+ config.treat_symbols_as_metadata_keys_with_true_values = true
3
+ config.run_all_when_everything_filtered = true
4
+ config.filter_run :focus
5
+ config.order = 'random'
6
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nuge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Parker Selbert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: grocer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: urbanairship
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '2.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '2.3'
83
+ description: Uniform interface for pushing notifications to multiple clients
84
+ email:
85
+ - parker@sorentwo.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .travis.yml
93
+ - CHANGELOG.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lib/nuge.rb
99
+ - lib/nuge/client.rb
100
+ - lib/nuge/clients/grocer.rb
101
+ - lib/nuge/clients/urbanairship.rb
102
+ - lib/nuge/pusher.rb
103
+ - lib/nuge/version.rb
104
+ - nuge.gemspec
105
+ - spec/nuge/client_spec.rb
106
+ - spec/nuge/clients/grocer_spec.rb
107
+ - spec/nuge/clients/urbanairship_spec.rb
108
+ - spec/nuge/pusher_spec.rb
109
+ - spec/nuge_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage: https://github.com/dscout/nuge
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.0.0
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Uniform interface for pushing notifications to multiple clients
135
+ test_files:
136
+ - spec/nuge/client_spec.rb
137
+ - spec/nuge/clients/grocer_spec.rb
138
+ - spec/nuge/clients/urbanairship_spec.rb
139
+ - spec/nuge/pusher_spec.rb
140
+ - spec/nuge_spec.rb
141
+ - spec/spec_helper.rb