hey-you 0.1.8 → 1.0.0

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: 111d968c94f6f51e803d4232f32e335be19897f91d0ab391ce46a85916a3eae0
4
- data.tar.gz: 9915bfd76064d7c977fab321aa655e66dfb32a4b18a9303b7529c8c115bc991c
3
+ metadata.gz: f5ce0968273a44b7121fd2d9799905b2f2c8ca013f40f3e497ff45f7e83feeb0
4
+ data.tar.gz: fc8c194e95f2f76314339dbfb64ddf36f38c818cc07296cd9516fb9a44135f56
5
5
  SHA512:
6
- metadata.gz: c31b6441abb8c789a634857200271d92a2f685bc1f4e85c636c13b616d5ddccb30e876dbf2318dfcb2b484cf715ed57bca6bff137bccb5934d09b263d846333b
7
- data.tar.gz: 54138ca7c7d4f13b6c226273d1fa85d31b9fda63426c3f67f9c20071f71cee2990403ae7d5f7257bfe2cc19053494c498333914bdb370a7bc87375eef521445b
6
+ metadata.gz: c3630186f02e156de3db5cbbe7195d4ba83d2bc31a4f000c3fc5afd7d1b69f193f742e6a4acab1eef1a741727c14e9f296856fe28e79e4df2eb6ff92b7006da6
7
+ data.tar.gz: 14d5065592601334769ca13f815a6d10665c8718d2ea14317867d40be0d713fd88c9a1e67c8b27f9b7805281e58bc64682c7e77498c4ffcab2b46edaa4c44644
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # HeyYou [Alpha]
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
@@ -67,14 +68,18 @@ environment. You can set it like `notifications.#{ENV['APP_ENV]}.yml`
67
68
  builder. Default: `.`
68
69
  * __config.registered_channels__ - Avialable channels for your
69
70
  applications. Default: `[:push, :email]`
71
+ * __config.require_all_channels__ - Boolean. If true, when data for channel will not found in
72
+ file collection, error will be raised. Default: `false`
70
73
  * __config.localization__ - Boolean. If true, hey-you begin support I18n locales for notifications collection. Your
71
- notifications for build should be nested in `I18n.locale` key. For example:
74
+ notifications for build should be nested in `I18n.locale` key. Default: `false`. For example:
72
75
 
73
76
  ```ruby
74
77
  # config/initializers/hey-you.rb
75
78
  HeyYou::Config.configure do
76
79
  ...
77
- config.collection_files = I18n.available_locales.map { |locale| "config/notifications/#{locale}.yml" }
80
+ config.collection_files = Rails.application.config.i18n.available_locales.map do |locale|
81
+ "config/notifications/#{locale}.yml"
82
+ end
78
83
  ...
79
84
  end
80
85
  ```
@@ -124,7 +129,7 @@ Default 60
124
129
  Default 30
125
130
  ##### Email
126
131
  * __config.email.layout__ - default layout for email letters.
127
- * __config.email.default_mailing__ - use default mail sending or use custom mailer classes
132
+ * __config.email.use_default_mailing__ - use default mail sending or use custom mailer classes
128
133
  * __config.email.default_mailer_class__ - default mailer class for email notifications
129
134
  * __config.email.default_mailer_method__ - default mailer_method for mailer_class
130
135
  * __config.email.default_delivery_method__ - expects, that mailer_method will build message and delivery_method will send it.
@@ -359,7 +364,7 @@ in your channel config.
359
364
  Now, when you will send notification, it will be send with your channel too.
360
365
 
361
366
  ## Extensions
362
- * [Slack channel](https://github.com/QNester/hey-you)
367
+ * [Slack channel](https://github.com/QNester/hey-you-slack)
363
368
  [![Gem Version](https://badge.fury.io/rb/hey-you-slack.svg)](https://badge.fury.io/rb/hey-you-slack)
364
369
 
365
370
  ## Development
@@ -1,10 +1,13 @@
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
@@ -14,18 +17,32 @@ module HeyYou
14
17
  @data = fetch_from_collection_by_key(key, options[:locale])
15
18
  @options = options
16
19
  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)
20
+ init_channel_builder(ch, key)
22
21
  end
23
22
  end
24
23
 
25
24
  private
26
25
 
27
- def define_ch_method(ch)
28
- method_proc = -> { instance_variable_get("@#{ch}".to_sym) }
26
+ def init_channel_builder(ch, key)
27
+ if config.require_all_channels
28
+ unless data[ch.to_s]
29
+ raise RequiredChannelNotFound, "For key #{key} and channel #{ch} data not exists in collection."
30
+ end
31
+ end
32
+ unless data[ch.to_s]
33
+ define_ch_method(ch, true)
34
+ return
35
+ end
36
+
37
+ ch_builder =
38
+ HeyYou::Builder.const_get("#{ch.downcase.capitalize}").new(data, key, options)
39
+ instance_variable_set("@#{ch}".to_sym, ch_builder)
40
+
41
+ define_ch_method(ch)
42
+ end
43
+
44
+ def define_ch_method(ch, empty = false)
45
+ method_proc = empty ? -> { nil } : -> { instance_variable_get("@#{ch}".to_sym) }
29
46
  self.class.send(:define_method, ch, method_proc)
30
47
  end
31
48
 
@@ -37,15 +54,15 @@ module HeyYou
37
54
  keys << locale
38
55
  end
39
56
  keys = keys + key.to_s.split(config.splitter)
40
- keys.reduce(config.collection) do |memo, nested_key|
57
+ data = keys.reduce(config.collection) do |memo, nested_key|
41
58
  memo[nested_key.to_s] if memo
42
59
  end
43
- end
44
-
45
- def config
46
- Config.config
60
+ return data if data
61
+ raise DataNotFound, "collection data not found for `#{keys.join(config.splitter)}`"
47
62
  end
48
63
 
49
64
  class UnknownLocale < StandardError; end
65
+ class DataNotFound < StandardError; end
66
+ class RequiredChannelNotFound < StandardError; end
50
67
  end
51
68
  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
22
  mailer_msg = mailer.public_send(mailer_method, data: builder.email, to: to)
29
- mailer_msg.public_send(delivery_method)
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,7 @@
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
5
 
6
6
  #
7
7
  # @config REQUIRED collection_file [String] - File path for general notifications file
@@ -17,7 +17,15 @@ module HeyYou
17
17
  class Config
18
18
  extend Configurable
19
19
 
20
- DEFAULT_REGISTERED_CHANNELS = %i[email push]
20
+ DEFAULTS = {
21
+ registered_channels: %i[email push],
22
+ splitter: '.',
23
+ log_tag: 'HeyYou',
24
+ localization: false,
25
+ require_all_channels: false
26
+ }
27
+
28
+ DEFAULT_REGISTERED_CHANNELS =
21
29
  DEFAULT_SPLITTER = '.'
22
30
  DEFAULT_GLOBAL_LOG_TAG = 'HeyYou'
23
31
  DEFAULT_LOCALIZATION_FLAG = false
@@ -27,15 +35,17 @@ module HeyYou
27
35
  attr_reader :collection, :env_collection, :configured, :registered_receivers
28
36
  attr_accessor(
29
37
  :collection_files, :env_collection_file, :splitter,
30
- :registered_channels, :localization, :logger, :log_tag
38
+ :registered_channels, :localization, :logger, :log_tag,
39
+ :require_all_channels
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
 
@@ -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
@@ -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,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,9 +44,9 @@ 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
  )
49
52
  receive_options = receive_info[ch].fetch(:options, {}) || {}
@@ -60,9 +63,8 @@ module HeyYou
60
63
  private
61
64
 
62
65
  def channel_allowed?(ch, to, builder, **options)
63
- unless to[ch].is_a?(Hash) ? to[ch.to_sym][:subject] || to[ch.to_s][:subject] : to[ch.to_sym] || to[ch.to_s]
64
- return false
65
- 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
66
68
  channel_allowed_by_only?(ch, options[:only]) && !builder.send(ch).nil?
67
69
  end
68
70
 
@@ -75,10 +77,6 @@ module HeyYou
75
77
  return only.map(&:to_sym).include?(ch.to_sym) if only.is_a?(Array)
76
78
  only.to_sym == ch.to_sym
77
79
  end
78
-
79
- def config
80
- Config.config
81
- end
82
80
  end
83
81
 
84
82
  class NotRegisteredReceiver < StandardError; end
@@ -1,3 +1,3 @@
1
1
  module HeyYou
2
- VERSION = "0.1.8"
2
+ VERSION = "1.0.0"
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.8
4
+ version: 1.0.0
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-28 00:00:00.000000000 Z
11
+ date: 2019-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fcm
@@ -139,6 +139,7 @@ files:
139
139
  - lib/hey_you/config/conigurable.rb
140
140
  - lib/hey_you/config/email.rb
141
141
  - lib/hey_you/config/push.rb
142
+ - lib/hey_you/helper.rb
142
143
  - lib/hey_you/receiver.rb
143
144
  - lib/hey_you/sender.rb
144
145
  - lib/hey_you/version.rb
@@ -160,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
161
  - !ruby/object:Gem::Version
161
162
  version: '0'
162
163
  requirements: []
163
- rubygems_version: 3.0.1
164
+ rubygems_version: 3.0.6
164
165
  signing_key:
165
166
  specification_version: 4
166
167
  summary: Send multichannel notification with one command.