crusade_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +48 -0
  5. data/app/assets/javascripts/crusade.js +15 -0
  6. data/app/assets/javascripts/crusade_rails/configuration.js.coffee.erb +4 -0
  7. data/app/assets/javascripts/crusade_rails/crusade.js.coffee +46 -0
  8. data/app/assets/javascripts/crusade_rails/notification_permission.js.coffee +13 -0
  9. data/app/assets/stylesheets/crusade_rails/application.css +13 -0
  10. data/app/controllers/crusade_rails/apns/v1/log_controller.rb +13 -0
  11. data/app/controllers/crusade_rails/apns/v1/push_package_controller.rb +23 -0
  12. data/app/controllers/crusade_rails/apns/v1/subscription_controller.rb +37 -0
  13. data/app/controllers/crusade_rails/application_controller.rb +14 -0
  14. data/app/helpers/crusade_rails/application_helper.rb +4 -0
  15. data/app/models/crusade_rails/user_subscription.rb +4 -0
  16. data/app/services/crusade_rails/notification_service.rb +36 -0
  17. data/app/services/crusade_rails/user_subscription_service.rb +36 -0
  18. data/app/views/layouts/crusade_rails/application.html.erb +14 -0
  19. data/config/initializers/teaspoon.rb +65 -0
  20. data/config/routes.rb +15 -0
  21. data/db/migrate/20130917203302_create_crusade_rails_user_subscriptions.rb +12 -0
  22. data/lib/crusade_rails/configuration_loader.rb +33 -0
  23. data/lib/crusade_rails/engine.rb +9 -0
  24. data/lib/crusade_rails/version.rb +3 -0
  25. data/lib/crusade_rails.rb +4 -0
  26. data/lib/generators/crusade/install/USAGE +9 -0
  27. data/lib/generators/crusade/install/install_generator.rb +17 -0
  28. data/lib/generators/crusade/install/templates/configuration.yml +23 -0
  29. data/lib/generators/crusade/install/templates/initializer.rb +2 -0
  30. data/lib/tasks/crusade_rails_tasks.rake +4 -0
  31. data/test/controllers/apns/v1/log_controller_test.rb +35 -0
  32. data/test/controllers/apns/v1/push_package_controller_test.rb +41 -0
  33. data/test/controllers/apns/v1/subscription_controller_test.rb +61 -0
  34. data/test/crusade_rails_test.rb +7 -0
  35. data/test/dummy/README.rdoc +28 -0
  36. data/test/dummy/Rakefile +6 -0
  37. data/test/dummy/app/assets/javascripts/application.js +13 -0
  38. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  39. data/test/dummy/app/controllers/application_controller.rb +5 -0
  40. data/test/dummy/app/helpers/application_helper.rb +2 -0
  41. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  42. data/test/dummy/bin/bundle +3 -0
  43. data/test/dummy/bin/rails +4 -0
  44. data/test/dummy/bin/rake +4 -0
  45. data/test/dummy/config/application.rb +23 -0
  46. data/test/dummy/config/boot.rb +5 -0
  47. data/test/dummy/config/certificate.p12 +0 -0
  48. data/test/dummy/config/crusade.yml +23 -0
  49. data/test/dummy/config/crusade_absolute.yml +13 -0
  50. data/test/dummy/config/database.yml +25 -0
  51. data/test/dummy/config/environment.rb +5 -0
  52. data/test/dummy/config/environments/development.rb +29 -0
  53. data/test/dummy/config/environments/production.rb +80 -0
  54. data/test/dummy/config/environments/test.rb +36 -0
  55. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  56. data/test/dummy/config/initializers/crusade.rb +2 -0
  57. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  58. data/test/dummy/config/initializers/inflections.rb +16 -0
  59. data/test/dummy/config/initializers/mime_types.rb +5 -0
  60. data/test/dummy/config/initializers/secret_token.rb +12 -0
  61. data/test/dummy/config/initializers/session_store.rb +3 -0
  62. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  63. data/test/dummy/config/locales/en.yml +23 -0
  64. data/test/dummy/config/routes.rb +3 -0
  65. data/test/dummy/config.ru +4 -0
  66. data/test/dummy/db/development.sqlite3 +0 -0
  67. data/test/dummy/db/migrate/20130921092524_create_crusade_rails_user_subscriptions.crusade_rails.rb +13 -0
  68. data/test/dummy/db/schema.rb +34 -0
  69. data/test/dummy/db/test.sqlite3 +0 -0
  70. data/test/dummy/lib/icon.iconset/icon_128x128.png +0 -0
  71. data/test/dummy/lib/icon.iconset/icon_128x128@2x.png +0 -0
  72. data/test/dummy/lib/icon.iconset/icon_16x16.png +0 -0
  73. data/test/dummy/lib/icon.iconset/icon_16x16@2x.png +0 -0
  74. data/test/dummy/lib/icon.iconset/icon_32x32.png +0 -0
  75. data/test/dummy/lib/icon.iconset/icon_32x32@2x.png +0 -0
  76. data/test/dummy/log/development.log +9000 -0
  77. data/test/dummy/log/test.log +18388 -0
  78. data/test/dummy/public/404.html +58 -0
  79. data/test/dummy/public/422.html +58 -0
  80. data/test/dummy/public/500.html +57 -0
  81. data/test/dummy/public/favicon.ico +0 -0
  82. data/test/dummy/tmp/cache/assets/development/sprockets/0330051201f83b2ff3843e3c6747e79d +0 -0
  83. data/test/dummy/tmp/cache/assets/development/sprockets/0534641409a2140bc6e7ebc3ffb3a54d +0 -0
  84. data/test/dummy/tmp/cache/assets/development/sprockets/108a4cc5d6041a541455f23f064f718f +0 -0
  85. data/test/dummy/tmp/cache/assets/development/sprockets/10b6d318fbf01394655188cb55968932 +0 -0
  86. data/test/dummy/tmp/cache/assets/development/sprockets/1903ce46cdbce5cbddb3f58cacb00c89 +0 -0
  87. data/test/dummy/tmp/cache/assets/development/sprockets/19e13c514bd0c76f667eacd20d91d72c +0 -0
  88. data/test/dummy/tmp/cache/assets/development/sprockets/1d295354fabfd91032e5c51cf32fe192 +0 -0
  89. data/test/dummy/tmp/cache/assets/development/sprockets/24efeb1e9e576159a181fb53b3f114a7 +0 -0
  90. data/test/dummy/tmp/cache/assets/development/sprockets/27cb4afd1625534b2a374a81b9df26de +0 -0
  91. data/test/dummy/tmp/cache/assets/development/sprockets/28fcfef8d3cf8654ba549bc1761e83e4 +0 -0
  92. data/test/dummy/tmp/cache/assets/development/sprockets/2969a922d5c2342a65712f8bd9cc99a6 +0 -0
  93. data/test/dummy/tmp/cache/assets/development/sprockets/2de3b3bd851fd121492b6d4ac403e556 +0 -0
  94. data/test/dummy/tmp/cache/assets/development/sprockets/2e0be385ec13186af8c1ea31b03b96fd +0 -0
  95. data/test/dummy/tmp/cache/assets/development/sprockets/2e465486b1a6c8f2d49a4ed114d70396 +0 -0
  96. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  97. data/test/dummy/tmp/cache/assets/development/sprockets/3125a19a8cea48c16b56e8b59a5c5294 +0 -0
  98. data/test/dummy/tmp/cache/assets/development/sprockets/3cc705332293b3c69203db8a96e9d830 +0 -0
  99. data/test/dummy/tmp/cache/assets/development/sprockets/3cd8491f62d0bb72dc92a13205463599 +0 -0
  100. data/test/dummy/tmp/cache/assets/development/sprockets/3cde555d17b62d65401cc9272bae6e08 +0 -0
  101. data/test/dummy/tmp/cache/assets/development/sprockets/556f3a4cc7bf634157a1bcb26a3d2b6d +0 -0
  102. data/test/dummy/tmp/cache/assets/development/sprockets/5f268780e6c88d505c5593761755010a +0 -0
  103. data/test/dummy/tmp/cache/assets/development/sprockets/6701d9e3411fdcd8e5e547bdec87cb66 +0 -0
  104. data/test/dummy/tmp/cache/assets/development/sprockets/6b77a9aa8954a56d3da421ee8f0813e8 +0 -0
  105. data/test/dummy/tmp/cache/assets/development/sprockets/6f19bfcf39fc6e2ba42b315015e41741 +0 -0
  106. data/test/dummy/tmp/cache/assets/development/sprockets/72194bfe82d488c5e022c9046c0581c5 +0 -0
  107. data/test/dummy/tmp/cache/assets/development/sprockets/732aa0a732c68b36c462c1aa61e9c5db +0 -0
  108. data/test/dummy/tmp/cache/assets/development/sprockets/742719bcba2beaa6ee593b3c6eabe40b +0 -0
  109. data/test/dummy/tmp/cache/assets/development/sprockets/753c20567a3407e94309e3a586ba70c1 +0 -0
  110. data/test/dummy/tmp/cache/assets/development/sprockets/76d2c2ca1f8747e7c285677d59d1f9d9 +0 -0
  111. data/test/dummy/tmp/cache/assets/development/sprockets/8674169919e38981b419dbd475a583aa +0 -0
  112. data/test/dummy/tmp/cache/assets/development/sprockets/8770d1f7ac46ca50bf453462671ea0b9 +0 -0
  113. data/test/dummy/tmp/cache/assets/development/sprockets/8b4c20d1cbaeadc08407a48823e8c7c3 +0 -0
  114. data/test/dummy/tmp/cache/assets/development/sprockets/8d66da576394374d14318805fe809466 +0 -0
  115. data/test/dummy/tmp/cache/assets/development/sprockets/9aacc018015dba444d327301c20b63c5 +0 -0
  116. data/test/dummy/tmp/cache/assets/development/sprockets/9bf8f0dcacf365b365a44fc7acaf5b33 +0 -0
  117. data/test/dummy/tmp/cache/assets/development/sprockets/9f6a5c70035131b2862f0cf1b1ffba5f +0 -0
  118. data/test/dummy/tmp/cache/assets/development/sprockets/aa3a6aa3669f27355e35fd070b5ec5cc +0 -0
  119. data/test/dummy/tmp/cache/assets/development/sprockets/b0f0a9774bdd9aba74bc3dbaa280abd2 +0 -0
  120. data/test/dummy/tmp/cache/assets/development/sprockets/b116102c65d66d0bac493130339e906f +0 -0
  121. data/test/dummy/tmp/cache/assets/development/sprockets/b3c46efa2d626eb47c04b3299485ada2 +0 -0
  122. data/test/dummy/tmp/cache/assets/development/sprockets/b60162f01dfafeaa2fd08552bee0d7b7 +0 -0
  123. data/test/dummy/tmp/cache/assets/development/sprockets/ba4082b2576e0439500c1de73f75e722 +0 -0
  124. data/test/dummy/tmp/cache/assets/development/sprockets/bd98cfe5b883a49d02e51dff15eb2d62 +0 -0
  125. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  126. data/test/dummy/tmp/cache/assets/development/sprockets/d0ba95da0d65927f38a86ec70536acfa +0 -0
  127. data/test/dummy/tmp/cache/assets/development/sprockets/ddcd3a7d9f01da29b22f5bb8894c9e9d +0 -0
  128. data/test/dummy/tmp/cache/assets/development/sprockets/dfb8f1f4a35d49c4853bb839dd074b03 +0 -0
  129. data/test/dummy/tmp/cache/assets/development/sprockets/edb1e911142e4ffa330d5f54129ae937 +0 -0
  130. data/test/dummy/tmp/cache/assets/development/sprockets/f3676986bcff0feb06ca29fc36c62054 +0 -0
  131. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  132. data/test/dummy/tmp/cache/assets/development/sprockets/f8e371443421803a78fdbe7fb29531b7 +0 -0
  133. data/test/dummy/tmp/cache/assets/development/sprockets/f9b223cb5385faca472d99ad42c60776 +0 -0
  134. data/test/dummy/tmp/cache/assets/development/sprockets/fcd414d106937117c1f77dafcb48d32d +0 -0
  135. data/test/dummy/tmp/cache/assets/development/sprockets/ff6fd5ae68516118b84d0dc0485e4456 +0 -0
  136. data/test/fixtures/crusade_rails/default_routes.rb +6 -0
  137. data/test/generators/install_generator_test.rb +66 -0
  138. data/test/integration/configuration_loader_test.rb +56 -0
  139. data/test/integration/push_package_integration_test.rb +14 -0
  140. data/test/javascripts/support/jquery.js +9790 -0
  141. data/test/javascripts/support/safari.coffee +7 -0
  142. data/test/javascripts/test_helper.coffee +25 -0
  143. data/test/javascripts/unit/configuration_test.js.coffee +11 -0
  144. data/test/javascripts/unit/crusade_test.js.coffee +102 -0
  145. data/test/javascripts/unit/notification_permission_test.js.coffee +49 -0
  146. data/test/routes/apns_routes_test.rb +39 -0
  147. data/test/services/notification_service_test.rb +69 -0
  148. data/test/services/user_subscription_service_test.rb +104 -0
  149. data/test/support/fake_push_notification.rb +24 -0
  150. data/test/teaspoon_env.rb +33 -0
  151. data/test/test_helper.rb +27 -0
  152. metadata +468 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 667a327626f4b01ed39136f2f80d48d9adb43ee2
4
+ data.tar.gz: ee77ad2e8b097a26761baeb8b44cf9b93ba5f9b1
5
+ SHA512:
6
+ metadata.gz: 3a05c52203dd4d1ddafbb7d8ecc04482deff5cae6eae91ae155f4214afcee1f8da5685418c86672b0967766c41d993500edabf940bcd5e851419d265187cdea7
7
+ data.tar.gz: 16f7b9c8411de096b05af79de79931e45edfe50b36a665d444e56f243fa121029ec4027f3735091f4007091f7514b1674dfa1f8a0cc23990cc82febd1726058a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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
+ = CrusadeRails
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
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 = 'CrusadeRails'
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("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+ desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'
34
+ task routes: :environment do
35
+ all_routes = CrusadeRails::Engine.routes.routes
36
+ require 'action_dispatch/routing/inspector'
37
+ inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
38
+ puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
39
+ end
40
+
41
+ desc "Run the javascript specs"
42
+ task :teaspoon => :environment do |t, args|
43
+ require "teaspoon/console"
44
+ files = ENV['files'].nil? ? [] : ENV['files'].split(',')
45
+ fail if Teaspoon::Console.new({suite: ENV["suite"], driver_cli_options: ENV["driver_cli_options"]}, files ).execute
46
+ end
47
+
48
+ task default: :test
@@ -0,0 +1,15 @@
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 vendor/assets/javascripts of plugins, if any, 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
+ //= jquery
14
+ //= require ./crusade_rails/configuration
15
+ //= require_tree ./crusade_rails
@@ -0,0 +1,4 @@
1
+
2
+ window.crusade =
3
+ pushId: '<%= Rails.application.config.crusade.push_id %>'
4
+ webserviceUrl: '<%= Rails.application.config.crusade.webservice_url %>'
@@ -0,0 +1,46 @@
1
+
2
+ class @Crusade
3
+
4
+ requestPermission: (userId) ->
5
+ promise = $.Deferred((defer) =>
6
+ permission = currentPermission()
7
+
8
+ resolve(permission, defer, userId)
9
+
10
+ ).promise()
11
+
12
+ promise.granted = promise.done
13
+ promise.denied = promise.fail
14
+
15
+ promise
16
+
17
+ checkPermission: ->
18
+ currentPermission()
19
+
20
+ currentPermission = ->
21
+ wrapPermission window.safari.pushNotification.permission(window.crusade.pushId)
22
+
23
+ wrapPermission = (safariPermission) ->
24
+ new NotificationPermission safariPermission
25
+
26
+ requestNewPemission = (userId) ->
27
+ $.Deferred((defer) =>
28
+ window.safari.pushNotification.requestPermission(
29
+ window.crusade.webserviceUrl
30
+ window.crusade.pushId
31
+ { userId: userId }
32
+ (safariPermission) =>
33
+ permission = wrapPermission safariPermission
34
+
35
+ resolve(permission, defer, userId)
36
+ )
37
+ ).promise()
38
+
39
+ resolve = (permission, defer, userId) ->
40
+ switch permission.permission
41
+ when 'granted' then defer.resolve(permission)
42
+ when 'denied' then defer.reject(permission)
43
+ when 'default'
44
+ requestNewPemission(userId).
45
+ done((permission) -> defer.resolve(permission)).
46
+ fail((permission) -> defer.reject(permission))
@@ -0,0 +1,13 @@
1
+ class @NotificationPermission
2
+
3
+ constructor: (safariPermission) ->
4
+ safariPermission = defaultPermission() unless safariPermission
5
+
6
+ @permission = safariPermission.permission
7
+ @deviceToken = safariPermission.deviceToken
8
+
9
+ isGranted: ->
10
+ @permission == "granted"
11
+
12
+ defaultPermission = ->
13
+ { permission: "default", deviceToken: null }
@@ -0,0 +1,13 @@
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 vendor/assets/stylesheets of plugins, if any, 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 top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,13 @@
1
+ module CrusadeRails
2
+ module Apns
3
+ module V1
4
+ class LogController < CrusadeRails::ApplicationController
5
+ def create
6
+ Rails.logger.error JSON.pretty_generate json
7
+
8
+ render nothing: true, status: 204
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ module CrusadeRails
2
+ module Apns
3
+ module V1
4
+ class PushPackageController < CrusadeRails::ApplicationController
5
+ skip_before_action :verify_authenticity_token
6
+
7
+ def create
8
+ generator = Crusade::APNS::PushPackageGenerator.new Rails.application.config.crusade
9
+ user_id = json['user']
10
+
11
+ Rails.logger.debug "requesting push package for user #{user_id}"
12
+ user_token, push_package = generator.generate(user_id)
13
+
14
+ Rails.logger.debug "registering push package #{user_id} - #{user_token}"
15
+ CrusadeRails::UserSubscriptionService.new.register user_id, user_token
16
+
17
+ Rails.logger.debug "Generation done. sending the file"
18
+ send_data File.read(push_package), type: 'application/zip'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ module CrusadeRails
2
+ module Apns
3
+ module V1
4
+ class SubscriptionController < CrusadeRails::ApplicationController
5
+ def create
6
+ Rails.logger.debug "registring push notification #{user_token} - #{device_token}"
7
+
8
+ user_service.subscribe user_token, device_token
9
+
10
+ render nothing: true, status: 200
11
+ end
12
+
13
+ def delete
14
+ Rails.logger.debug "unregistring push notification #{user_token} - #{device_token}"
15
+
16
+ user_service.unsubscribe user_token, device_token
17
+
18
+ render nothing: true, status: 200
19
+ end
20
+
21
+ private
22
+
23
+ def device_token
24
+ params[:device_token]
25
+ end
26
+
27
+ def user_token
28
+ request.headers['Authorization'].gsub 'ApplePushNotifications ', ''
29
+ end
30
+
31
+ def user_service
32
+ @service ||= CrusadeRails::UserSubscriptionService.new
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ module CrusadeRails
2
+ class ApplicationController < ActionController::Base
3
+
4
+ protected
5
+
6
+ def body
7
+ request.body.read
8
+ end
9
+
10
+ def json
11
+ JSON.parse body
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module CrusadeRails
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module CrusadeRails
2
+ class UserSubscription < ActiveRecord::Base
3
+ end
4
+ end
@@ -0,0 +1,36 @@
1
+ module CrusadeRails
2
+ class NotificationService
3
+ attr_reader :push_notification
4
+
5
+ def initialize push_notification = nil
6
+ push_notification = default_push_notification unless push_notification
7
+ self.push_notification = push_notification
8
+ end
9
+
10
+ def send_notification user_id, notification_attrs
11
+ subscription = UserSubscription.where(user_id: user_id).first
12
+
13
+ return unless subscription && subscription.active?
14
+
15
+ send build_notification(notification_attrs, subscription)
16
+ end
17
+
18
+ private
19
+
20
+ attr_writer :push_notification
21
+
22
+ def send notification
23
+ push_notification.send notification
24
+ end
25
+
26
+ def build_notification notification_attrs, subscription
27
+ Crusade::APNS::Notification.new notification_attrs.merge(
28
+ device_token: subscription.device_token
29
+ )
30
+ end
31
+
32
+ def default_push_notification
33
+ Crusade::APNS::PushNotification.new(Rails.application.config.crusade)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ module CrusadeRails
2
+ class UserSubscriptionService
3
+ def register user_id, user_token
4
+ subscription = CrusadeRails::UserSubscription.new({
5
+ user_token: user_token,
6
+ user_id: user_id
7
+ })
8
+ subscription.save
9
+ end
10
+
11
+ def subscribe user_token, device_token
12
+ subscription = find_subscription_by_user_token user_token
13
+ subscription.update_attributes({
14
+ device_token: device_token,
15
+ active: true
16
+ })
17
+ subscription.save
18
+ end
19
+
20
+ def unsubscribe user_token, device_token
21
+ subscription = find_subscription_by_user_token user_token
22
+ subscription.update_attributes({
23
+ active: false
24
+ })
25
+ subscription.save
26
+ end
27
+
28
+ private
29
+
30
+ attr_accessor :user_id
31
+
32
+ def find_subscription_by_user_token(user_token)
33
+ CrusadeRails::UserSubscription.where(user_token: user_token).first
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>CrusadeRails</title>
5
+ <%= stylesheet_link_tag "crusade_rails/application", media: "all" %>
6
+ <%= javascript_include_tag "crusade_rails/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,65 @@
1
+ Teaspoon.setup do |config|
2
+ require 'coffee_script'
3
+
4
+ # This determines where the Teaspoon routes will be mounted. Changing this to "/jasmine" would allow you to browse to
5
+ # http://localhost:3000/jasmine to run your specs.
6
+ config.mount_at = "/teaspoon"
7
+
8
+ # This defaults to Rails.root if left nil. If you're testing an engine using a dummy application it can be useful to
9
+ # set this to your engines root.. E.g. `Teaspoon::Engine.root`
10
+ config.root = CrusadeRails::Engine.root
11
+
12
+ # These paths are appended to the Rails assets paths (relative to config.root), and by default is an array that you
13
+ # can replace or add to.
14
+ config.asset_paths = ["test/javascripts", "test/javascripts/stylesheets"]
15
+
16
+ # Fixtures are rendered through a standard controller. This means you can use things like HAML or RABL/JBuilder, etc.
17
+ # to generate fixtures within this path.
18
+ config.fixture_path = "test/javascripts/fixtures"
19
+
20
+ # You can modify the default suite configuration and create new suites here. Suites can be isolated from one another.
21
+ # When defining a suite you can provide a name and a block. If the name is left blank, :default is assumed. You can
22
+ # omit various directives and the defaults will be used.
23
+ #
24
+ # To run a specific suite
25
+ # - in the browser: http://localhost/teaspoon/[suite_name]
26
+ # - from the command line: rake teaspoon suite=[suite_name]
27
+ config.suite do |suite|
28
+
29
+ # You can specify a file matcher and all matching files will be loaded when the suite is run. It's important that
30
+ # these files are serve-able from sprockets.
31
+ #
32
+ # Note: Can also be set to nil.
33
+ suite.matcher = "{test/javascripts,app/assets}/**/*.{js,js.coffee,coffee}"
34
+
35
+ # Each suite can load a different helper, which can in turn require additional files. This file is loaded before
36
+ # your specs are loaded, and can be used as a manifest.
37
+ suite.helper = "test_helper"
38
+
39
+ # These are the core Teaspoon javascripts. It's strongly encouraged to include only the base files here. You can
40
+ # require other support libraries in your spec helper, which allows you to change them without having to restart the
41
+ # server.
42
+ #
43
+ # Available frameworks: teaspoon-jasmine, teaspoon-mocha, teaspoon-qunit
44
+ #
45
+ # Note: To use the CoffeeScript source files use `"teaspoon/jasmine"` etc.
46
+ suite.javascripts = ["teaspoon-jasmine"]
47
+
48
+ # If you want to change how Teaspoon looks, or include your own stylesheets you can do that here. The default is the
49
+ # stylesheet for the HTML reporter.
50
+ suite.stylesheets = ["teaspoon"]
51
+
52
+ # When running coverage reports, you probably want to exclude libraries that you're not testing.
53
+ # Accepts an array of filenames or regular expressions. The default is to exclude assets from vendors or gems.
54
+ suite.no_coverage = [%r{/lib/ruby/gems/}, %r{/vendor/assets/}, %r{/support/}, %r{/(.+)_helper.}]
55
+ # suite.no_coverage << "jquery.min.js" # excludes jquery from coverage reports
56
+
57
+ end
58
+
59
+ # Example suite. Since we're just filtering to files already within the root test/javascripts, these files will also
60
+ # be run in the default suite -- but can be focused into a more specific suite.
61
+ #config.suite :targeted do |suite|
62
+ # suite.matcher = "spec/javascripts/targeted/*_test.{js,js.coffee,coffee}"
63
+ #end
64
+
65
+ end if defined?(Teaspoon) && Teaspoon.respond_to?(:setup) # let Teaspoon be undefined outside of development/test/asset groups
data/config/routes.rb ADDED
@@ -0,0 +1,15 @@
1
+ CrusadeRails::Engine.routes.draw do
2
+ namespace "apns" do
3
+ namespace "v1" do
4
+ post "pushPackages/#{Rails.application.config.crusade.push_id}",
5
+ to: "push_package#create", as: "apns_push_package"
6
+
7
+ post "/devices/:device_token/registrations/#{Rails.application.config.crusade.push_id}",
8
+ to: "subscription#create", as: "apns_push_registration"
9
+ delete "/devices/:device_token/registrations/#{Rails.application.config.crusade.push_id}",
10
+ to: "subscription#delete", as: "apns_push_unregistration"
11
+
12
+ post "log", to: "log#create", as: "apns_log"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ class CreateCrusadeRailsUserSubscriptions < ActiveRecord::Migration
2
+ def change
3
+ create_table :crusade_rails_user_subscriptions do |t|
4
+ t.integer :user_id
5
+ t.string :user_token
6
+ t.string :device_token
7
+ t.boolean :active
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ require 'pathname'
2
+
3
+ module CrusadeRails
4
+ def load_configuration!(configuration_file = default_configuration_file)
5
+ configuration = load_configuration_file configuration_file
6
+ configuration = preappend_absolute_path(configuration)
7
+ Rails.application.config.crusade = configuration
8
+ configuration
9
+ end
10
+
11
+ extend self
12
+
13
+ private
14
+
15
+ def load_configuration_file(config_file)
16
+ Crusade::APNS::Configuration.load config_file
17
+ end
18
+
19
+ def default_configuration_file
20
+ File.join Rails.root, "config", "crusade.yml"
21
+ end
22
+
23
+ def preappend_absolute_path(config)
24
+ config.iconset_dir = change_path config.iconset_dir
25
+ config.certificate = change_path config.certificate
26
+ config
27
+ end
28
+
29
+ def change_path(path)
30
+ pathname = Pathname.new path
31
+ pathname.absolute? ? path : File.join(Rails.root, path)
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ require 'crusade/apns'
2
+
3
+ module CrusadeRails
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace CrusadeRails
6
+ end
7
+ end
8
+
9
+ require 'crusade_rails/configuration_loader'
@@ -0,0 +1,3 @@
1
+ module CrusadeRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "crusade_rails/engine"
2
+
3
+ module CrusadeRails
4
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Install crusade engine configuration & initializer files
3
+
4
+ Example:
5
+ rails generate crusade:install
6
+
7
+ This will create:
8
+ config/initializers/crusade.rb
9
+ config/crusade.yml
@@ -0,0 +1,17 @@
1
+ require 'rails/generators'
2
+
3
+ class Crusade::InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_initializer_file
7
+ template "initializer.rb", File.join(destination_root, "config", "initializers", "crusade.rb")
8
+ end
9
+
10
+ def copy_configuration_file
11
+ template "configuration.yml", File.join(destination_root, "config", "crusade.yml")
12
+ end
13
+
14
+ def update_routes
15
+ route 'mount CrusadeRails::Engine, at: "/push"'
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ default: &default
2
+ site_name: Apns config fixture
3
+ push_id: web.com.example.push.id
4
+ iconset_dir: lib/icon.iconset
5
+ certificate: config/certificate.p12
6
+ certificate_password: test
7
+
8
+ development:
9
+ <<: *default
10
+ url_format: http://www.example.com/push/%@
11
+ webservice_url: https://www.example.com/push
12
+ allowed_domains:
13
+ - http://www.example.com
14
+ - https://www.example.com
15
+
16
+ test:
17
+ <<: *default
18
+ url_format: http://localhost/push/%@
19
+ webservice_url: https://localhost/push
20
+ allowed_domains:
21
+ - http://localhost
22
+ - https://localhost
23
+ certificate_password:
@@ -0,0 +1,2 @@
1
+
2
+ CrusadeRails.load_configuration!
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :crusade_rails do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ describe CrusadeRails::Apns::V1::LogController do
4
+
5
+ before { @routes = CrusadeRails::Engine.routes }
6
+
7
+ describe "create action" do
8
+ let(:error) { { error: 'something went wrong' } }
9
+ let(:pretty_error) { "{\n \"logs\": {\n \"error\": \"something went wrong\"\n }\n}" }
10
+
11
+ before do
12
+ request.env['RAW_POST_DATA'] = { logs: error }.to_json
13
+ post :create, logs: error, :format => :json
14
+ end
15
+
16
+ it 'renders a 204 - no content' do
17
+ response.status.must_equal 204
18
+ end
19
+
20
+ it 'renders an empty body response' do
21
+ response.body.blank?.must_be_true
22
+ end
23
+
24
+ it 'logs the error via the rails logger' do
25
+ mock_logger = MiniTest::Mock.new
26
+ mock_logger.expect(:error, nil, [pretty_error])
27
+
28
+ Rails.stub(:logger, mock_logger) do
29
+ post :create, :format => :json
30
+ end
31
+
32
+ mock_logger.verify.must_be_truthy
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ describe CrusadeRails::Apns::V1::PushPackageController do
4
+
5
+ before { @routes = CrusadeRails::Engine.routes }
6
+
7
+ describe "create action" do
8
+ let(:temp_file) { '/tmp/text.txt' }
9
+ let(:user_id) { 10 }
10
+ let(:user_token) { '123zz' }
11
+
12
+ let(:service) { stub_everything(:user_service) }
13
+
14
+ before do
15
+ File.stubs(:read).with(temp_file).returns('test data')
16
+
17
+ generator = stub :generator
18
+ generator.stubs(:generate).with(user_id).returns [user_token, temp_file]
19
+ Crusade::APNS::PushPackageGenerator.stubs(:new).returns generator
20
+
21
+ CrusadeRails::UserSubscriptionService.stubs(:new).returns(service)
22
+
23
+ request.env['RAW_POST_DATA'] = { user: user_id }.to_json
24
+
25
+ post :create, :format => :json
26
+ end
27
+
28
+ it { response.status.must_equal 200 }
29
+ it { response.content_type.must_equal 'application/zip' }
30
+
31
+ it 'send the push package zip file' do
32
+ response.body.must_equal 'test data'
33
+ end
34
+
35
+ it 'register the user_id push package request' do
36
+ service.expects(:register).with(user_id, user_token)
37
+
38
+ post :create, :format => :json
39
+ end
40
+ end
41
+ end