action_push 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
+ SHA256:
3
+ metadata.gz: ff1cc117f681c6bda61285aa5107cf481178e282a8e0feed7fa38b43a75ec3ab
4
+ data.tar.gz: dc6057e306c7dbcf688aade09d3e23cc60da3b06d4ed6be3c2493c2ef8a480d1
5
+ SHA512:
6
+ metadata.gz: 948ab35dfc5cc18ed73281b071f4b3ca1d96f57be63e839f5554156e2ffbd4e5981c5ba17f0bba69efa8e53f552cac83e3b5b8a1d7cc17f72fd1257ba6679862
7
+ data.tar.gz: 7771253022b54cb8bb21c76a7d651fef27fed3bc7ec12536871a1f88074ecfa605dce9fde1071e68c6b06b36edc2150687efd9e0280d2b4f4b66b0cc55ecb4b6
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /*.gem
10
+
11
+ # configs
12
+ /gorush.yml
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,124 @@
1
+ require:
2
+ - rubocop-performance
3
+
4
+ AllCops:
5
+ AutoCorrect: false
6
+ Exclude:
7
+ - '*.gemspec'
8
+ - 'bin/*'
9
+ - 'spec/**/*'
10
+ - 'vendor/**/*'
11
+ TargetRubyVersion: 2.6.5
12
+
13
+ Bundler/OrderedGems:
14
+ Enabled: false
15
+
16
+ # ============================== Documentation ======================
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ # ============================== Metrics ============================
21
+ Metrics/ClassLength:
22
+ Max: 180
23
+ Metrics/BlockLength:
24
+ Enabled: true
25
+ Metrics/MethodLength:
26
+ Max: 25
27
+ Metrics/AbcSize:
28
+ Max: 40
29
+
30
+ # ============================== Naming =============================
31
+ Naming/PredicateName:
32
+ ForbiddenPrefixes:
33
+ - is_
34
+ Naming/FileName:
35
+ Enabled: true
36
+ Exclude:
37
+ - 'Gemfile'
38
+ Naming/MethodParameterName:
39
+ Enabled: false
40
+ Naming/AccessorMethodName:
41
+ Enabled: false
42
+
43
+ # ============================== Layout =============================
44
+ Layout/HashAlignment:
45
+ EnforcedHashRocketStyle: key
46
+ EnforcedColonStyle: key
47
+ Layout/ParameterAlignment:
48
+ EnforcedStyle: with_fixed_indentation
49
+ Layout/CaseIndentation:
50
+ EnforcedStyle: case
51
+ IndentOneStep: false
52
+ Layout/MultilineMethodCallIndentation:
53
+ Enabled: true
54
+ EnforcedStyle: indented
55
+ Layout/SpaceBeforeBlockBraces:
56
+ EnforcedStyle: space
57
+ EnforcedStyleForEmptyBraces: space
58
+ Layout/EmptyLines:
59
+ Enabled: true
60
+ Layout/EmptyLineAfterMagicComment:
61
+ Enabled: false
62
+ Layout/EmptyLinesAroundBlockBody:
63
+ Enabled: true
64
+ Layout/EndAlignment:
65
+ EnforcedStyleAlignWith: variable
66
+ Layout/FirstHashElementIndentation:
67
+ EnforcedStyle: consistent
68
+ Layout/HeredocIndentation:
69
+ Enabled: false
70
+ Layout/RescueEnsureAlignment:
71
+ Enabled: false
72
+ Layout/LineLength:
73
+ Max: 140
74
+ # ============================== Style ==============================
75
+ Style/RescueModifier:
76
+ Enabled: true
77
+ Style/PercentLiteralDelimiters:
78
+ PreferredDelimiters:
79
+ default: '[]'
80
+ '%i': '[]'
81
+ '%w': '[]'
82
+ Exclude:
83
+ - 'config/routes.rb'
84
+ Style/StringLiterals:
85
+ Enabled: true
86
+ Style/AsciiComments:
87
+ Enabled: false
88
+ Style/Copyright:
89
+ Enabled: false
90
+ Style/SafeNavigation:
91
+ Enabled: false
92
+ Style/Lambda:
93
+ Enabled: false
94
+ Style/Alias:
95
+ Enabled: true
96
+ EnforcedStyle: prefer_alias_method
97
+ Style/ClassAndModuleChildren:
98
+ Enabled: true
99
+ EnforcedStyle: nested
100
+ Style/TrailingCommaInArrayLiteral:
101
+ Enabled: true
102
+ EnforcedStyleForMultiline: no_comma
103
+ Style/RescueStandardError:
104
+ Enabled: true
105
+ EnforcedStyle: explicit
106
+ Style/InverseMethods:
107
+ AutoCorrect: false
108
+ Enabled: true
109
+ Style/IfUnlessModifier:
110
+ Enabled: false
111
+ Style/SpecialGlobalVars:
112
+ Enabled: false
113
+ Style/BlockComments:
114
+ Enabled: false
115
+ Style/GuardClause:
116
+ Enabled: false
117
+ Style/TrailingCommaInHashLiteral:
118
+ Enabled: false
119
+
120
+ # ============================== Lint ==============================
121
+ Lint/DuplicateMethods:
122
+ Enabled: false
123
+ Lint/AmbiguousOperator:
124
+ Enabled: false
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.6.5
5
+
6
+ before_install:
7
+ - bundle install
8
+
9
+ script:
10
+ - bundle exec rubocop
11
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in action_push.gemspec
8
+ gemspec
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ action_push (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ ast (2.4.0)
12
+ coderay (1.1.2)
13
+ concurrent-ruby (1.1.5)
14
+ crack (0.4.3)
15
+ safe_yaml (~> 1.0.0)
16
+ diff-lcs (1.3)
17
+ hashdiff (1.0.0)
18
+ i18n (1.8.2)
19
+ concurrent-ruby (~> 1.0)
20
+ jaro_winkler (1.5.4)
21
+ method_source (0.9.2)
22
+ parallel (1.19.1)
23
+ parser (2.7.0.2)
24
+ ast (~> 2.4.0)
25
+ pry (0.12.2)
26
+ coderay (~> 1.1.0)
27
+ method_source (~> 0.9.0)
28
+ public_suffix (4.0.3)
29
+ rainbow (3.0.0)
30
+ rake (10.5.0)
31
+ rspec (3.9.0)
32
+ rspec-core (~> 3.9.0)
33
+ rspec-expectations (~> 3.9.0)
34
+ rspec-mocks (~> 3.9.0)
35
+ rspec-core (3.9.1)
36
+ rspec-support (~> 3.9.1)
37
+ rspec-expectations (3.9.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.9.0)
40
+ rspec-mocks (3.9.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-support (3.9.2)
44
+ rubocop (0.79.0)
45
+ jaro_winkler (~> 1.5.1)
46
+ parallel (~> 1.10)
47
+ parser (>= 2.7.0.1)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 1.7)
51
+ rubocop-performance (1.5.2)
52
+ rubocop (>= 0.71.0)
53
+ ruby-progressbar (1.10.1)
54
+ safe_yaml (1.0.5)
55
+ unicode-display_width (1.6.1)
56
+ webmock (3.8.0)
57
+ addressable (>= 2.3.6)
58
+ crack (>= 0.3.2)
59
+ hashdiff (>= 0.4.0, < 2.0.0)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ action_push!
66
+ bundler (~> 1.17)
67
+ i18n
68
+ pry
69
+ rake (~> 10.0)
70
+ rspec (~> 3.0)
71
+ rubocop
72
+ rubocop-performance
73
+ webmock
74
+
75
+ BUNDLED WITH
76
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Aliaksandr Shylau
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,12 @@
1
+ .PHONY: help
2
+
3
+ help:
4
+ @echo 'Available targets:'
5
+ @echo ' make dockerize OR make ARGS="--build" dockerize'
6
+ @echo ' make release'
7
+
8
+ dockerize:
9
+ docker-compose up ${ARGS}
10
+
11
+ release:
12
+ bin/release.sh
@@ -0,0 +1,180 @@
1
+ ```ruby
2
+ class ApplicationPush < ActionPush::Base
3
+ default :ios do |ios|
4
+ ios.thread_id = 'fuck'
5
+ ios.badge = 10
6
+ end
7
+ end
8
+ ```
9
+
10
+ ```ruby
11
+ class UserPush < ApplicationPush
12
+ default :ios do |ios|
13
+ ios.alert do |a|
14
+ a.launch_image = 'user.png'
15
+ end
16
+ end
17
+
18
+ def welcome
19
+ # envelope#title and #body set corresponding
20
+ # attributes for all registered providers like ios and android
21
+ envelope.title = 'hello'
22
+ envelope.body = 'world'
23
+ envelope.payload = { user_id: 1 }
24
+
25
+ envelope.for(:ios) do |ios|
26
+ ios.thread_id = 123
27
+ end
28
+
29
+ # push method sets #scheduled = true to the message,
30
+ # so this pushes will be delivered when you call a
31
+ # class method: `UserPush.welcome`
32
+ push :ios, token: 'foo'
33
+ push :android, token: 'bar'
34
+ end
35
+
36
+
37
+ def buy
38
+ push :ios, token: 'foo' do |ios|
39
+ # translations are available under the key:
40
+ # en.user_push.buy.title
41
+ #
42
+ # You can set I18n scope by redefining i18n_scope method:
43
+ #
44
+ # def i18n_scope
45
+ # "self.class.to_s.underscore}.#{action_name}"
46
+ # end
47
+ ios.title = t('.title')
48
+
49
+ # you can redefine scheduling in a block
50
+ ios.scheduled = false
51
+ end
52
+
53
+ push :android do |android|
54
+ android.token = 'foo'
55
+ android.title = t('.title')
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+ ## Using with Sidekiq
62
+
63
+ ```ruby
64
+ # enable delay extension at `config/initializers/sidekiq.rb`
65
+ Sidekiq::Extensions.enable_delay!
66
+
67
+ @user = User.last
68
+ UserPush.delay_for(4.seconds).welcome(@user.id)
69
+ ```
70
+
71
+ ## Configuration
72
+
73
+ ```ruby
74
+ gorush = ActionPush::Delivery::Gorush.new(
75
+ url: 'http://localhost:8088/api/push',
76
+ topic: 'com.app.id',
77
+ logger: STDOUT
78
+ )
79
+
80
+ ActionPush::Base.register_delivery_method :ios, ->(push) do
81
+ gorush.send_to_apple(push)
82
+ end
83
+ ```
84
+
85
+ ## Testing
86
+
87
+ ```ruby
88
+ # spec/support/action_push.rb
89
+
90
+ ActionPush::Base.register_delivery_method :ios, ActionPush::Delivery::Memory
91
+
92
+ RSpec.configure do |config|
93
+ config.before(:each) do
94
+ ActionPush::Delivery::Memory.clear
95
+ end
96
+ end
97
+ ```
98
+
99
+ ```ruby
100
+ # app/models/user.rb
101
+
102
+ class User < ApplicationRecord
103
+ def confirm
104
+ update!(confirmed_at: Time.current)
105
+ UserPush.welcome(id)
106
+ end
107
+ end
108
+ ````
109
+
110
+ ```ruby
111
+ # app/pushes/user_push.rb
112
+
113
+ class UserPush < ApplicationPush
114
+ def welcome(id)
115
+ user = User.find(id)
116
+
117
+ envelope.title = 'Welcome push'
118
+ envelope.body = 'Glad to see you'
119
+
120
+ push :ios, token: user.ios_push_token
121
+ end
122
+ end
123
+ ```
124
+
125
+ ```ruby
126
+ # spec/models/user_spec.rb
127
+
128
+ RSpec.describe User do
129
+ let(:user) do
130
+ create :user
131
+ end
132
+
133
+ it 'sends a push' do
134
+ expect { user.confirm }.to change { ActionPush::Delivery::Memory.size }.by(1)
135
+ expect(ActionPush::Delivery::Memory.last).to have_attributes(title: 'Welcome push', body: 'Glad to see you', token: user.ios_push_token)
136
+ end
137
+ end
138
+ ```
139
+
140
+ ## Реализация провайдера
141
+
142
+ Можно легко реализовать функционал нового провайдера для, скажем, отправки
143
+ уведомлений в браузер пользователя через WebSocket.
144
+
145
+ В этом примере провайдером будет выступать [nchan](https://github.com/slact/nchan)
146
+
147
+ ```ruby
148
+ class WebsocketMessage < ActionPush::Provider::Base
149
+ attr_accessor :chanel, :payload
150
+ end
151
+ ```
152
+
153
+ ```ruby
154
+ class ApplicationPush < ActionPush::Base
155
+ register_default :socket, WebsocketMessage.new
156
+ end
157
+ ```
158
+
159
+ ```ruby
160
+ class FollowRequestPush < ApplicationPush
161
+ def notify(user_id)
162
+ user = User.find(user_id)
163
+ push :socket, chanel: "pub/#{user.id}", payload: { title: t('.title') }
164
+ end
165
+ end
166
+ ```
167
+
168
+ ```ruby
169
+ # config/initializers/action_push
170
+
171
+ ApplicationPush.register_delivery_method :socket, ->(push) do
172
+ `curl http://nchan:8080/#{push.chanel} -X POST --data='#{push.payload.to_json}'`
173
+ end
174
+ ```
175
+
176
+ Теперь вы можете слать уведомления прямо в браузер пользователя:
177
+
178
+ ```ruby
179
+ FollowRequestPush.notify(1)
180
+ ```