talktome 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: 868359e1f73e9fcf52180d2217179b7ea15ad990
4
- data.tar.gz: 49a82192411f5ca01f129e3ece14712ce1eee0be
3
+ metadata.gz: 2817b7e3a3e6d077e889d0d53ac368195785af09
4
+ data.tar.gz: e5d9c60306e644301a1029739f16041f3b740be2
5
5
  SHA512:
6
- metadata.gz: b485f9ec89169ba868237e9007cf9d2f46cb42fbffb5ed28e1565a5193b3e4d2047b48284343a1ca395b370cd046156479fea710784614a404268d31382ad0f6
7
- data.tar.gz: ccb84f63e28059def408fa31365913f993cde2335b8ec62874e8de180bfa493a37fec596cddaca1765456c6ce8cf0d44eb0da39ea49daa6cb4439232226cd05e
6
+ metadata.gz: 8cf1e843cfd0ce3187bc693889ed5baf7e0deeddff436cf483ec0af6f20f6698e5d91eea60717f990b270f274bddd1ff959e75a1dca8fc7e5ad7325873c31f56
7
+ data.tar.gz: f312d5300c8eeacee34bd90097d61bd996246081b0400e013f59a63147a374719a59c904b3bd191481fc9d08e1417c5eebf266374931273a448e05b047ff0c8f
@@ -9,6 +9,56 @@ module Talktome
9
9
  end
10
10
  module_function :redcarpet
11
11
 
12
+ #
13
+ # Infer all client and strategy options from environment variables.
14
+ # The following ones are recognized:
15
+ #
16
+ # - TALKTOME_DEBUG: when set (to anything) enables the dumping of sent
17
+ # messages to the debug folder
18
+ # - TALKTOME_EMAIL_DELIVERY: "smtp", "file" or "test"
19
+ # - TALKTOME_EMAIL_DEFAULT_FROM: default from address to use for email sending
20
+ # - TALKTOME_SMTP_ADDRESS: host address for smtp sending
21
+ # - TALKTOME_SMTP_PORT: port of smtp server to use
22
+ # - TALKTOME_SMTP_DOMAIN: sending domain
23
+ # - TALKTOME_SMTP_USER: user for smtp authentication
24
+ # - TALKTOME_SMTP_PASSWORD: user for smtp authentication
25
+ #
26
+ def auto_options(folder)
27
+ options = {}
28
+ debug_folder = folder/"tmp"
29
+
30
+ options[:debugger] = ->(message, user, handler) {
31
+ debug_folder.mkdir_p unless debug_folder.exists?
32
+ (debug_folder/"#{user[:email]}.html").write(message.to_html)
33
+ (debug_folder/"#{user[:email]}.txt").write(message.to_text)
34
+ } if ENV['TALKTOME_DEBUG']
35
+
36
+ options[:strategies] = {}
37
+
38
+ email_delivery = (ENV['TALKTOME_EMAIL_DELIVERY'] || "test").to_sym
39
+ email_config = {}
40
+
41
+ email_config.merge!({
42
+ address: ENV['TALKTOME_SMTP_ADDRESS'],
43
+ port: ENV['TALKTOME_SMTP_PORT'],
44
+ domain: ENV['TALKTOME_SMTP_DOMAIN'],
45
+ user_name: ENV['TALKTOME_SMTP_USER'],
46
+ password: ENV['TALKTOME_SMTP_PASSWORD']
47
+ }) if email_delivery == :smtp
48
+
49
+ email_config.merge!({
50
+ location: debug_folder.mkdir_p
51
+ }) if email_delivery == :file
52
+
53
+ options[:strategies][:email] = ::Talktome::Strategy::Email.new{|email|
54
+ email.delivery_method(email_delivery, email_config)
55
+ email.from(ENV['TALKTOME_EMAIL_DEFAULT_FROM']) if ENV['TALKTOME_EMAIL_DEFAULT_FROM']
56
+ }
57
+
58
+ options
59
+ end
60
+ module_function :auto_options
61
+
12
62
  end
13
63
  require 'talktome/version'
14
64
  require 'talktome/error'
@@ -1,19 +1,27 @@
1
1
  module Talktome
2
2
  class Client
3
3
 
4
- def initialize
5
- @strategies = {}
4
+ def initialize(folder, options = {})
5
+ raise ArgumentError, "Missing root folder" unless folder
6
+ raise ArgumentError, "Invalid root folder" unless Path(folder).directory?
7
+ @folder = folder
8
+ @options = Talktome.auto_options(folder).merge(options)
6
9
  yield self if block_given?
7
10
  end
11
+ attr_reader :folder, :options
8
12
 
9
13
  def strategy(name, stragegy)
10
- @strategies[name] = stragegy
14
+ strategies[name] = stragegy
11
15
  end
12
16
 
13
17
  protected
14
18
 
19
+ def strategies
20
+ options[:strategies]
21
+ end
22
+
15
23
  def get_handler(strategy)
16
- @strategies[strategy]
24
+ strategies[strategy]
17
25
  end
18
26
 
19
27
  end # class Client
@@ -1,21 +1,16 @@
1
1
  module Talktome
2
2
  class Client
3
+ #
4
+ # Builds a local client, that applies strategies synchronously and runs
5
+ # locally
6
+ #
3
7
  class Local < Client
4
8
 
5
- def initialize(folder, options = {})
6
- raise ArgumentError, "Missing messages folder" unless folder
7
- raise ArgumentError, "Invalid messages folder" unless Path(folder).directory?
8
- @folder = folder
9
- @options = options
10
- super()
11
- end
12
- attr_reader :folder, :options
13
-
14
- def talktome(message, user, tpldata, strategies)
9
+ def talktome(message, user, tpldata, strategies, &callback)
15
10
  message, handler = load_message!(message, strategies)
16
11
  message = message.instantiate(tpldata)
17
12
  options[:debugger].call(message, user, handler) if options[:debugger]
18
- handler.send_message message, user
13
+ handler.send_message message, user, &callback
19
14
  end
20
15
 
21
16
  protected
@@ -30,7 +30,7 @@ module Talktome
30
30
  end
31
31
 
32
32
  def extension
33
- path.ext.to_s
33
+ path.ext.to_s.gsub(/^\./,"")
34
34
  end
35
35
 
36
36
  def to_text
@@ -14,6 +14,8 @@ module Talktome
14
14
  message: message,
15
15
  user: user
16
16
  })
17
+ yield(@last) if block_given?
18
+ @last
17
19
  end
18
20
 
19
21
  end # class Debug
@@ -30,6 +30,8 @@ module Talktome
30
30
  raise "Unsupported extension `#{message.extension}`"
31
31
  end
32
32
 
33
+ yield(mail) if block_given?
34
+
33
35
  mail.deliver!
34
36
  end
35
37
 
@@ -1,7 +1,7 @@
1
1
  module Talktome
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 1
4
+ MINOR = 2
5
5
  TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
@@ -53,6 +53,14 @@ module Talktome
53
53
  expect(strategy.last.message).not_to be_nil
54
54
  expect(strategy.last.message.to_html).to eql("<html><title>Hello Test user</title><body><h1>Hello Test user</h1>\n\n<p>Welcome to this email example!</p>\n\n<h3>Test user</h3>\n</body></html>\n")
55
55
  end
56
+
57
+ it 'yields the callback with the email' do
58
+ seen = nil
59
+ client.talktome("welcome", user, tpldata, [:email]){|m|
60
+ seen = m
61
+ }
62
+ expect(seen).not_to be_nil
63
+ end
56
64
  end
57
65
 
58
66
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: talktome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-14 00:00:00.000000000 Z
11
+ date: 2019-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -79,9 +79,6 @@ dependencies:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
81
  version: '1'
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: 1.0.5
85
82
  type: :runtime
86
83
  prerelease: false
87
84
  version_requirements: !ruby/object:Gem::Requirement
@@ -89,9 +86,6 @@ dependencies:
89
86
  - - "~>"
90
87
  - !ruby/object:Gem::Version
91
88
  version: '1'
92
- - - ">="
93
- - !ruby/object:Gem::Version
94
- version: 1.0.5
95
89
  - !ruby/object:Gem::Dependency
96
90
  name: redcarpet
97
91
  requirement: !ruby/object:Gem::Requirement
@@ -99,9 +93,6 @@ dependencies:
99
93
  - - "~>"
100
94
  - !ruby/object:Gem::Version
101
95
  version: '3'
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- version: '3.4'
105
96
  type: :runtime
106
97
  prerelease: false
107
98
  version_requirements: !ruby/object:Gem::Requirement
@@ -109,9 +100,6 @@ dependencies:
109
100
  - - "~>"
110
101
  - !ruby/object:Gem::Version
111
102
  version: '3'
112
- - - ">="
113
- - !ruby/object:Gem::Version
114
- version: '3.4'
115
103
  description: Talktome helps you talk to users by email, messaging, sms, etc. It abstracts
116
104
  the messaging mechanisms and lets you manage message templates easily.
117
105
  email: blambeau@gmail.com
@@ -163,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
151
  version: '0'
164
152
  requirements: []
165
153
  rubyforge_project:
166
- rubygems_version: 2.5.2.3
154
+ rubygems_version: 2.6.11
167
155
  signing_key:
168
156
  specification_version: 4
169
157
  summary: Talktome helps you talk to users by email, messaging, sms, etc.