maia 0.0.1

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: 07e9b4e9e339d8ea7471b6c9e8aaebdfbdd548c8
4
+ data.tar.gz: 6fd7307ccb07daa05743742be5170bf47a418b60
5
+ SHA512:
6
+ metadata.gz: d6199c40dcabcd6ac6ea904f0e1e304236b923ef9a3fc71e10d6f9475f2bdca93663bee40573d7a6415bacb52362c8830a1fd74e067921eaf79a18b6bcbcc604
7
+ data.tar.gz: ca3419fe349c23fa6c292b2d9d4961a563b4089f1fdbb764ae11527d3cd91bc168259537e8df9ebc2960d342f79c7092d519ed9c0e7700040a84720c025b044a
@@ -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.
@@ -0,0 +1,17 @@
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
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
8
+ load 'rails/tasks/engine.rake'
9
+
10
+
11
+ load 'rails/tasks/statistics.rake'
12
+
13
+ Bundler::GemHelper.install_tasks
14
+
15
+ require 'rspec/core/rake_task'
16
+ task :default => :spec
17
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,32 @@
1
+ module Maia
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ def create
6
+ if @device = existing_device
7
+ update_token_expiration @device
8
+ else
9
+ @device = create_device_token
10
+ end
11
+ respond_with @device
12
+ end
13
+
14
+ private
15
+ def existing_device
16
+ current_user.devices.find_by token: params[:device][:token]
17
+ end
18
+
19
+ def update_token_expiration(device)
20
+ device.reset_token_expiry
21
+ device.save
22
+ end
23
+
24
+ def create_device_token
25
+ current_user.devices.create permitted_params
26
+ end
27
+
28
+ def permitted_params
29
+ params.require(:device).permit :token
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ module Maia
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :devices, class_name: 'Maia::Device', as: :pushable
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ module Maia
2
+ class Device < ActiveRecord::Base
3
+ belongs_to :pushable, polymorphic: true
4
+
5
+ validates :token, presence: true, uniqueness: { scope: :pushable }
6
+
7
+ before_save :reset_token_expiry
8
+
9
+ def reset_token_expiry
10
+ self.token_expires_at = Time.current + expire_token_in
11
+ end
12
+
13
+ def expire_token_in
14
+ 14.days
15
+ end
16
+
17
+ def token_expired?
18
+ token_expires_at.nil? || token_expires_at.past?
19
+ end
20
+
21
+ def self.owned_by(pushable)
22
+ klass = pushable.klass.name
23
+ table = pushable.table.name
24
+ joins("JOIN #{table} ON #{table}.id = #{table_name}.pushable_id").
25
+ where(pushable_type: klass).
26
+ merge(pushable).
27
+ uniq
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Maia Test Console</title>
5
+ <%= stylesheet_link_tag "//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" %>
6
+ <%= javascript_include_tag "//code.jquery.com/jquery-2.1.3.min.js" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <%= yield %>
11
+ </body>
12
+ </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>
@@ -0,0 +1,4 @@
1
+ Maia::Engine.routes.draw do
2
+ post 'messages/push', to: 'messages#push', as: :push
3
+ root to: 'messages#new'
4
+ end
@@ -0,0 +1,11 @@
1
+ class CreateMaiaDevices < ActiveRecord::Migration
2
+ def change
3
+ create_table :maia_devices do |t|
4
+ t.references :pushable, polymorphic: true, index: true
5
+ t.string :token
6
+ t.datetime :token_expires_at
7
+
8
+ t.timestamps null: false
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails'
2
+ require 'maia/engine'
3
+ require 'maia/message'
4
+ require 'maia/messenger'
5
+ require 'maia/poke'
6
+ require 'maia/error'
7
+
8
+ module Maia
9
+ end
@@ -0,0 +1,8 @@
1
+ module Maia
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Maia
4
+
5
+ require 'mercurius'
6
+ require 'responders'
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module Maia
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,58 @@
1
+ module Maia
2
+ class Message
3
+ MAX_TOKENS_AT_ONCE = 999
4
+
5
+ def send_to(pushable)
6
+ devices =
7
+ case pushable
8
+ when ActiveRecord::Relation
9
+ Device.owned_by pushable
10
+ when ActiveRecord::Base
11
+ pushable.devices
12
+ else
13
+ raise ArgumentError.new
14
+ end
15
+
16
+ enqueue devices, to_h
17
+ end
18
+
19
+ def enqueue(devices, payload)
20
+ devices.pluck(:token).each_slice(MAX_TOKENS_AT_ONCE) do |tokens|
21
+ Maia::Messenger.perform_later tokens, payload.deep_stringify_keys
22
+ end
23
+ end
24
+
25
+ def alert
26
+ ''
27
+ end
28
+
29
+ def badge
30
+ nil
31
+ end
32
+
33
+ def sound
34
+ 'default'
35
+ end
36
+
37
+ def other
38
+ {}
39
+ end
40
+
41
+ def content_available?
42
+ false
43
+ end
44
+
45
+ def to_h
46
+ {
47
+ data: other,
48
+ content_available: content_available?,
49
+ notification: {
50
+ title: alert,
51
+ body: alert,
52
+ sound: sound,
53
+ badge: badge
54
+ }.compact
55
+ }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,52 @@
1
+ module Maia
2
+ class Messenger < ActiveJob::Base
3
+
4
+ def perform(tokens, payload)
5
+ logger.info "Pushing to #{tokens.size} token(s)..."
6
+ logger.info "Payload: #{payload}"
7
+
8
+ notification = GCM::Notification.new(payload)
9
+ responses = gcm.deliver(notification, tokens)
10
+
11
+ responses.each do |response|
12
+ if error = response.error
13
+ raise Maia::Error, error
14
+ else
15
+ handle_failed_tokens response.results.failed
16
+ update_devices_to_use_canonical_ids response.results.with_canonical_ids
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+ def handle_failed_tokens(results)
23
+ results.each do |result|
24
+ device = Maia::Device.find_by(token: result.token)
25
+ case result.error
26
+ when /InvalidRegistration/, /NotRegistered/
27
+ logger.info "Invalid token #{device.token} - destroying device #{device.id}."
28
+ device.destroy
29
+ when /MessageTooBig/
30
+ logger.info 'Message sent was too large.'
31
+ when /Timeout/
32
+ logger.info 'Request to GCM timed out'
33
+ end
34
+ end
35
+ end
36
+
37
+ def update_devices_to_use_canonical_ids(results)
38
+ results.each do |result|
39
+ device = Maia::Device.find_by token: result.token
40
+ device.update token: result.canonical_id
41
+ end
42
+ end
43
+
44
+ def gcm
45
+ @_service ||= GCM::Service.new connection: connection
46
+ end
47
+
48
+ def connection
49
+ nil # use the default
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ module Maia
2
+ class Poke < Message
3
+ def alert
4
+ 'Poke'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Maia
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maia
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-09-10 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.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activejob
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: responders
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mercurius
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.9
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.9
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
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: rspec-rails
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
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: capybara-webkit
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: launchy
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Manage device tokens and push messaging with Rails and Mercurius.
154
+ email:
155
+ - loganserman@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - MIT-LICENSE
161
+ - Rakefile
162
+ - app/controllers/concerns/maia/controller.rb
163
+ - app/models/concerns/maia/model.rb
164
+ - app/models/maia/device.rb
165
+ - app/views/layouts/maia/application.html.erb
166
+ - app/views/maia/messages/new.html.erb
167
+ - config/routes.rb
168
+ - db/migrate/20150302191320_create_maia_devices.rb
169
+ - lib/maia.rb
170
+ - lib/maia/engine.rb
171
+ - lib/maia/error.rb
172
+ - lib/maia/message.rb
173
+ - lib/maia/messenger.rb
174
+ - lib/maia/poke.rb
175
+ - lib/maia/version.rb
176
+ homepage: https://github.com/lserman/maia
177
+ licenses:
178
+ - MIT
179
+ metadata: {}
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubyforge_project:
196
+ rubygems_version: 2.4.5.1
197
+ signing_key:
198
+ specification_version: 4
199
+ summary: Manage device tokens and push messaging with Rails and Mercurius.
200
+ test_files: []