mail-notify 0.0.1 → 0.0.2

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: b6b11dc10c2fb12b2ad57606156b3d2e555ee0f5ae47c1b5ed65aa5edda4f7e3
4
- data.tar.gz: 69d53a7e7cd2cb27f1a918247faa02945df6c07b57be1ff017418556cfdf9dd4
3
+ metadata.gz: 5a39b418b67958ee80a979be7f33d0367c9b122914d3640bfc65e59db8fd6ba8
4
+ data.tar.gz: 48fe8389d949853835b6f4cba7510cb1f2aea18c47899bb56bb2e52ddcc17eb4
5
5
  SHA512:
6
- metadata.gz: 771e26cd9314ea752907d9bf7e09e213581247c32582b1a4663be105f5c9a4aabec30797e4ad8cbd831b83c354b1e4ed4cbeafc7f79781f9ce9d17da80ce151c
7
- data.tar.gz: 14d36787c4dfab3458c31bdf3382fbfe760a82b78d4041ca48a9c5ccc9821534993dd68ae56da1826bff78ebba3dcf540b41920d6a2e32fcb5a9addd0d033e37
6
+ metadata.gz: 04e90ef052082fb609bd0e5ca3b680df6794d05a21b133c2ee268d4292ae1175eb35c7e2aaaff4085317a74ad413e06d3f7edb072d8f7c6e1e4b715d10582129
7
+ data.tar.gz: 8a874ac8124e9a1126d887fc5ba8d543ac999a30fa26c6ab3703c1f2e9a56aa6e6861b4c495f7fc7e64b7f523dad010d54b5e8353019968d00d9b7133f82c55c
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  .DS_Store
13
+ /Gemfile.lock
@@ -5,6 +5,8 @@ Style/Documentation:
5
5
  Exclude:
6
6
  - 'spec/**/*'
7
7
  - 'test/**/*'
8
- - 'lib/mail/notify.rb'
9
- - 'lib/mail/notify/delivery_method.rb'
10
- - 'lib/mail/notify/railtie.rb'
8
+ - 'lib/**/*'
9
+
10
+ Metrics/BlockLength:
11
+ Exclude:
12
+ - 'spec/**/*'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mail-notify (0.1.0)
4
+ mail-notify (0.0.2)
5
5
  actionmailer (~> 5.0)
6
6
  notifications-ruby-client (~> 2.0)
7
7
 
data/README.md CHANGED
@@ -20,8 +20,7 @@ Or install it yourself as:
20
20
 
21
21
  $ gem install mail-notify
22
22
 
23
- Then, add the following to your `config/environments/*.rb` (where * is `test`, `development`, `production` or
24
- whatever other environment(s) you have) file(s):
23
+ Then, add the following to your `config/environments/*.rb` (where * is `test`, `development`, `production` or whatever other environment(s) you have) file(s):
25
24
 
26
25
  ```ruby
27
26
  config.action_mailer.delivery_method = :notify
@@ -30,34 +29,54 @@ config.action_mailer.notify_settings = {
30
29
  }
31
30
  ```
32
31
 
33
- ## Usage
32
+ ### Mailers
34
33
 
35
- This gem assumes you have a very simple template set up in Notify, with a `((subject))` variable
36
- in the subject line, and a `((body))` variable in the body field, as below:
34
+ There are two options for using `Mail::Notify`, either templating in Rails with a view, or templating in Notify. Whichever way you choose, you'll need your mailers to inherit from `Mail::Notify::Mailer` like so:
37
35
 
38
- ![Example screenshot](docs/screenshot.png)
36
+ ```ruby
37
+ class MyMailer < Mail::Notify::Mailer
38
+ end
39
+ ```
39
40
 
40
- Support for further customisations may come further down the road.
41
+ #### With a view
41
42
 
42
- ### Mailers
43
+ Out of the box, Notify offers support for templating, with some rudimentary logic included. If you'd rather have your templating logic included with your source code for ease of access, or you want to do some more complex logic that's not supported by Notify, you can template your mailer views in erb.
44
+
45
+ For this to work with Notify, you'll need a very simple template set up in Notify, with a `((subject))` variable in the subject line, and a `((body))` variable in the body field, as below:
46
+
47
+ ![Example screenshot](docs/screenshot.png)
43
48
 
44
- Using mailers is pretty much identical to as if you were using any other action_mailer delivery method, the only difference is you'll need to add your `template_id` to your call to `mail`, eg:
49
+ Next, in your mailer you'll need to call `view_mail` with the first parameter being the ID of the notify template, followed by a hash of email headers e.g:
45
50
 
46
51
  ```ruby
47
- class MyMailer < ApplicationMailer
52
+ class MyMailer < Mail::Notify::Mailer
48
53
  def send_email
49
- mail(
54
+ notify_mail('YOUR_TEMPLATE_ID_GOES_HERE',
50
55
  to: 'mail@somewhere.com',
51
- subject: 'Subject line goes here',
52
- template_id: 'YOUR_TEMPLATE_ID_GOES_HERE'
56
+ subject: 'Subject line goes here'
53
57
  )
54
58
  end
55
59
  end
56
60
  ```
57
61
 
58
- ### Views
62
+ Your view can then be a simple `text.erb` file. You can add some markdown for headers, bullet points and links etc. These are handled in the same way as standard action_mailer views.
59
63
 
60
- Views should be simple `text.erb` files. You can add some markdown for headers, bullet points and links etc. These are handled in the same way as standard action_mailer views.
64
+ #### With Notify templating
65
+
66
+ You can also send your customisations in the more traditional way, and do your templating in Notify if you prefer. For this, you'll need to call `template_mail`, again with the first parameter being the ID of the template, and a hash of email headers, including your personalisations, e.g:
67
+
68
+ ```ruby
69
+ class MyMailer < Mail::Notify::Mailer
70
+ def send_email
71
+ template_mail('YOUR_TEMPLATE_ID_GOES_HERE',
72
+ to: 'mail@somewhere.com',
73
+ personalisations: {
74
+ foo: 'bar'
75
+ }
76
+ )
77
+ end
78
+ end
79
+ ```
61
80
 
62
81
  ## Development
63
82
 
@@ -5,6 +5,8 @@ require 'notifications/client'
5
5
  require 'mail/notify/version'
6
6
  require 'mail/notify/railtie' if defined? Rails
7
7
  require 'mail/notify/delivery_method'
8
+ require 'mail/notify/personalisation'
9
+ require 'mail/notify/mailer'
8
10
 
9
11
  module Mail
10
12
  module Notify
@@ -10,7 +10,9 @@ module Mail
10
10
  end
11
11
 
12
12
  def deliver!(mail)
13
- initialize_params(mail)
13
+ @mail = mail
14
+ @personalisation = Personalisation.new(@mail)
15
+ initialize_params
14
16
  send_email
15
17
  end
16
18
 
@@ -20,14 +22,11 @@ module Mail
20
22
  @client ||= Notifications::Client.new(@settings[:api_key])
21
23
  end
22
24
 
23
- def initialize_params(mail)
25
+ def initialize_params
24
26
  @email_params = {
25
- email_address: mail.to.first,
26
- template_id: mail[:template_id].to_s,
27
- personalisation: {
28
- body: mail.body.raw_source,
29
- subject: mail.subject
30
- }
27
+ email_address: @mail.to.first,
28
+ template_id: @mail[:template_id].to_s,
29
+ personalisation: @personalisation.to_h
31
30
  }
32
31
  end
33
32
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mail
4
+ module Notify
5
+ class Mailer < ActionMailer::Base
6
+ def view_mail(template_id, headers)
7
+ mail(headers.merge(template_id: template_id))
8
+ end
9
+
10
+ def template_mail(template_id, headers)
11
+ mail(headers.merge(body: '', subject: '', template_id: template_id))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mail
4
+ module Notify
5
+ class Personalisation
6
+ def initialize(mail)
7
+ @body = mail.body.raw_source
8
+ @subject = mail.subject
9
+ @personalisation = mail[:personalisation]&.unparsed_value || {}
10
+ end
11
+
12
+ def to_h
13
+ merged_options.reject { |_k, v| v.blank? }
14
+ end
15
+
16
+ private
17
+
18
+ def merged_options
19
+ {
20
+ body: @body,
21
+ subject: @subject
22
+ }.merge(@personalisation)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mail
4
4
  module Notify
5
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail-notify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Harrison
@@ -148,6 +148,8 @@ files:
148
148
  - docs/screenshot.png
149
149
  - lib/mail/notify.rb
150
150
  - lib/mail/notify/delivery_method.rb
151
+ - lib/mail/notify/mailer.rb
152
+ - lib/mail/notify/personalisation.rb
151
153
  - lib/mail/notify/railtie.rb
152
154
  - lib/mail/notify/version.rb
153
155
  - mail-notify.gemspec