ahoy_email 0.1.2 → 0.1.3

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: 6ca8c4b534c78c9790f7f5d1a3c33138301b7123
4
- data.tar.gz: 6131456a218ea53314bed0a6837b6c3b0701b69b
3
+ metadata.gz: 8d7461d3588d651ab3d225a8a599d9d09fb04cec
4
+ data.tar.gz: d39ee0ffe62f926206113aa73fd3afe622fd535d
5
5
  SHA512:
6
- metadata.gz: 535f24ba504347ba5a86ecb78fbd837963d99cc50e88d396c6a9c703565964d2debd91e40a345cb4c3159d4a1c3c440d3dd9faf73869912f5ea2489dac941cfe
7
- data.tar.gz: 17e044817fe7a3860d14a62f079c9aaca38803671eee9f6412506422653f37474aca70091efcc8f6fc3a4451c5a1ddb24241231970277ce98bc88461439e2211
6
+ metadata.gz: 1fb90d1b9f2615095e6ece4c9228ca7ea37092844861aef591b45abea856885ba8a1304ab657b2fc8b5479ffa74f72fe9f94861a05d06e11b0c8c930fd7666b6
7
+ data.tar.gz: c34afcb5fff0ff0054e9c62b30a884f735b0aef4e7ce3aa7150c3d80469ff1bc5cb673de1e5c2c9a975ecb629b6448685c84115f7e000a1d47832fe41d5671cf
@@ -1,3 +1,8 @@
1
+ ## 0.1.3
2
+
3
+ - Added `to` and `mailer` fields
4
+ - Added subscribers for open and click events
5
+
1
6
  ## 0.1.2
2
7
 
3
8
  - Added `AhoyEmail.track` (fix)
data/README.md CHANGED
@@ -142,6 +142,30 @@ end
142
142
  AhoyEmail.track open: false
143
143
  ```
144
144
 
145
+ ## Events
146
+
147
+ Subscribe to open and click events. Create an initializer `config/initializers/ahoy_email.rb` with:
148
+
149
+ ```ruby
150
+ class EmailSubscriber
151
+
152
+ def open(event)
153
+ # :message and :controller keys
154
+ ahoy = event[:controller].ahoy
155
+ ahoy.track "Email opened", message_id: event[:message].id
156
+ end
157
+
158
+ def click(event)
159
+ # same keys as above, plus :url
160
+ ahoy = event[:controller].ahoy
161
+ ahoy.track "Email clicked", message_id: event[:message].id, url: event[:url]
162
+ end
163
+
164
+ end
165
+
166
+ AhoyEmail.subscribers << EmailSubscriber.new
167
+ ```
168
+
145
169
  ## Reference
146
170
 
147
171
  You can use a `Proc` for any option.
@@ -7,16 +7,19 @@ module Ahoy
7
7
  @message.opened_at = Time.now
8
8
  @message.save!
9
9
  end
10
+ publish :open
10
11
  send_data Base64.decode64("R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="), type: "image/gif", disposition: "inline"
11
12
  end
12
13
 
13
14
  def click
14
15
  if @message and !@message.clicked_at
15
16
  @message.clicked_at = Time.now
17
+ @message.opened_at ||= @message.clicked_at
16
18
  @message.save!
17
19
  end
18
20
  url = params[:url]
19
21
  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), AhoyEmail.secret_token, url)
22
+ publish :click, url: params[:url]
20
23
  if secure_compare(params[:signature], signature)
21
24
  redirect_to url
22
25
  else
@@ -30,6 +33,16 @@ module Ahoy
30
33
  @message = AhoyEmail.message_model.where(token: params[:id]).first
31
34
  end
32
35
 
36
+ def publish(name, event = {})
37
+ AhoyEmail.subscribers.each do |subscriber|
38
+ if subscriber.respond_to?(name)
39
+ event[:message] = @message
40
+ event[:controller] = self
41
+ subscriber.send name, event
42
+ end
43
+ end
44
+ end
45
+
33
46
  # from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/message_verifier.rb
34
47
  # constant-time comparison algorithm to prevent timing attacks
35
48
  def secure_compare(a, b)
@@ -9,7 +9,7 @@ require "ahoy_email/mailer"
9
9
  require "ahoy_email/engine"
10
10
 
11
11
  module AhoyEmail
12
- mattr_accessor :secret_token, :options
12
+ mattr_accessor :secret_token, :options, :subscribers
13
13
 
14
14
  self.options = {
15
15
  message: true,
@@ -21,9 +21,12 @@ module AhoyEmail
21
21
  utm_term: nil,
22
22
  utm_content: nil,
23
23
  utm_campaign: proc {|message, mailer| mailer.action_name },
24
- user: proc{|message, mailer| User.where(email: message.to.first).first rescue nil }
24
+ user: proc{|message, mailer| (message.to.size == 1 ? User.where(email: message.to.first).first : nil) rescue nil },
25
+ mailer: proc{|message, mailer| "#{mailer.class.name}##{mailer.action_name}" }
25
26
  }
26
27
 
28
+ self.subscribers = []
29
+
27
30
  def self.track(options)
28
31
  self.options = self.options.merge(options)
29
32
  end
@@ -13,14 +13,16 @@ module AhoyEmail
13
13
  if options[:message]
14
14
  @ahoy_message = AhoyEmail.message_model.new
15
15
  ahoy_message.token = generate_token
16
+ ahoy_message.to = message.to.join(", ") if ahoy_message.respond_to?(:to=)
16
17
  ahoy_message.user = options[:user]
17
18
 
18
19
  track_open if options[:open]
19
20
  track_links if options[:utm_params] or options[:click]
20
21
 
21
- # save
22
+ ahoy_message.mailer = options[:mailer] if ahoy_message.respond_to?(:mailer=)
22
23
  ahoy_message.subject = message.subject if ahoy_message.respond_to?(:subject=)
23
24
  ahoy_message.content = message.to_s if ahoy_message.respond_to?(:content=)
25
+
24
26
  ahoy_message.save
25
27
  message["Ahoy-Message-Id"] = ahoy_message.id
26
28
  end
@@ -1,3 +1,3 @@
1
1
  module AhoyEmail
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -4,11 +4,13 @@ class <%= migration_class_name %> < ActiveRecord::Migration
4
4
  t.string :token
5
5
 
6
6
  # user
7
+ t.text :to
7
8
  t.integer :user_id
8
9
  t.string :user_type
9
10
 
10
11
  # optional
11
12
  # feel free to remove
13
+ t.string :mailer
12
14
  t.text :subject
13
15
  t.text :content
14
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ahoy_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-02 00:00:00.000000000 Z
11
+ date: 2014-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails