hey-you 1.0.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -4
- data/CHANGELOG.md +4 -0
- data/README.md +50 -6
- data/lib/hey_you/config.rb +8 -24
- data/lib/hey_you/config/data_source.rb +38 -0
- data/lib/hey_you/data_source/_base.rb +9 -0
- data/lib/hey_you/data_source/hash.rb +17 -0
- data/lib/hey_you/data_source/yaml.rb +37 -0
- data/lib/hey_you/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d00b4135e266174d5c8a8a1090a34acedac8b2fbdc6c82c0dd19d9e30072ef3
|
4
|
+
data.tar.gz: ae481c0244a148df0620a2507b6eacb94dfdaa72f4cbc6ddeccd909ce2c6d914
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40ce68c45090a678097d965c98603eeec09fbf87f2946f7f3602137c92a46636320e89a88c329b17a146b4eb0543be6e716d7ccbac2e4036dba0fe7ef476f6ea
|
7
|
+
data.tar.gz: 538ceba1fd584b76ef57bad07583474d8ad17f52375f0521a467fd30208f5996ee76d9442f7927ae5ca8893ca41ec52e82c981df16a999e34e70aeef57c966db
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# HeyYou
|
1
|
+
# HeyYou
|
2
2
|
[![Build Status](https://travis-ci.com/QNester/hey-you.svg?branch=master)](https://travis-ci.com/QNester/hey-you#)
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/hey-you.svg)](https://badge.fury.io/rb/hey-you)
|
4
4
|
|
@@ -44,15 +44,18 @@ Or install it yourself as:
|
|
44
44
|
First, you must configure HeyYou. Example:
|
45
45
|
```ruby
|
46
46
|
HeyYou::Config.configure do
|
47
|
-
config.
|
47
|
+
config.data_source.options = { collection_files: ['config/notifications.yml'] }
|
48
48
|
config.email.from = 'noreply@example-mail.com'
|
49
49
|
config.push.fcm_token = 'fcm_server_key'
|
50
50
|
end
|
51
51
|
```
|
52
52
|
#### Required settings
|
53
53
|
Options for gem base work.
|
54
|
-
#####
|
55
|
-
* __config.
|
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`
|
56
59
|
##### Push
|
57
60
|
* __config.push.fcm_token__ - Required setting for push channel. You can not send
|
58
61
|
push messages if setting was not set. You should set it to equal your fcm server key.
|
@@ -77,9 +80,10 @@ notifications for build should be nested in `I18n.locale` key. Default: `false`.
|
|
77
80
|
# config/initializers/hey-you.rb
|
78
81
|
HeyYou::Config.configure do
|
79
82
|
...
|
80
|
-
|
83
|
+
i18n_files = Rails.application.config.i18n.available_locales.map do |locale|
|
81
84
|
"config/notifications/#{locale}.yml"
|
82
85
|
end
|
86
|
+
config.data_source.options = { collection_files: i18n_files }
|
83
87
|
...
|
84
88
|
end
|
85
89
|
```
|
@@ -187,7 +191,7 @@ for method? This is string key for builder. Read next to understand it.
|
|
187
191
|
|
188
192
|
### Build your notification
|
189
193
|
HeyYou Notification Builder - good system for store your notifications in one place.
|
190
|
-
|
194
|
+
By default you need create yml file with follow format:
|
191
195
|
```yaml
|
192
196
|
# config/notifications/collection.yml
|
193
197
|
any_key:
|
@@ -214,6 +218,46 @@ notification will send for all available channels for receiver:
|
|
214
218
|
1) Send push via fcm
|
215
219
|
2) Send email
|
216
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
|
+
|
217
261
|
### Send notification
|
218
262
|
Receiver not only one way to send notification. You can send it using `HeyYou::Sender`.
|
219
263
|
Just use method `#send` for HeyYou::Sender and pass notification key and `to` options
|
data/lib/hey_you/config.rb
CHANGED
@@ -2,6 +2,7 @@ require 'yaml'
|
|
2
2
|
require 'hey_you/config/conigurable'
|
3
3
|
require 'hey_you/config/push'
|
4
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
|
@@ -34,9 +35,8 @@ module HeyYou
|
|
34
35
|
|
35
36
|
attr_reader :collection, :env_collection, :configured, :registered_receivers
|
36
37
|
attr_accessor(
|
37
|
-
:
|
38
|
-
:
|
39
|
-
:require_all_channels
|
38
|
+
:splitter, :registered_channels, :localization, :logger, :log_tag,
|
39
|
+
:require_all_channels, :data_source
|
40
40
|
)
|
41
41
|
|
42
42
|
def initialize
|
@@ -49,13 +49,6 @@ module HeyYou
|
|
49
49
|
define_ch_config_methods
|
50
50
|
end
|
51
51
|
|
52
|
-
def collection_file
|
53
|
-
@collection_files || raise(
|
54
|
-
CollectionFileNotDefined,
|
55
|
-
'You must define HeyYou::Config.collection_files'
|
56
|
-
)
|
57
|
-
end
|
58
|
-
|
59
52
|
def collection
|
60
53
|
@collection ||= load_collection
|
61
54
|
end
|
@@ -84,6 +77,10 @@ module HeyYou
|
|
84
77
|
logger&.info("[#{log_tag}] #{msg} ")
|
85
78
|
end
|
86
79
|
|
80
|
+
def data_source
|
81
|
+
DataSource.instance
|
82
|
+
end
|
83
|
+
|
87
84
|
private
|
88
85
|
|
89
86
|
def define_ch_config_methods
|
@@ -101,21 +98,8 @@ module HeyYou
|
|
101
98
|
self.class.send(:define_method, ch, method_proc)
|
102
99
|
end
|
103
100
|
|
104
|
-
# Load yaml from collection_file and merge it with yaml from env_collection_file
|
105
101
|
def load_collection
|
106
|
-
|
107
|
-
notification_collection = {}
|
108
|
-
collection_files.each do |file|
|
109
|
-
notification_collection.merge!(YAML.load_file(file))
|
110
|
-
end
|
111
|
-
notification_collection.merge!(env_collection)
|
112
|
-
end
|
113
|
-
|
114
|
-
def load_env_collection
|
115
|
-
if env_collection_file
|
116
|
-
return YAML.load_file(env_collection_file) rescue { }
|
117
|
-
end
|
118
|
-
{}
|
102
|
+
data_source.load_data
|
119
103
|
end
|
120
104
|
end
|
121
105
|
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
|
@@ -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
|
data/lib/hey_you/version.rb
CHANGED
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.0
|
4
|
+
version: 1.2.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:
|
11
|
+
date: 2020-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fcm
|
@@ -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,12 @@ 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
|
142
147
|
- lib/hey_you/helper.rb
|
143
148
|
- lib/hey_you/receiver.rb
|
144
149
|
- lib/hey_you/sender.rb
|