pushable-engine 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f783392e8da04c06a3cf2010367d2195e5e4254c
4
+ data.tar.gz: d57ae0a99b17ee6d8a13a2475b93dd1af644e836
5
+ SHA512:
6
+ metadata.gz: 6ae97afddbb464f47b9dfe6a70512c6615e526cb50773a834d2d2289b89dafab8b667c5330df8a43fc4934c641fce55ef3198bf968e3bb0df97df3ad0feb911f
7
+ data.tar.gz: c5c249919a83db077c8b309155655ad4485f961fbc49a920c672228185382f367be3fb9a17a976184b903b9484a21ba37b32d689a5ba6ac59dc2a4dff16dbcbc
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Logan Serman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Pushable
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Pushable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,11 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
3
+
4
+ $(document).on('ready page:load', function() {
5
+ $('#console_push_klass').on('change', function() {
6
+ var klass = $(this).val();
7
+ $('.form-group[data-klass] input:not([type="hidden"])').val('');
8
+ $('.form-group[data-klass]').addClass('hide');
9
+ $('.form-group[data-klass="' + klass + '"]').removeClass('hide');
10
+ });
11
+ });
@@ -0,0 +1,25 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
16
+
17
+ * {
18
+ font-family: 'Helvetica', sans-serif;
19
+ font-size: 14px;
20
+ }
21
+
22
+ .alert {
23
+ margin: 20px 0 0 0 !important;
24
+ text-align: center;
25
+ }
@@ -0,0 +1,16 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
5
+
6
+ form.console {
7
+ margin-top: 20px;
8
+ padding: 20px;
9
+ background-color: #fafafa;
10
+ border: 1px solid #ccc;
11
+ border-radius: 5px;
12
+ }
13
+
14
+ .form-group[data-klass] input {
15
+ margin-bottom: 10px;
16
+ }
@@ -0,0 +1,34 @@
1
+ module Pushable
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ def create
6
+ if @device = user_has_device_already?
7
+ update_token_expiration
8
+ else
9
+ @device = create_device_token
10
+ end
11
+ respond_with @device
12
+ end
13
+
14
+ private
15
+
16
+ def user_has_device_already?
17
+ current_user.devices.find_by token: params[:device][:token]
18
+ end
19
+
20
+ def update_token_expiration
21
+ @device.reset_token_expiry
22
+ @device.save
23
+ end
24
+
25
+ def create_device_token
26
+ current_user.devices.create permitted_params
27
+ end
28
+
29
+ def permitted_params
30
+ params.require(:device).permit :token, :platform
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ module Pushable
2
+ class ConsolesController < ::ActionController::Base
3
+ layout 'pushable/application'
4
+
5
+ def show
6
+ @push = Pushable::ConsolePush.new
7
+ @stubs = Pushable::Console.stubs
8
+ @klasses = @stubs.map(&:klass)
9
+ end
10
+
11
+ def push
12
+ @push = Pushable::ConsolePush.new permitted_params
13
+ if @push.valid? && @push.deliver
14
+ flash[:notice] = "Push delivered (sent to #{@push.sent_count} recipient(s))."
15
+ else
16
+ flash[:alert] = "Push failed! #{@push.errors.full_messages.to_sentence}."
17
+ end
18
+ show
19
+ render :show
20
+ end
21
+
22
+ private
23
+
24
+ def permitted_params
25
+ klass = params[:console_push][:klass]
26
+ params[:console_push].slice(:device_token, :klass).merge (params[:console_push][klass] || {})
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ module Pushable
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :devices, class_name: 'Pushable::Device', as: :pushable
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,50 @@
1
+ module Pushable
2
+ class ConsolePush
3
+ include ActiveModel::Model
4
+
5
+ attr_accessor :device_token, :klass, :data, :sent_count
6
+
7
+ validates :klass, :device_token, presence: true
8
+
9
+ def initialize(attributes = {})
10
+ super
11
+ @sent_count = 0
12
+ end
13
+
14
+ def deliver
15
+ data.each do |name, info|
16
+ setter = "#{name}="
17
+ if message.respond_to?(setter)
18
+ value = info['value']
19
+ value = value.to_i if info['type'] == 'integer'
20
+ message.send setter, value
21
+ end
22
+ end
23
+
24
+ devices.each do |device|
25
+ if recipient = device.pushable
26
+ response = message.send_to recipient
27
+ self.sent_count += 1
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def devices
35
+ @_devices ||= Pushable::Device.where(token: device_token)
36
+ end
37
+
38
+ def message
39
+ @_message ||= begin
40
+ _class = Class.new(klass.constantize) do
41
+ def initialize
42
+ end
43
+ end
44
+
45
+ _class.new
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,40 @@
1
+ module Pushable
2
+ class Device < ActiveRecord::Base
3
+ belongs_to :pushable, polymorphic: true
4
+
5
+ validates :token, presence: true, uniqueness: { scope: :pushable }
6
+ validates :platform, inclusion: { in: %w(ios android) }
7
+
8
+ before_save :reset_token_expiry
9
+
10
+ def reset_token_expiry
11
+ self.token_expires_at = Time.current + expire_token_in
12
+ end
13
+
14
+ def expire_token_in
15
+ 14.days
16
+ end
17
+
18
+ def token_expired?
19
+ token_expires_at.nil? || token_expires_at.past?
20
+ end
21
+
22
+ def self.android
23
+ where(platform: 'android')
24
+ end
25
+
26
+ def self.ios
27
+ where(platform: 'ios')
28
+ end
29
+
30
+ def self.owned_by(pushable)
31
+ klass = pushable.klass.name
32
+ table = pushable.table.name
33
+ joins("JOIN #{table} ON #{table}.id = pushable_devices.pushable_id").
34
+ where(pushable_type: klass).
35
+ merge(pushable).
36
+ uniq
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,50 @@
1
+ module Pushable
2
+ class Message
3
+ MAX_TOKENS_AT_ONCE = 999
4
+
5
+ def send_to(pushable)
6
+ if pushable.is_a? ActiveRecord::Relation
7
+ enqueue Device.android.owned_by(pushable), format(:android)
8
+ enqueue Device.ios.owned_by(pushable), format(:ios)
9
+ elsif pushable.is_a?(ActiveRecord::Base)
10
+ enqueue pushable.devices.android, format(:android)
11
+ enqueue pushable.devices.ios, format(:ios)
12
+ else
13
+ raise ArgumentError.new('#send_to only accepts an ActiveRecord Relation or ActiveRecord object!')
14
+ end
15
+ end
16
+
17
+ def enqueue(devices, payload)
18
+ devices.pluck(:id).each_slice(MAX_TOKENS_AT_ONCE) do |tokens|
19
+ Pushable::Messenger.perform_later tokens, payload.deep_stringify_keys
20
+ end
21
+ end
22
+
23
+ def alert
24
+ ''
25
+ end
26
+
27
+ def badge
28
+ 1
29
+ end
30
+
31
+ def sound
32
+ 'default'
33
+ end
34
+
35
+ def other
36
+ {}
37
+ end
38
+
39
+ private
40
+
41
+ def format(platform)
42
+ if platform == :ios
43
+ { alert: alert, badge: badge, sound: sound, other: other }
44
+ elsif platform == :android
45
+ { alert: alert }.merge(data: other)
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,24 @@
1
+ module Pushable
2
+ class Messenger < ActiveJob::Base
3
+
4
+ def perform(device_ids, payload)
5
+ send_gcm Device.android.where(id: device_ids).pluck(:token), payload
6
+ send_apns Device.ios.where(id: device_ids).pluck(:token), payload
7
+ end
8
+
9
+ def send_gcm(tokens, payload)
10
+ return unless tokens.any?
11
+ service = GCM::Service.new
12
+ notification = GCM::Notification.new(payload)
13
+ service.deliver notification, tokens
14
+ end
15
+
16
+ def send_apns(tokens, payload)
17
+ return unless tokens.any?
18
+ service = APNS::Service.new
19
+ notification = APNS::Notification.new(payload)
20
+ service.deliver notification, tokens
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Pushable</title>
5
+ <%= stylesheet_link_tag "//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" %>
6
+ <%= stylesheet_link_tag "pushable/application", media: "all" %>
7
+ <%= javascript_include_tag "//code.jquery.com/jquery-2.1.3.min.js" %>
8
+ <%= javascript_include_tag "pushable/application" %>
9
+ <%= csrf_meta_tags %>
10
+ </head>
11
+ <body>
12
+
13
+ <%= yield %>
14
+
15
+ </body>
16
+ </html>
@@ -0,0 +1,26 @@
1
+ <div class="container">
2
+ <% flash.each do |k, v| %>
3
+ <% klass = { notice: 'success', alert: 'danger' }.fetch(k.intern, 'info') %>
4
+ <div class="alert alert-<%= klass %>" role="alert"><%= v %></div>
5
+ <% end %>
6
+ <%= form_for @push, url: push_path, html: { class: 'console form col-md-6 col-md-offset-3' } do |f| %>
7
+ <div class="form-group">
8
+ <%= f.label :device_token %>
9
+ <%= f.text_field :device_token, class: 'form-control' %>
10
+ </div>
11
+ <div class="form-group">
12
+ <%= f.label :klass, 'Message' %>
13
+ <%= f.select :klass, ['', @klasses].flatten, {}, class: 'form-control' %>
14
+ </div>
15
+ <% @stubs.each do |stub| %>
16
+ <div class="form-group hide" data-klass="<%= stub.klass %>">
17
+ <%= f.label :data %>
18
+ <% stub.fields.each do |field, type| %>
19
+ <%= text_field_tag "console_push[#{stub.klass}][data][#{field}][value]", nil, placeholder: field, class: 'form-control' %>
20
+ <%= hidden_field_tag "console_push[#{stub.klass}][data][#{field}][type]", type.to_s %>
21
+ <% end %>
22
+ </div>
23
+ <% end %>
24
+ <%= f.submit 'Push', class: 'btn btn-primary' %>
25
+ <% end %>
26
+ </div>
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ Pushable::Engine.routes.draw do
2
+ post 'consoles/push', to: 'consoles#push', as: :push
3
+ root to: 'consoles#show'
4
+ end
@@ -0,0 +1,12 @@
1
+ class CreatePushableDevices < ActiveRecord::Migration
2
+ def change
3
+ create_table :pushable_devices do |t|
4
+ t.string :token
5
+ t.string :platform
6
+ t.references :pushable, polymorphic: true, index: true
7
+ t.datetime :token_expires_at
8
+
9
+ t.timestamps null: false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module Pushable
2
+ class Console
3
+
4
+ def self.stubs
5
+ @@stubs ||= []
6
+ end
7
+
8
+ def self.<<(stub)
9
+ stubs << stub
10
+ end
11
+
12
+ def self.reset
13
+ @@stubs = []
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Pushable
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Pushable
4
+
5
+ require 'responders'
6
+ require 'mercurius'
7
+
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module Pushable
2
+ class Stub
3
+ attr_reader :klass, :fields
4
+
5
+ def initialize(klass, fields = {})
6
+ @klass = klass
7
+ @fields = fields
8
+ validate
9
+ end
10
+
11
+ private
12
+
13
+ def validate
14
+ fields.each do |k, v|
15
+ raise ArgumentError.new("Field types must be in: [:string, :integer]") if ![:string, :integer].include?(v)
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Pushable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails'
2
+ require 'pushable/engine'
3
+ require 'pushable/console'
4
+ require 'pushable/stub'
5
+
6
+ module Pushable
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :pushable do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pushable-engine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Logan Serman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: responders
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mercurius
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: capybara
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: capybara-webkit
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: launchy
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
+ description: Rails engine to manage device tokens and push messaging.
126
+ email:
127
+ - logan.serman@metova.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - README.rdoc
134
+ - Rakefile
135
+ - app/assets/javascripts/pushable/application.js
136
+ - app/assets/javascripts/pushable/consoles.js
137
+ - app/assets/stylesheets/pushable/application.css
138
+ - app/assets/stylesheets/pushable/consoles.css
139
+ - app/controllers/concerns/pushable/controller.rb
140
+ - app/controllers/pushable/consoles_controller.rb
141
+ - app/models/concerns/pushable/model.rb
142
+ - app/models/pushable/console_push.rb
143
+ - app/models/pushable/device.rb
144
+ - app/models/pushable/message.rb
145
+ - app/models/pushable/messenger.rb
146
+ - app/views/layouts/pushable/application.html.erb
147
+ - app/views/pushable/consoles/show.html.erb
148
+ - config/routes.rb
149
+ - db/migrate/20150302191320_create_pushable_devices.rb
150
+ - lib/pushable-engine.rb
151
+ - lib/pushable/console.rb
152
+ - lib/pushable/engine.rb
153
+ - lib/pushable/stub.rb
154
+ - lib/pushable/version.rb
155
+ - lib/tasks/pushable_tasks.rake
156
+ homepage: https://github.com/lserman/pushable-engine
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.2.2
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Rails engine to manage device tokens and push messaging.
180
+ test_files: []
181
+ has_rdoc: