hey-you 1.2.0 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d00b4135e266174d5c8a8a1090a34acedac8b2fbdc6c82c0dd19d9e30072ef3
4
- data.tar.gz: ae481c0244a148df0620a2507b6eacb94dfdaa72f4cbc6ddeccd909ce2c6d914
3
+ metadata.gz: 9ad2c28532b80d7b3efb44590b9a9281169cd09240062d99a94a8c26498e9d8e
4
+ data.tar.gz: 837c96af5d9c6d2a220d8384a6b77c1209f2358247d7a16cc5e33165045eb461
5
5
  SHA512:
6
- metadata.gz: 40ce68c45090a678097d965c98603eeec09fbf87f2946f7f3602137c92a46636320e89a88c329b17a146b4eb0543be6e716d7ccbac2e4036dba0fe7ef476f6ea
7
- data.tar.gz: 538ceba1fd584b76ef57bad07583474d8ad17f52375f0521a467fd30208f5996ee76d9442f7927ae5ca8893ca41ec52e82c981df16a999e34e70aeef57c966db
6
+ metadata.gz: dbd3ac7e73206eb9e3410fb51acb84f2c25fb8266570d047d2483ec12b11907b7629d10f19407b797abbc1c36cd918a3610b2b5f44c082f2d5d02fe00133b32d
7
+ data.tar.gz: add12e5c26ddf9a8dcb8f75592d115f095168eb55a6486fdec1c79abe9a10cbabb9f50390afc614a5945856bddca9108c7742fd124e47dc6cc0bce469bd04525
@@ -1,8 +1,6 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.0
5
- - 2.4.0
6
4
  - 2.5.0
7
5
  - 2.6.0
8
6
  - 2.7.0
@@ -1,4 +1,22 @@
1
1
  # Changelog for hey-you gem
2
+ ## 1.3.1/1.3.2/1.3.3
3
+ Fixing bugs
4
+
5
+ ## 1.3.0
6
+ - Feature: `body_part` in email builder.
7
+
8
+ ### 1.2.3
9
+ - Improvement: fix ruby 2.7 warnings
10
+ - Fix: fix `NoMethodError` in `sender.rb` when channel must be ignored by `if`
11
+
12
+ ### 1.2.2
13
+ - Improvement: `if` condition for receiver (if condition `false` - sending will be skipped).
14
+ - Improvement: `force` option - send message independent on `if` condition.
15
+
16
+
17
+ ### 1.2.1
18
+ - Improvement: Builder will not make channel builder if it skipped by only option
2
19
 
3
20
  ### 1.2.0
4
- - Feature: data source extensions (check readme for more information)
21
+ - Feature: data source extensions (check readme for more information).
22
+ __Attention__: You should rewrite your configuration for use yaml data source!
data/README.md CHANGED
@@ -20,7 +20,7 @@ him easy.
20
20
 
21
21
 
22
22
  ## Requirements
23
- * Ruby 2.3.0 min (rspec works with ruby 2.3.3, 2.4.2, 2.5.3)
23
+ * Ruby 2.5.0 min (rspec works with ruby 2.5.0, 2.6.0, 2.7.0)
24
24
  * FCM - Gem send push notification using [fcm gem](https://github.com/spacialdb/fcm).
25
25
  You need *fcm server key* to successful configure push notifications.
26
26
 
@@ -52,10 +52,12 @@ First, you must configure HeyYou. Example:
52
52
  #### Required settings
53
53
  Options for gem base work.
54
54
  ##### Data Source
55
- * __config.data_source.source_class__ - Class implemented instance method `load_collections` returning hash (by default HeyYou::DataSource::Yaml)
55
+ * __config.data_source.source_class__ - Class implemented instance method `load_collections` returning hash (by default `HeyYou::DataSource::Yaml`)
56
56
  * __config.data_source.options__ - Arguments for source_class. This options will be passed to init `source_class`
57
57
  OR
58
58
  * __config.data_source.source_instance__ - Instance of source class implemented `load_collections`
59
+
60
+ Read more about data source in [data source](#data-source-1).d
59
61
  ##### Push
60
62
  * __config.push.fcm_token__ - Required setting for push channel. You can not send
61
63
  push messages if setting was not set. You should set it to equal your fcm server key.
@@ -65,8 +67,6 @@ push messages if setting was not set. You should set it to equal your fcm server
65
67
  #### Optional settings
66
68
  Additional options for configure your notifications.
67
69
  #### Base
68
- * __config.env_collection_file__ - File contained all your notifications texts for
69
- environment. You can set it like `notifications.#{ENV['APP_ENV]}.yml`
70
70
  * __config.splitter__ - Chars for split notification keys for
71
71
  builder. Default: `.`
72
72
  * __config.registered_channels__ - Avialable channels for your
@@ -163,8 +163,8 @@ fetching values required to send notification. For push channel
163
163
  expected that proc will return receiver's fcm registration id. For
164
164
  email expected that proc will return receiver's email address.
165
165
 
166
- You can pass options for receiver channels. You must pass proc with receive_data to `:subject` key and options
167
- pass to `:options` key:
166
+ You can pass options and sending condition for receiver channels. You must pass proc with receive_data to `:subject` key and options
167
+ pass to `:options` key. `if` key should be passed for sending condition:
168
168
 
169
169
  ```ruby
170
170
  class User < Model
@@ -172,7 +172,11 @@ class User < Model
172
172
 
173
173
  receive(
174
174
  push: -> { push_token.value },
175
- email: { subject: -> { email }, options: { mailer_class: UserMailer, mailer_method: :notify! } }
175
+ email: {
176
+ subject: -> { email },
177
+ if: -> { email_notifications? },
178
+ options: { mailer_class: UserMailer, mailer_method: :notify! }
179
+ }
176
180
  )
177
181
  end
178
182
  ```
@@ -189,6 +193,15 @@ Last command will fetch notifications credentials for user instance
189
193
  and will try to send SMS, Push and Email for it. What argument we pass
190
194
  for method? This is string key for builder. Read next to understand it.
191
195
 
196
+ Sometimes you need send notification independent on user's notification settings. For this case you can use
197
+ `force` option in `#send_notification`:
198
+ ```ruby
199
+ user = User.find(1)
200
+ user.settings.update!(email_notifications: false)
201
+ user.send_notification('for_users.hello') #=> will not send notification
202
+ user.send_notification('for_users.hello', force: true) #=> will send notification
203
+ ```
204
+
192
205
  ### Build your notification
193
206
  HeyYou Notification Builder - good system for store your notifications in one place.
194
207
  By default you need create yml file with follow format:
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_runtime_dependency "mail", '~> 2.7'
27
27
  spec.add_runtime_dependency "i18n", '~> 1.0'
28
28
 
29
- spec.add_development_dependency "rake", '~> 10.5'
29
+ spec.add_development_dependency "rake", '~> 13.0'
30
30
  spec.add_development_dependency "rspec", '~> 3.7'
31
31
  spec.add_development_dependency "webmock", '~> 3.4'
32
32
  spec.add_development_dependency "ffaker", '~> 2.9'
@@ -13,11 +13,14 @@ module HeyYou
13
13
  # Define methods for each registered channel. After initialize you can use
14
14
  # `instance.<ch_name>`. It will be return instance of HeyYou::Builder::<YOUR_CHANNEL_NAME>
15
15
  #
16
+ # Skip builder for excluded channels (not included in `only` option)
17
+ #
18
+ # @param [String] key - notification key for fetching notification data from collection
16
19
  def initialize(key, **options)
17
20
  @data = fetch_from_collection_by_key(key, options[:locale])
18
21
  @options = options
19
22
  config.registered_channels.each do |ch|
20
- init_channel_builder(ch, key)
23
+ init_channel_builder(ch, key) if channel_allowed_by_only?(ch, options[:only])
21
24
  end
22
25
  end
23
26
 
@@ -35,7 +38,7 @@ module HeyYou
35
38
  end
36
39
 
37
40
  ch_builder =
38
- HeyYou::Builder.const_get("#{ch.downcase.capitalize}").new(data, key, options)
41
+ HeyYou::Builder.const_get("#{ch.downcase.capitalize}").new(data[ch.to_s], key, **options)
39
42
  instance_variable_set("@#{ch}".to_sym, ch_builder)
40
43
 
41
44
  define_ch_method(ch)
@@ -61,6 +64,12 @@ module HeyYou
61
64
  raise DataNotFound, "collection data not found for `#{keys.join(config.splitter)}`"
62
65
  end
63
66
 
67
+ def channel_allowed_by_only?(ch, only)
68
+ return true unless only
69
+ return only.map(&:to_sym).include?(ch.to_sym) if only.is_a?(Array)
70
+ only.to_sym == ch.to_sym
71
+ end
72
+
64
73
  class UnknownLocale < StandardError; end
65
74
  class DataNotFound < StandardError; end
66
75
  class RequiredChannelNotFound < StandardError; end
@@ -23,10 +23,8 @@ module HeyYou
23
23
  raise InterpolationError, "Failed build notification string `#{notification_string}`: #{err.message}"
24
24
  end
25
25
 
26
- def ch_data
27
- data.fetch(current_builder_name)
28
- end
29
26
 
27
+ alias ch_data data
30
28
  alias channel_data ch_data
31
29
 
32
30
  def ch_options
@@ -3,19 +3,36 @@ require_relative '_base'
3
3
  module HeyYou
4
4
  class Builder
5
5
  class Email < Base
6
- attr_reader :subject, :body, :layout, :mailer_class, :mailer_method, :delivery_method
6
+ attr_reader :subject, :body, :layout, :mailer_class, :mailer_method, :delivery_method, :body_parts
7
7
 
8
8
  def build
9
9
  @mailer_class = ch_data.fetch('mailer_class', nil)
10
10
  @mailer_method = ch_data.fetch('mailer_method', nil)
11
11
  @delivery_method = ch_data.fetch('delivery_method', nil)
12
- @body = interpolate(ch_data.fetch('body'), options)
12
+ @body = interpolate(ch_data['body'], options) if ch_data['body']
13
+ @body_parts = interpolate_each(ch_data.fetch('body_parts', nil), options)
13
14
  @subject = interpolate(ch_data.fetch('subject'), options)
14
15
  end
15
16
 
16
17
  def to_hash
17
- { body: body, subject: subject }
18
+ { body: body, subject: subject, body_parts: body_parts }
19
+ end
20
+
21
+ private
22
+
23
+ def interpolate_each(notification_hash, options)
24
+ return notification_hash unless notification_hash.is_a?(Hash)
25
+
26
+ notification_hash.each do |k, v|
27
+ next interpolate_each(v, options) if v.is_a?(Hash)
28
+
29
+ begin
30
+ notification_hash[k] = v % options
31
+ rescue KeyError
32
+ raise InterpolationError, "Failed build notification string `#{v}`: #{err.message}"
33
+ end
34
+ end
18
35
  end
19
36
  end
20
37
  end
21
- end
38
+ end
@@ -7,13 +7,13 @@ module HeyYou
7
7
  class << self
8
8
  def send!(builder, to:, **options)
9
9
  method = config.email.use_default_mailing ? :send_via_mail : :send_via_custom_class
10
- public_send(method, builder, to, options)
10
+ public_send(method, builder, to, **options)
11
11
  end
12
12
 
13
13
  # Send email via custom class instance.
14
14
  def send_via_custom_class(builder, to, **options)
15
- mailer = mailer_class_from_builder(builder, options)
16
- mailer_method = mailer_method_from_builder(mailer, builder, options)
15
+ mailer = mailer_class_from_builder(builder, **options)
16
+ mailer_method = mailer_method_from_builder(mailer, builder, **options)
17
17
  delivery_method = options[:delivery_method] ||
18
18
  builder.email.delivery_method ||
19
19
  config.email.default_delivery_method
@@ -22,7 +22,7 @@ module HeyYou
22
22
  raise InvalidDataSourceError, 'You must pass `config.data_source.source_class` in configuration.'
23
23
  end
24
24
 
25
- source_class.new(options).load_collections
25
+ source_class.new(**options).load_collections
26
26
  rescue ArgumentError => err
27
27
  problem_fields =
28
28
  err.message.gsub(/missing keyword(.?):\s/, '').split(', ').map { |f| "`#{f}`" }.join(', ')
@@ -74,9 +74,11 @@ module HeyYou
74
74
  if receiver_data[ch].is_a?(Hash)
75
75
  me = self
76
76
  self.send(:define_method, "#{ch}_ch_receive_info", receiver_data[ch].fetch(:subject))
77
+ self.send(:define_method, "#{ch}_ch_receive_condition", receiver_data[ch].fetch(:if, -> { true }))
77
78
  self.send(:define_method, "#{ch}_ch_receive_options", -> { me.receiver_data[ch].fetch(:options, {}) })
78
79
  else
79
80
  self.send(:define_method, "#{ch}_ch_receive_info", receiver_data[ch])
81
+ self.send(:define_method, "#{ch}_ch_receive_condition", -> { true })
80
82
  self.send(:define_method, "#{ch}_ch_receive_options", -> { {} })
81
83
  end
82
84
  end
@@ -15,6 +15,7 @@ module HeyYou
15
15
  # @input notification_key [String] - key for notification builder
16
16
  # @input options [Hash]
17
17
  # @option only [String/Array[String]] - whitelist for using channels
18
+ # @option force [Boolean] - ignore `if` for receiver
18
19
  #
19
20
  def send_to(receiver, notification_key, **options)
20
21
  unless receiver_valid?(receiver)
@@ -29,24 +30,28 @@ module HeyYou
29
30
  def send!(notification_key, receiver, **options)
30
31
  to_hash = {}
31
32
  receiver.class.receiver_channels.each do |ch|
33
+ if !options[:force] && !receiver.public_send("#{ch}_ch_receive_condition")
34
+ next
35
+ end
36
+
32
37
  to_hash[ch] = {
33
38
  # Fetch receiver's info for sending: phone_number, email, etc
34
39
  subject: receiver.public_send("#{ch}_ch_receive_info"),
35
40
  # Fetch receiver's options like :mailer_class
36
- options: receiver.public_send("#{ch}_ch_receive_options") || {}
41
+ options: receiver.public_send("#{ch}_ch_receive_options").merge(options) || {}
37
42
  }
38
43
  end
39
44
 
40
- send_to_receive_info(notification_key, to_hash, options)
45
+ send_to_receive_info(notification_key, to_hash, **options)
41
46
  end
42
47
 
43
48
  def send_to_receive_info(notification_key, receive_info, **options)
44
- builder = Builder.new(notification_key, options)
49
+ builder = Builder.new(notification_key, **options)
45
50
  response = {}
46
51
  config.registered_channels.each do |ch|
47
- if channel_allowed?(ch, receive_info, builder, options) && builder.respond_to?(ch) && builder.public_send(ch)
52
+ if channel_allowed?(ch, receive_info, builder, **options) && builder.respond_to?(ch) && builder.public_send(ch)
48
53
  config.log(
49
- "Send #{ch}-message to #{receive_info[ch][:subject]} with data: #{builder.public_send(ch).data}" \
54
+ "Send #{ch}-message to `#{receive_info[ch][:subject]}` with data: #{builder.public_send(ch).data}" \
50
55
  " and options: #{receive_info[ch][:options]}"
51
56
  )
52
57
  receive_options = receive_info[ch].fetch(:options, {}) || {}
@@ -54,7 +59,7 @@ module HeyYou
54
59
  builder, to: receive_info[ch][:subject], **receive_options
55
60
  )
56
61
  else
57
- config.log("Channel #{ch} not allowed.")
62
+ config.log("Channel #{ch} not allowed or sending condition doesn't return truthy result.")
58
63
  end
59
64
  end
60
65
  response
@@ -63,7 +68,10 @@ module HeyYou
63
68
  private
64
69
 
65
70
  def channel_allowed?(ch, to, builder, **options)
66
- condition = to[ch].is_a?(Hash) ? to[ch.to_sym][:subject] || to[ch.to_s][:subject] : to[ch.to_sym] || to[ch.to_s]
71
+ data_in_subject = to[ch.to_sym]&.fetch(:subject, nil) || to[ch.to_s]&.fetch(:subject, nil)
72
+ data_in_core = to[ch.to_sym] || to[ch.to_s]
73
+
74
+ condition = to[ch].is_a?(Hash) ? data_in_subject : data_in_core
67
75
  return false unless condition
68
76
  channel_allowed_by_only?(ch, options[:only]) && !builder.send(ch).nil?
69
77
  end
@@ -81,4 +89,4 @@ module HeyYou
81
89
 
82
90
  class NotRegisteredReceiver < StandardError; end
83
91
  end
84
- end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module HeyYou
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hey-you
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Nesterov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-15 00:00:00.000000000 Z
11
+ date: 2020-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fcm
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '10.5'
61
+ version: '13.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '10.5'
68
+ version: '13.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -166,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0'
168
168
  requirements: []
169
- rubygems_version: 3.0.6
169
+ rubygems_version: 3.1.2
170
170
  signing_key:
171
171
  specification_version: 4
172
172
  summary: Send multichannel notification with one command.