acts_as_hoc_pushable 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d00454537e4bab721f743e015a0480f109ee6b18
4
+ data.tar.gz: f52dc1694290daf75ac9f576b903a5ef08164afa
5
+ SHA512:
6
+ metadata.gz: 6ca7e19d67c12c9371bab1f1c33a3273f162c22640abeebdef5a25b44dd1bcb66ac2a80a9de421bc28ac6679e799f5c10a78f2599de5d724e7aee6ccc444e4c7
7
+ data.tar.gz: 80f7a7592b607a4b67dabf21710fabc397a2a40819c75e83e962e25355def575d609061379d4d05a632f612ee55b5b43c65cb61e2f1b1abec4682fd56f6d6f69
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in acts_as_hoc_pushable.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Gert Lavsen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,82 @@
1
+ # ActsAsHocPushable
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/acts_as_hoc_pushable`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'acts_as_hoc_pushable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install acts_as_hoc_pushable
22
+
23
+ ## Usage
24
+
25
+ ### Make application ready for acts_as_hoc_pushable
26
+ Install by running:
27
+
28
+ $ rails generate acts_as_hoc_pushable:install
29
+
30
+ This will create, models, migrations and initializer
31
+
32
+ ### Extend model
33
+ Add the `acts_as_hoc_pushable` to the model you want to be extended with push functionallity - eg. user.
34
+
35
+ ```ruby
36
+ #app/models/user.rb
37
+ class User < ApplicationRecord
38
+ acts_as_hoc_pushable
39
+ end
40
+ ```
41
+
42
+ now you have the following functionallity added to the model
43
+
44
+ * Add a device to an user
45
+ `user.add_device(token:, platform:, platform_version:, push_environment:)`
46
+ `user.add_device(params)`
47
+ `user.add_device!(params)`
48
+ * Get active devices
49
+ `user.active_devices`
50
+ * Get platform specific devices
51
+ `user.ios_devices`
52
+ `user.android_devices`
53
+ * Send push notification
54
+ `user.send_push_notification(title:, message:, **data)`
55
+ * Send silent push notification
56
+ `send_silent_push_notification(**data)`
57
+
58
+
59
+ ### Other functions
60
+
61
+ #### Send to topic:
62
+ You can send push notifications to a topic with:
63
+ ```ruby
64
+ ActsAsHocPushable::PushNotification.send_push_notification_to_topic(topic:, title: nil, message: nil, **data)
65
+ ```
66
+ or for silent notification:
67
+ ```ruby
68
+ ActsAsHocPushable::PushNotification.send_silent_push_notification_to_topic(topic:, **data)
69
+ ```
70
+ #### Send to many devices:
71
+ ```ruby
72
+ ActsAsHocPushable::PushNotification.send_push_notification(devices:, title: nil, message: nil, **data)
73
+ ```
74
+ or for silent notification:
75
+ ```ruby
76
+ ActsAsHocPushable::PushNotification.send_silent_push_notification(devices:, **data)
77
+ ```
78
+
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "acts_as_hoc_pushable/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_hoc_pushable"
8
+ spec.version = ActsAsHocPushable::VERSION
9
+ spec.authors = ["Gert Lavsen"]
10
+ spec.email = ["gert@houseofcode.io"]
11
+
12
+ spec.summary = "Add push tokens to model"
13
+ spec.description = "Add push tokens to model"
14
+ spec.homepage = "https://github.com/lavsen/acts_as_hoc_pushable"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+ spec.add_dependency "fcm"
33
+ spec.add_development_dependency "bundler", "~> 1.16.a"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acts_as_hoc_pushable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ require 'acts_as_hoc_pushable/version'
2
+ require 'acts_as_hoc_pushable/acts_as_hoc_pushable'
3
+ require 'acts_as_hoc_pushable/configuration'
4
+ require 'acts_as_hoc_pushable/push_notification'
5
+ require 'acts_as_hoc_pushable/active_record/device.rb'
6
+
7
+ module ActsAsHocPushable
8
+ LOCK = Mutex.new
9
+ class << self
10
+ def configure(config_hash=nil)
11
+ if config_hash
12
+ config_hash.each do |k,v|
13
+ configuration.send("#{k}=", v) rescue nil if configuration.respond_to?("#{k}=")
14
+ end
15
+ end
16
+
17
+ yield(configuration) if block_given?
18
+ end
19
+
20
+ def configuration
21
+ @configuration = nil unless defined?(@configuration)
22
+ @configuration || LOCK.synchronize { @configuration ||= ActsAsHocPushable::Configuration.new }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,52 @@
1
+ module ActsAsHocPushable
2
+ class Device < ActiveRecord::Base
3
+ belongs_to :parent, polymorphic: true
4
+
5
+ validates :token, :platform, :valid_at, :parent, :platform_version, :push_environment, presence: true
6
+ validates :token, uniqueness: true
7
+ validates :active, inclusion: { in: [true, false] }
8
+
9
+ before_validation :set_valid_at, on: :create
10
+ before_validation :strip_spaces_from_token, on: :create, if: :token
11
+
12
+ scope :active, -> { where(invalidated_at: nil, active: true) }
13
+ scope :ios, -> { where(platform: "ios") }
14
+ scope :android, -> { where(platform: "ios") }
15
+
16
+ default_scope { active }
17
+
18
+ def ios?
19
+ platform == 'ios'
20
+ end
21
+
22
+ def android?
23
+ platform == 'android'
24
+ end
25
+
26
+ def invalidate
27
+ update_attributes(invalidated_at: Time.current)
28
+ end
29
+
30
+ def deactivate
31
+ update_attributes(active: false, deactivated_at: Time.current)
32
+ end
33
+
34
+ def send_notification(title:, message:, **data)
35
+ ActsAsHocPushable::PushNotification.send_push_notification(devices: [self], title: title, message: message, **data)
36
+ end
37
+
38
+ def send_silent_notification(**data)
39
+ ActsAsHocPushable::PushNotification.send_silent_push_notification(devices: [self], **data)
40
+ end
41
+
42
+ private
43
+
44
+ def set_valid_at
45
+ self.valid_at = Time.current
46
+ end
47
+
48
+ def strip_spaces_from_token
49
+ self.token = token.delete(' ')
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,59 @@
1
+ module ActsAsHocPushable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ end
6
+
7
+ module ClassMethods
8
+ def acts_as_hoc_pushable(_options = {})
9
+ has_many :devices, as: :parent, class_name: 'ActsAsHocPushable::Device', dependent: :destroy
10
+ end
11
+ end
12
+
13
+ def active_devices
14
+ devices.active
15
+ end
16
+
17
+ def ios_devices
18
+ devices.ios
19
+ end
20
+
21
+ def android_devices
22
+ devices.android
23
+ end
24
+
25
+
26
+ def add_device(token:, platform:, platform_version:, push_environment:)
27
+ device = devices.new(token: token, platform: platform, platform_version: platform_version, push_environment: push_environment)
28
+ return device if device.save
29
+ device.errors.each do |attribute, message|
30
+ errors.add(:devices, "#{attribute} #{message}")
31
+ end
32
+ nil
33
+ end
34
+
35
+ def add_device(device_params)
36
+ device = devices.new(device_params)
37
+ return device if device.save
38
+ device.errors.each do |attribute, message|
39
+ errors.add(:devices, "#{attribute} #{message}")
40
+ end
41
+ nil
42
+ end
43
+
44
+ def add_device!(device_params)
45
+ device = devices.new(device_params)
46
+ device.save!
47
+ device
48
+ end
49
+
50
+ def send_push_notification(title:, message:, **data)
51
+ ActsAsHocPushable::PushNotification.send_push_notification(devices: devices, title: title, message: message, **data)
52
+ end
53
+
54
+ def send_silent_push_notification(**data)
55
+ ActsAsHocPushable::PushNotification.send_silent_push_notification(devices: devices, **data)
56
+ end
57
+
58
+ end
59
+ ActiveRecord::Base.send :include, ActsAsHocPushable
@@ -0,0 +1,7 @@
1
+ module ActsAsHocPushable
2
+ class Configuration
3
+ attr_accessor :firebase_key, :debug_firebase_response
4
+ def initialize
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,92 @@
1
+ require 'json'
2
+ require 'fcm'
3
+ module ActsAsHocPushable
4
+ class PushNotification
5
+
6
+ attr_accessor :devices, :title, :message, :data
7
+
8
+ def initialize(devices:, title:, message:, **data)
9
+ @devices = devices
10
+ @title = title
11
+ @message = message
12
+ @data = data
13
+ end
14
+
15
+ def self.send_push_notification_to_topic(topic:, title: nil, message: nil, **data)
16
+ new(devices: nil, title: title, message: message, **data).perform_topic(topic: topic)
17
+ end
18
+
19
+ def self.send_silent_push_notification_to_topic(topic:, **data)
20
+ silent_data = data.merge({ content_avaiable: true })
21
+ new(devices: nil, title: nil, message: nil, **silent_data).perform_topic(topic: topic)
22
+ end
23
+
24
+ def self.send_push_notification(devices:, title: nil, message: nil, **data)
25
+ new(devices: devices, title: title, message: message, **data).perform
26
+ end
27
+
28
+ def self.send_silent_push_notification(devices:, **data)
29
+ silent_data = data.merge({ content_avaiable: true })
30
+ new(devices: devices, title: nil, message: nil, **silent_data).perform
31
+ end
32
+
33
+ def perform_topic(topic:)
34
+ response = client.send_to_topic(topic, push_options)
35
+ handle_response(response)
36
+ end
37
+
38
+ def perform
39
+ tokens = @devices.map {|d| d.token }
40
+ response = client.send(tokens, push_options)
41
+ handle_response(response)
42
+ end
43
+
44
+ protected
45
+
46
+ def title
47
+ @title ||= ""
48
+ end
49
+
50
+ def push_options(options = {})
51
+ options = {
52
+ priority: 'high',
53
+ data: data
54
+ }.merge(options)
55
+ options[:notification] = { body: @message, title: title } unless @message.nil?
56
+ options
57
+ end
58
+
59
+ private
60
+
61
+ def client
62
+ FCM.new(ActsAsHocPushable.configuration.firebase_key)
63
+ end
64
+
65
+
66
+
67
+ # Handles the response from firebase. Invalidates failed tokens
68
+ def handle_response(response)
69
+ Rails.logger.info "Raw response from fcm: #{response}" if (ActsAsHocPushable.configuration.debug_firebase_response ||= false)
70
+ body = parse_json(response[:body])
71
+ return if body.nil?
72
+ Rails.logger.info "Parsed response from fcm: #{body}" if (ActsAsHocPushable.configuration.debug_firebase_response ||= false)
73
+ failure = body['failure'].to_i
74
+ if failure > 0
75
+ i = 1
76
+ results = body['results']
77
+ results.each do |r|
78
+ invalidate_token_at_pos(i) if r.has_key? 'error'
79
+ i += 1
80
+ end
81
+ end
82
+ end
83
+
84
+ def parse_json(json)
85
+ JSON.parse(json) rescue nil if json && json.length >= 2
86
+ end
87
+
88
+ def invalidate_token_at_pos(pos)
89
+ @devices.first(pos).last.invalidate if pos <= @devices.count
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsHocPushable
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module ActsAsHocPushable
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def copy_initializer_file
11
+ template "initializer.rb", "config/initializers/acts_as_hoc_pushable.rb"
12
+ end
13
+
14
+ def copy_migration_files
15
+ migration_template "migration/create_devices.rb", "db/migrate/create_devices.rb"
16
+ end
17
+
18
+ def self.next_migration_number(dirname)
19
+ if ActiveRecord::Base.timestamped_migrations
20
+ sleep 1 # make sure each time we get a different timestamp
21
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
22
+ else
23
+ "%.3d" % (current_migration_number(dirname) + 1)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ ActsAsHocPushable.configure do |config|
2
+ config.firebase_key = "replace_me_with_your_firebase_api_key"
3
+ config.debug_firebase_response = false
4
+ end
@@ -0,0 +1,21 @@
1
+ class CreateDevices < ActiveRecord::Migration
2
+ def change
3
+ create_table :devices do |t|
4
+ t.string :token
5
+ t.integer :parent_id
6
+ t.string :parent_type
7
+ t.string :platform
8
+ t.boolean :active, default: true
9
+ t.datetime :valid_at
10
+ t.datetime :invalidated_at
11
+ t.datetime :deleted_at
12
+ t.string :platform_version
13
+ t.string :platform_type
14
+ t.string :push_environment
15
+ t.datetime :deactivated_at
16
+ t.timestamps
17
+ end
18
+
19
+ add_index :devices, :token, unique: true
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_hoc_pushable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gert Lavsen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fcm
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.16.a
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.16.a
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Add push tokens to model
56
+ email:
57
+ - gert@houseofcode.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - acts_as_hoc_pushable.gemspec
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/acts_as_hoc_pushable.rb
71
+ - lib/acts_as_hoc_pushable/active_record/device.rb
72
+ - lib/acts_as_hoc_pushable/acts_as_hoc_pushable.rb
73
+ - lib/acts_as_hoc_pushable/configuration.rb
74
+ - lib/acts_as_hoc_pushable/push_notification.rb
75
+ - lib/acts_as_hoc_pushable/version.rb
76
+ - lib/generators/acts_as_hoc_pushable/install_generator.rb
77
+ - lib/generators/acts_as_hoc_pushable/templates/initializer.rb
78
+ - lib/generators/acts_as_hoc_pushable/templates/migration/create_devices.rb
79
+ homepage: https://github.com/lavsen/acts_as_hoc_pushable
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.13
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Add push tokens to model
103
+ test_files: []