hey-you 0.1.7 → 1.2.1

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: 679472ed713fc109350ef9fd2b48880822edf62bf6a53874eb2df9470de2b580
4
- data.tar.gz: 4d4539b92cf5cf8a080b592bcc806a9ec7d51a5f2fbd2f7a66f9e9ae24d55fda
3
+ metadata.gz: a7b3080ba368db5d653caccc37a984f7700292a54ba90204c086bdd7b95b4b6d
4
+ data.tar.gz: c2f589b517e59fdda778d6b9f99074a7c2fc932bbf97052a395f1b3491260d74
5
5
  SHA512:
6
- metadata.gz: 8f60580d1951c4a90463f9c8c51d28854e2b98ec7bed42a5fe5ae5a87a86f7f2321377cad70f2afefc7e24e3aa09c2aa9db46c88dd519d3099bc360eb72516ea
7
- data.tar.gz: 06f1a99454e30a562cdf28eee7caa12d41ab133e57abe4d4fd437bc62149f33919c3dee81f8dd7045238c6c7f5df73055d95aa1711d3db04ceb421efe7ecef7e
6
+ metadata.gz: 3799695ed32d46f2aa0005346aa37d3b2288a373391900dedb575f90e5d71db4272d1d73edf3b5d5754066600bff886bc5e845271329bfead1dcc4f0208b3523
7
+ data.tar.gz: 29421836627cf3ba25e6731bca4e84830bef3f450ab5d400c85f7b80ef5a63b28c0492621e1edecb69ab2ae3506b1d178a88203ac34dec9a85a47d5171f0083a
@@ -1,10 +1,9 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.3
5
- - 2.4.2
6
- - 2.5.3
7
- - 2.6.1
4
+ - 2.5.0
5
+ - 2.6.0
6
+ - 2.7.0
8
7
  before_install: gem install bundler
9
8
  scripts:
10
9
  - bundle exec rspec
@@ -0,0 +1,8 @@
1
+ # Changelog for hey-you gem
2
+
3
+ ### 1.2.1
4
+ - Improvement: Builder will not make channel builder if it skipped by only option
5
+
6
+ ### 1.2.0
7
+ - Feature: data source extensions (check readme for more information).
8
+ __Attention__: You should rewrite your configuration for use yaml data source!
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
- # HeyYou [Alpha]
1
+ # HeyYou
2
2
  [![Build Status](https://travis-ci.com/QNester/hey-you.svg?branch=master)](https://travis-ci.com/QNester/hey-you#)
3
+ [![Gem Version](https://badge.fury.io/rb/hey-you.svg)](https://badge.fury.io/rb/hey-you)
3
4
 
4
5
  Send multichannel notifications with one command.
5
6
  Сonvenient storage of notifications texts. Create your
@@ -15,10 +16,11 @@ him easy.
15
16
  * [Send notification](#send-notification)
16
17
  * [Sender options](#sender-options)
17
18
  * [Create your own channels](#create-your-own-channels)
19
+ * [Extensions](#extensions)
18
20
 
19
21
 
20
22
  ## Requirements
21
- * 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)
22
24
  * FCM - Gem send push notification using [fcm gem](https://github.com/spacialdb/fcm).
23
25
  You need *fcm server key* to successful configure push notifications.
24
26
 
@@ -42,15 +44,20 @@ Or install it yourself as:
42
44
  First, you must configure HeyYou. Example:
43
45
  ```ruby
44
46
  HeyYou::Config.configure do
45
- config.collection_files = ['config/notifications.yml']
47
+ config.data_source.options = { collection_files: ['config/notifications.yml'] }
46
48
  config.email.from = 'noreply@example-mail.com'
47
49
  config.push.fcm_token = 'fcm_server_key'
48
50
  end
49
51
  ```
50
52
  #### Required settings
51
53
  Options for gem base work.
52
- ##### Base
53
- * __config.collection_files__ - File or files contained all your notifications texts.
54
+ ##### Data Source
55
+ * __config.data_source.source_class__ - Class implemented instance method `load_collections` returning hash (by default `HeyYou::DataSource::Yaml`)
56
+ * __config.data_source.options__ - Arguments for source_class. This options will be passed to init `source_class`
57
+ OR
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
54
61
  ##### Push
55
62
  * __config.push.fcm_token__ - Required setting for push channel. You can not send
56
63
  push messages if setting was not set. You should set it to equal your fcm server key.
@@ -60,20 +67,23 @@ push messages if setting was not set. You should set it to equal your fcm server
60
67
  #### Optional settings
61
68
  Additional options for configure your notifications.
62
69
  #### Base
63
- * __config.env_collection_file__ - File contained all your notifications texts for
64
- environment. You can set it like `notifications.#{ENV['APP_ENV]}.yml`
65
70
  * __config.splitter__ - Chars for split notification keys for
66
71
  builder. Default: `.`
67
72
  * __config.registered_channels__ - Avialable channels for your
68
73
  applications. Default: `[:push, :email]`
74
+ * __config.require_all_channels__ - Boolean. If true, when data for channel will not found in
75
+ file collection, error will be raised. Default: `false`
69
76
  * __config.localization__ - Boolean. If true, hey-you begin support I18n locales for notifications collection. Your
70
- notifications for build should be nested in `I18n.locale` key. For example:
77
+ notifications for build should be nested in `I18n.locale` key. Default: `false`. For example:
71
78
 
72
79
  ```ruby
73
80
  # config/initializers/hey-you.rb
74
81
  HeyYou::Config.configure do
75
82
  ...
76
- config.collection_files = I18n.available_locales.map { |locale| "config/notifications/#{locale}.yml" }
83
+ i18n_files = Rails.application.config.i18n.available_locales.map do |locale|
84
+ "config/notifications/#{locale}.yml"
85
+ end
86
+ config.data_source.options = { collection_files: i18n_files }
77
87
  ...
78
88
  end
79
89
  ```
@@ -123,7 +133,7 @@ Default 60
123
133
  Default 30
124
134
  ##### Email
125
135
  * __config.email.layout__ - default layout for email letters.
126
- * __config.email.default_mailing__ - use default mail sending or use custom mailer classes
136
+ * __config.email.use_default_mailing__ - use default mail sending or use custom mailer classes
127
137
  * __config.email.default_mailer_class__ - default mailer class for email notifications
128
138
  * __config.email.default_mailer_method__ - default mailer_method for mailer_class
129
139
  * __config.email.default_delivery_method__ - expects, that mailer_method will build message and delivery_method will send it.
@@ -181,7 +191,7 @@ for method? This is string key for builder. Read next to understand it.
181
191
 
182
192
  ### Build your notification
183
193
  HeyYou Notification Builder - good system for store your notifications in one place.
184
- You need create yml file with follow format:
194
+ By default you need create yml file with follow format:
185
195
  ```yaml
186
196
  # config/notifications/collection.yml
187
197
  any_key:
@@ -208,6 +218,46 @@ notification will send for all available channels for receiver:
208
218
  1) Send push via fcm
209
219
  2) Send email
210
220
 
221
+ #### Data Source
222
+ Often we need store our notification text in another data source, not int yml files. HeyYou has flexible
223
+ system for data source. All what you need - make your own provider source and pass it to config. For example:
224
+
225
+ ```ruby
226
+ class NotificationText < ApplicationRecord
227
+ def self.load_collections
228
+ # load colelctions from database to hash
229
+ # THIS METHOD MUST returns hash!
230
+ end
231
+ end
232
+ ```
233
+
234
+ ```ruby
235
+ HeyYou::Config.configure do
236
+ # NotificationText is a rails model
237
+ config.data_source.source_instance = NotificationText
238
+ end
239
+ ```
240
+
241
+ HeyYou will pull texts from database to memory with initialize your application after this configuration. Builder
242
+ will build your texts successfully.
243
+
244
+ HeyYou by default contains only two data sources:
245
+ 1. `HeyYou::DataSource::Yaml` - for store notifications collections in data. For example:
246
+ ```ruby
247
+ HeyYou::Config.configure do
248
+ config.data_source.source_class = HeyYou::DataSource::Yaml
249
+ config.data_source.options = { collection_files: ['config/notifications.yml'] }
250
+ end
251
+ ```
252
+ 2. `HeyYou::DataSource::Hash` - store notification everywhere you want and pass to HeyYou only hash
253
+ ```ruby
254
+ config.data_source.source_class = HeyYou::DataSource::Yaml
255
+ config.data_source.options = { data: { welcome: { email: { ... }, push: { ... } } } }
256
+ ```
257
+
258
+ __Pay attention__: for difference source we should pass difference options.
259
+
260
+
211
261
  ### Send notification
212
262
  Receiver not only one way to send notification. You can send it using `HeyYou::Sender`.
213
263
  Just use method `#send` for HeyYou::Sender and pass notification key and `to` options
@@ -357,6 +407,9 @@ in your channel config.
357
407
 
358
408
  Now, when you will send notification, it will be send with your channel too.
359
409
 
410
+ ## Extensions
411
+ * [Slack channel](https://github.com/QNester/hey-you-slack)
412
+ [![Gem Version](https://badge.fury.io/rb/hey-you-slack.svg)](https://badge.fury.io/rb/hey-you-slack)
360
413
 
361
414
  ## Development
362
415
 
@@ -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'
@@ -1,31 +1,51 @@
1
1
  require 'i18n'
2
- require_relative 'builder/email'
3
- require_relative 'builder/push'
2
+ require 'hey_you/helper'
3
+ require 'hey_you/builder/email'
4
+ require 'hey_you/builder/push'
4
5
 
5
6
  module HeyYou
6
7
  class Builder
7
- attr_reader :data, :options
8
+ include HeyYou::Helper
9
+
10
+ attr_reader :data, :options, :keys
8
11
 
9
12
  # Load data from collection yaml via key and interpolate variables.
10
13
  # Define methods for each registered channel. After initialize you can use
11
14
  # `instance.<ch_name>`. It will be return instance of HeyYou::Builder::<YOUR_CHANNEL_NAME>
12
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
13
19
  def initialize(key, **options)
14
20
  @data = fetch_from_collection_by_key(key, options[:locale])
15
21
  @options = options
16
22
  config.registered_channels.each do |ch|
17
- ch_builder =
18
- HeyYou::Builder.const_get("#{ch.downcase.capitalize}").new(data, key, options)
19
- instance_variable_set("@#{ch}".to_sym, ch_builder)
20
-
21
- define_ch_method(ch)
23
+ init_channel_builder(ch, key) if channel_allowed_by_only?(ch, options[:only])
22
24
  end
23
25
  end
24
26
 
25
27
  private
26
28
 
27
- def define_ch_method(ch)
28
- method_proc = -> { instance_variable_get("@#{ch}".to_sym) }
29
+ def init_channel_builder(ch, key)
30
+ if config.require_all_channels
31
+ unless data[ch.to_s]
32
+ raise RequiredChannelNotFound, "For key #{key} and channel #{ch} data not exists in collection."
33
+ end
34
+ end
35
+ unless data[ch.to_s]
36
+ define_ch_method(ch, true)
37
+ return
38
+ end
39
+
40
+ ch_builder =
41
+ HeyYou::Builder.const_get("#{ch.downcase.capitalize}").new(data, key, options)
42
+ instance_variable_set("@#{ch}".to_sym, ch_builder)
43
+
44
+ define_ch_method(ch)
45
+ end
46
+
47
+ def define_ch_method(ch, empty = false)
48
+ method_proc = empty ? -> { nil } : -> { instance_variable_get("@#{ch}".to_sym) }
29
49
  self.class.send(:define_method, ch, method_proc)
30
50
  end
31
51
 
@@ -37,15 +57,21 @@ module HeyYou
37
57
  keys << locale
38
58
  end
39
59
  keys = keys + key.to_s.split(config.splitter)
40
- keys.reduce(config.collection) do |memo, nested_key|
60
+ data = keys.reduce(config.collection) do |memo, nested_key|
41
61
  memo[nested_key.to_s] if memo
42
62
  end
63
+ return data if data
64
+ raise DataNotFound, "collection data not found for `#{keys.join(config.splitter)}`"
43
65
  end
44
66
 
45
- def config
46
- Config.config
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
47
71
  end
48
72
 
49
73
  class UnknownLocale < StandardError; end
74
+ class DataNotFound < StandardError; end
75
+ class RequiredChannelNotFound < StandardError; end
50
76
  end
51
77
  end
@@ -10,6 +10,11 @@ module HeyYou
10
10
  build
11
11
  end
12
12
 
13
+ def to_hash
14
+ raise NotImplementedError, 'Builder not implemented #to_hash method'
15
+ end
16
+ alias_method :to_h, :to_hash
17
+
13
18
  private
14
19
 
15
20
  def interpolate(notification_string, options)
@@ -12,6 +12,10 @@ module HeyYou
12
12
  @body = interpolate(ch_data.fetch('body'), options)
13
13
  @subject = interpolate(ch_data.fetch('subject'), options)
14
14
  end
15
+
16
+ def to_hash
17
+ { body: body, subject: subject }
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -10,6 +10,10 @@ module HeyYou
10
10
  @body = interpolate(ch_data.fetch('body'), options)
11
11
  @data = options[:data] || {}
12
12
  end
13
+
14
+ def to_hash
15
+ { title: title, body: body, data: data }
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -1,6 +1,10 @@
1
+ require 'hey_you/helper'
1
2
  module HeyYou
2
3
  module Channels
3
4
  class Base
5
+ include HeyYou::Helper
6
+ extend HeyYou::Helper
7
+
4
8
  class << self
5
9
  def send!
6
10
  raise NotImplementedError, 'You should define #send! method in your channel.'
@@ -25,10 +29,6 @@ module HeyYou
25
29
  []
26
30
  end
27
31
 
28
- def config
29
- Config.config
30
- end
31
-
32
32
  def ch_name
33
33
  self.class.name.split('::').last.downcase
34
34
  end
@@ -6,31 +6,31 @@ module HeyYou
6
6
  class Email < Base
7
7
  class << self
8
8
  def send!(builder, to:, **options)
9
- raise CredentialsNotExists unless credentials_present?
10
-
11
- method = config.email.default_mailing ? :send_via_mail : :send_via_custom_class
9
+ method = config.email.use_default_mailing ? :send_via_mail : :send_via_custom_class
12
10
  public_send(method, builder, to, options)
13
11
  end
14
12
 
15
13
  # Send email via custom class instance.
16
14
  def send_via_custom_class(builder, to, **options)
17
15
  mailer = mailer_class_from_builder(builder, options)
18
-
19
- mailer_method = options[:mailer_method] ||
20
- builder.email.mailer_method ||
21
- config.email.default_mailer_method
22
-
16
+ mailer_method = mailer_method_from_builder(mailer, builder, options)
23
17
  delivery_method = options[:delivery_method] ||
24
18
  builder.email.delivery_method ||
25
19
  config.email.default_delivery_method
26
20
 
27
21
  log("Build mail via #{mailer}##{mailer_method}. Delivery with #{delivery_method}")
28
- mailer_msg = mailer.public_send(mailer_method, data: builder.email, to: to)
29
- mailer_msg.public_send(delivery_method)
22
+ mailer_msg = mailer.public_send(mailer_method, data: builder.email.to_hash, to: to)
23
+ return mailer_msg.public_send(delivery_method) if mailer_msg.respond_to?(delivery_method)
24
+ raise(
25
+ DeliveryMethodNotDefined,
26
+ "Instance of mailer #{mailer} with method #{mailer_method} not respond to #{delivery_method}"
27
+ )
30
28
  end
31
29
 
32
30
  # Send email with standard mail (gem 'mail')
33
31
  def send_via_mail(builder, to, **_)
32
+ raise CredentialsNotExists unless credentials_present?
33
+
34
34
  context = self
35
35
  mail = Mail.new do
36
36
  from HeyYou::Config.instance.email.from
@@ -74,14 +74,29 @@ module HeyYou
74
74
 
75
75
  mailer
76
76
  end
77
- end
78
77
 
79
- class CredentialsNotExists < StandardError;
80
- end
81
- class ActionMailerClassNotDefined < StandardError;
82
- end
83
- class MailerClassNotDefined < StandardError;
78
+ def mailer_method_from_builder(mailer_class, builder, **options)
79
+ mailer_method = options[:mailer_method] ||
80
+ builder.email.mailer_method ||
81
+ config.email.default_mailer_method
82
+ unless mailer_method
83
+ raise(
84
+ MailerMethodNotDefined,
85
+ 'You must set mailer_method in notifications collection or pass :mailer_method option.'
86
+ )
87
+ end
88
+
89
+ return mailer_method if mailer_class.respond_to?(mailer_method)
90
+ raise MailerMethodNotDefined, "Method ##{mailer_method} not defined for #{mailer_class}."
91
+ end
84
92
  end
93
+
94
+ class CredentialsNotExists < StandardError; end
95
+ class ActionMailerClassNotDefined < StandardError; end
96
+ class MailerClassNotDefined < StandardError; end
97
+ class MailerMethodNotDefined < StandardError; end
98
+ class DeliveryMethodNotDefined < StandardError; end
99
+
85
100
  end
86
101
  end
87
102
  end
@@ -1,7 +1,8 @@
1
1
  require 'yaml'
2
- require_relative 'config/conigurable'
3
- require_relative 'config/push'
4
- require_relative 'config/email'
2
+ require 'hey_you/config/conigurable'
3
+ require 'hey_you/config/push'
4
+ require 'hey_you/config/email'
5
+ require 'hey_you/config/data_source'
5
6
 
6
7
  #
7
8
  # @config REQUIRED collection_file [String] - File path for general notifications file
@@ -17,7 +18,15 @@ module HeyYou
17
18
  class Config
18
19
  extend Configurable
19
20
 
20
- DEFAULT_REGISTERED_CHANNELS = %i[email push]
21
+ DEFAULTS = {
22
+ registered_channels: %i[email push],
23
+ splitter: '.',
24
+ log_tag: 'HeyYou',
25
+ localization: false,
26
+ require_all_channels: false
27
+ }
28
+
29
+ DEFAULT_REGISTERED_CHANNELS =
21
30
  DEFAULT_SPLITTER = '.'
22
31
  DEFAULT_GLOBAL_LOG_TAG = 'HeyYou'
23
32
  DEFAULT_LOCALIZATION_FLAG = false
@@ -26,26 +35,20 @@ module HeyYou
26
35
 
27
36
  attr_reader :collection, :env_collection, :configured, :registered_receivers
28
37
  attr_accessor(
29
- :collection_files, :env_collection_file, :splitter,
30
- :registered_channels, :localization, :logger, :log_tag
38
+ :splitter, :registered_channels, :localization, :logger, :log_tag,
39
+ :require_all_channels, :data_source
31
40
  )
32
41
 
33
42
  def initialize
34
- @registered_channels ||= DEFAULT_REGISTERED_CHANNELS
35
- @splitter ||= DEFAULT_SPLITTER
43
+ @registered_channels ||= DEFAULTS[:registered_channels]
44
+ @splitter ||= DEFAULTS[:splitter]
36
45
  @registered_receivers = []
37
- @log_tag ||= DEFAULT_GLOBAL_LOG_TAG
38
- @localization ||= DEFAULT_LOCALIZATION_FLAG
46
+ @log_tag ||= DEFAULTS[:log_tag]
47
+ @localization ||= DEFAULTS[:localization]
48
+ @require_all_channels = DEFAULTS[:require_all_channels]
39
49
  define_ch_config_methods
40
50
  end
41
51
 
42
- def collection_file
43
- @collection_files || raise(
44
- CollectionFileNotDefined,
45
- 'You must define HeyYou::Config.collection_files'
46
- )
47
- end
48
-
49
52
  def collection
50
53
  @collection ||= load_collection
51
54
  end
@@ -74,6 +77,10 @@ module HeyYou
74
77
  logger&.info("[#{log_tag}] #{msg} ")
75
78
  end
76
79
 
80
+ def data_source
81
+ DataSource.instance
82
+ end
83
+
77
84
  private
78
85
 
79
86
  def define_ch_config_methods
@@ -91,21 +98,8 @@ module HeyYou
91
98
  self.class.send(:define_method, ch, method_proc)
92
99
  end
93
100
 
94
- # Load yaml from collection_file and merge it with yaml from env_collection_file
95
101
  def load_collection
96
- @collection_files = [collection_files] if collection_files.is_a?(String)
97
- notification_collection = {}
98
- collection_files.each do |file|
99
- notification_collection.merge!(YAML.load_file(file))
100
- end
101
- notification_collection.merge!(env_collection)
102
- end
103
-
104
- def load_env_collection
105
- if env_collection_file
106
- return YAML.load_file(env_collection_file) rescue { }
107
- end
108
- {}
102
+ data_source.load_data
109
103
  end
110
104
  end
111
105
  end
@@ -11,14 +11,15 @@ module HeyYou
11
11
  end
12
12
 
13
13
  def configure(&block)
14
- # TODO: log warn "Already configured" instead nil
15
- @configured ? nil : instance_eval(&block)
14
+ @configured ? raise(AlreadyConfiguredError, 'You already configure HeyYou') : instance_eval(&block)
16
15
  @configured = true
17
16
  end
18
17
 
19
18
  def config
20
19
  @config ||= self.instance
21
20
  end
21
+
22
+ class AlreadyConfiguredError < StandardError; end
22
23
  end
23
24
  end
24
25
  end
@@ -0,0 +1,38 @@
1
+ require 'hey_you/config/conigurable'
2
+ require 'hey_you/data_source/yaml'
3
+ require 'hey_you/data_source/hash'
4
+
5
+ module HeyYou
6
+ class Config
7
+ class DataSource
8
+ extend Configurable
9
+
10
+ attr_accessor :source_class, :options, :source_instance
11
+
12
+ def initialize
13
+ @type = DEFAULTS[:type]
14
+ @options = DEFAULTS[:options]
15
+ @source_class = HeyYou::DataSource::Yaml
16
+ end
17
+
18
+ def load_data
19
+ return source_instance.load_collections if source_instance
20
+
21
+ if source_class.nil?
22
+ raise InvalidDataSourceError, 'You must pass `config.data_source.source_class` in configuration.'
23
+ end
24
+
25
+ source_class.new(options).load_collections
26
+ rescue ArgumentError => err
27
+ problem_fields =
28
+ err.message.gsub(/missing keyword(.?):\s/, '').split(', ').map { |f| "`#{f}`" }.join(', ')
29
+ field_word = problem_fields.split(', ').size > 1 ? 'fields' : 'field'
30
+ msg = "You must pass #{field_word} #{problem_fields} for `config.data_source.options` in configuration"
31
+
32
+ raise InvalidOptionsError, msg
33
+ end
34
+
35
+ class InvalidOptionsError < StandardError; end
36
+ end
37
+ end
38
+ end
@@ -1,4 +1,4 @@
1
- require_relative 'conigurable'
1
+ require 'hey_you/config/conigurable'
2
2
  require 'mail'
3
3
 
4
4
  module HeyYou
@@ -13,7 +13,7 @@ module HeyYou
13
13
  attr_accessor(
14
14
  :from,
15
15
  :mail_delivery_method,
16
- :default_mailing,
16
+ :use_default_mailing,
17
17
  :default_delivery_method,
18
18
  :default_mailer_class,
19
19
  :default_mailer_method
@@ -21,7 +21,7 @@ module HeyYou
21
21
 
22
22
  def initialize
23
23
  @mail_delivery_method ||= MAIL_DELIVERY_METHOD
24
- @default_mailing ||= !default_mailer_class.nil?
24
+ @use_default_mailing ||= !default_mailer_class.nil?
25
25
  @async ||= true
26
26
  @default_mailer_method ||= DEFAULT_ACTION_MAILER_METHOD
27
27
  @default_delivery_method ||= DEFAULT_DELIVERY_METHOD
@@ -1,4 +1,4 @@
1
- require_relative 'conigurable'
1
+ require 'hey_you/config/conigurable'
2
2
  require 'fcm'
3
3
 
4
4
  module HeyYou
@@ -0,0 +1,9 @@
1
+ module HeyYou
2
+ module DataSource
3
+ class Base
4
+ def load_notifications
5
+ raise NotImplementedError
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ require 'hey_you/data_source/_base'
2
+
3
+ module HeyYou
4
+ module DataSource
5
+ class Hash < Base
6
+ attr_reader :data
7
+
8
+ def initialize(data:)
9
+ @data = data
10
+ end
11
+
12
+ def load_collections
13
+ data
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,37 @@
1
+ require 'hey_you/data_source/_base'
2
+
3
+ module HeyYou
4
+ module DataSource
5
+ class Yaml < Base
6
+ attr_reader :collection_files, :env_collection_file
7
+
8
+ def initialize(collection_files:, env_collection_file: nil)
9
+ @collection_files = collection_files
10
+ @collection_files = [collection_files] if collection_files.is_a?(String)
11
+ @env_collection_file = env_collection_file
12
+ end
13
+
14
+ # Load yaml from collection_file and merge it with yaml from env_collection_file
15
+ def load_collections
16
+ notification_collection = {}
17
+ collection_files.each do |file|
18
+ notification_collection.merge!(YAML.load_file(file))
19
+ end
20
+ notification_collection.merge!(env_collection)
21
+ end
22
+
23
+ def env_collection
24
+ @env_collection ||= load_env_collection
25
+ end
26
+
27
+ private
28
+
29
+ def load_env_collection
30
+ if env_collection_file
31
+ return YAML.load_file(env_collection_file) rescue { }
32
+ end
33
+ {}
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ module HeyYou
2
+ module Helper
3
+ def config
4
+ Config.instance
5
+ end
6
+ end
7
+ end
@@ -1,7 +1,10 @@
1
- require_relative 'sender'
1
+ require 'hey_you/sender'
2
+ require 'hey_you/helper'
2
3
 
3
4
  module HeyYou
4
5
  module Receiver
6
+ include HeyYou::Helper
7
+
5
8
  attr_reader :receiver_channels, :receiver_data
6
9
 
7
10
  def self.extended klass
@@ -48,7 +51,7 @@ module HeyYou
48
51
 
49
52
  @receiver_data = receiver_data
50
53
  @receiver_channels = receiver_data.keys
51
- hey_you_config.registrate_receiver(self)
54
+ config.registrate_receiver(self)
52
55
 
53
56
  define_receive_info_methods
54
57
  end
@@ -57,10 +60,10 @@ module HeyYou
57
60
 
58
61
  def check_channels(channels)
59
62
  channels.all? do |ch|
60
- next if hey_you_config.registered_channels.include?(ch.to_sym)
63
+ next if config.registered_channels.include?(ch.to_sym)
61
64
  raise(
62
65
  NotRegisteredChannel,
63
- "Channel #{ch} not registered. Registered channels: #{hey_you_config.registered_channels}"
66
+ "Channel #{ch} not registered. Registered channels: #{config.registered_channels}"
64
67
  )
65
68
  end
66
69
  @received_channels = channels
@@ -79,10 +82,6 @@ module HeyYou
79
82
  end
80
83
  end
81
84
 
82
- def hey_you_config
83
- Config.config
84
- end
85
-
86
85
  class NotRegisteredChannel < StandardError;
87
86
  end
88
87
  end
@@ -1,10 +1,13 @@
1
- require_relative 'builder'
2
-
3
- require_relative 'channels/push'
4
- require_relative 'channels/email'
1
+ require 'hey_you/helper'
2
+ require 'hey_you/builder'
3
+ require 'hey_you/channels/push'
4
+ require 'hey_you/channels/email'
5
5
 
6
6
  module HeyYou
7
7
  class Sender
8
+ include HeyYou::Helper
9
+ extend HeyYou::Helper
10
+
8
11
  class << self
9
12
  # Send notifications for receiver
10
13
  #
@@ -41,13 +44,14 @@ module HeyYou
41
44
  builder = Builder.new(notification_key, options)
42
45
  response = {}
43
46
  config.registered_channels.each do |ch|
44
- if channel_allowed?(ch, receive_info, builder, options)
47
+ if channel_allowed?(ch, receive_info, builder, options) && builder.respond_to?(ch) && builder.public_send(ch)
45
48
  config.log(
46
- "Send #{ch}-message to #{receive_info[ch][:subject]} with data: #{builder.send(ch).data}" \
49
+ "Send #{ch}-message to #{receive_info[ch][:subject]} with data: #{builder.public_send(ch).data}" \
47
50
  " and options: #{receive_info[ch][:options]}"
48
51
  )
52
+ receive_options = receive_info[ch].fetch(:options, {}) || {}
49
53
  response[ch] = Channels.const_get(ch.to_s.capitalize).send!(
50
- builder, to: receive_info[ch][:subject], **receive_info[ch][:options]
54
+ builder, to: receive_info[ch][:subject], **receive_options
51
55
  )
52
56
  else
53
57
  config.log("Channel #{ch} not allowed.")
@@ -59,9 +63,8 @@ module HeyYou
59
63
  private
60
64
 
61
65
  def channel_allowed?(ch, to, builder, **options)
62
- unless to[ch].is_a?(Hash) ? to[ch.to_sym][:subject] || to[ch.to_s][:subject] : to[ch.to_sym] || to[ch.to_s]
63
- return false
64
- end
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]
67
+ return false unless condition
65
68
  channel_allowed_by_only?(ch, options[:only]) && !builder.send(ch).nil?
66
69
  end
67
70
 
@@ -74,10 +77,6 @@ module HeyYou
74
77
  return only.map(&:to_sym).include?(ch.to_sym) if only.is_a?(Array)
75
78
  only.to_sym == ch.to_sym
76
79
  end
77
-
78
- def config
79
- Config.config
80
- end
81
80
  end
82
81
 
83
82
  class NotRegisteredReceiver < StandardError; end
@@ -1,3 +1,3 @@
1
1
  module HeyYou
2
- VERSION = "0.1.7"
2
+ VERSION = "1.2.1"
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: 0.1.7
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Nesterov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-09 00:00:00.000000000 Z
11
+ date: 2020-07-01 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
@@ -121,6 +121,7 @@ files:
121
121
  - ".rspec"
122
122
  - ".rubocop.yml"
123
123
  - ".travis.yml"
124
+ - CHANGELOG.md
124
125
  - Gemfile
125
126
  - README.md
126
127
  - Rakefile
@@ -137,8 +138,13 @@ files:
137
138
  - lib/hey_you/channels/push.rb
138
139
  - lib/hey_you/config.rb
139
140
  - lib/hey_you/config/conigurable.rb
141
+ - lib/hey_you/config/data_source.rb
140
142
  - lib/hey_you/config/email.rb
141
143
  - lib/hey_you/config/push.rb
144
+ - lib/hey_you/data_source/_base.rb
145
+ - lib/hey_you/data_source/hash.rb
146
+ - lib/hey_you/data_source/yaml.rb
147
+ - lib/hey_you/helper.rb
142
148
  - lib/hey_you/receiver.rb
143
149
  - lib/hey_you/sender.rb
144
150
  - lib/hey_you/version.rb
@@ -160,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
166
  - !ruby/object:Gem::Version
161
167
  version: '0'
162
168
  requirements: []
163
- rubygems_version: 3.0.1
169
+ rubygems_version: 3.0.8
164
170
  signing_key:
165
171
  specification_version: 4
166
172
  summary: Send multichannel notification with one command.