devise_mailjet 0.1.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/MIT_LICENSE +13 -13
- data/README.rdoc +116 -116
- data/app/views/devise/registrations/new.html.erb +27 -27
- data/app/views/devise/shared/mailjet/_form.html.erb +3 -3
- data/lib/devise_mailjet/mailjet_list_api_mapper.rb +73 -71
- data/lib/devise_mailjet/mailjet_worker.rb +8 -0
- data/lib/devise_mailjet/model.rb +110 -106
- data/lib/devise_mailjet/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0170c835a71f38e16dd1aa21b8d9eef6deee068344fa1cfdd09fa6aa49491a39
|
4
|
+
data.tar.gz: 5c5ce8d6173c0ba2cb443cb3cbf25a26a1d19d239e8c5dddc0f9f57054e03086
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11e365b515b13a0f6b5b8253d15fc13fb439ce0a1c5b51efffdbf31f37ec02107884356f3c064c8b77ef62468621ddefe24e53660b4fef772e305b45970a324e
|
7
|
+
data.tar.gz: 40b07efaec0c221065cf00658b0a47cba848931e1b1d24c79be36ea58dd8e105b7f8e3e58657b2b36fe527c9cc015f9957efb184b65ce07ac6b8916e6727f761
|
data/MIT_LICENSE
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
Copyright (c) 2011 Justin Cunningham
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
4
|
-
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
5
|
-
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
6
|
-
persons to whom the Software is furnished to do so, subject to the following conditions:
|
7
|
-
|
8
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
9
|
-
Software.
|
10
|
-
|
11
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
12
|
-
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
13
|
-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
1
|
+
Copyright (c) 2011 Justin Cunningham
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
4
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
5
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
6
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
7
|
+
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
9
|
+
Software.
|
10
|
+
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
12
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
13
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
14
14
|
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -1,116 +1,116 @@
|
|
1
|
-
= Devise MailJet
|
2
|
-
|
3
|
-
Devise MailJet adds a MailJet option to {devise}[https://github.com/plataformatec/devise] that easily enables users
|
4
|
-
to join your mailing list when they create an account.
|
5
|
-
|
6
|
-
{Delayed Job}[https://github.com/collectiveidea/delayed_job] is used automatically if your project uses it, and the
|
7
|
-
mapping between list names and list ids is cached automatically.
|
8
|
-
|
9
|
-
== Getting started
|
10
|
-
|
11
|
-
In your Gemfile, add devise_mailjet after devise:
|
12
|
-
|
13
|
-
gem "devise"
|
14
|
-
gem "devise_mailjet" # Last officially released gem
|
15
|
-
|
16
|
-
In your User model, add :mailjet to the devise call and make :join_mailing_list accessible:
|
17
|
-
|
18
|
-
devise :database_authenticatable, ..., :mailjet
|
19
|
-
attr_accessor :join_mailing_list
|
20
|
-
|
21
|
-
In your mailjet initializer (config/initializers/mailjet.rb), set your API key and mailing list name:
|
22
|
-
|
23
|
-
Mailjet.configure do |config|
|
24
|
-
config.api_key = 'your_api_key'
|
25
|
-
config.secret_key = 'your_secret_key'
|
26
|
-
config.default_from = 'you@example.com'
|
27
|
-
end
|
28
|
-
|
29
|
-
In your device initializer (config/initializers/device.rb), you can configue Mailjet specifics:
|
30
|
-
|
31
|
-
Devise.setup do |config|
|
32
|
-
# other Devise config parameters...
|
33
|
-
config.mailing_list_name = 'List Name'
|
34
|
-
config.mailing_list_opt_in_by_default = false
|
35
|
-
end
|
36
|
-
|
37
|
-
If you are using the default Devise registration views, the Join Mailing List checkbox is added automatically, if not,
|
38
|
-
either include the form partial in your new registration form:
|
39
|
-
|
40
|
-
<%= render :partial => "devise/shared/mailjet/form", :locals => {:form => f} %>
|
41
|
-
|
42
|
-
Or manually add a "Join Mailing List" checkbox to your new registration form:
|
43
|
-
|
44
|
-
<%= form.check_box :join_mailing_list %>
|
45
|
-
|
46
|
-
If you are using Simple Form, you can use:
|
47
|
-
|
48
|
-
<%= f.input :join_mailing_list, :as => :boolean %>
|
49
|
-
|
50
|
-
== Configuration
|
51
|
-
|
52
|
-
Create an initializer, and set your MailJet API key. To generate a new API key, go to the account tab in your
|
53
|
-
MailJet account and select API Keys & Authorized Apps, then add a key.
|
54
|
-
|
55
|
-
Mailjet.configure do |config|
|
56
|
-
config.api_key = 'your_api_key'
|
57
|
-
config.secret_key = 'your_secret_key'
|
58
|
-
config.default_from = 'you@example.com'
|
59
|
-
end'
|
60
|
-
|
61
|
-
Create a mailing list, and set the mailing list name in the initializer. To create a MailJet list, from your account
|
62
|
-
go to the Lists tab, then hit create list.
|
63
|
-
|
64
|
-
Devise.mailing_list_name = 'List Name'
|
65
|
-
|
66
|
-
Add options from the {MailJet API Docs}[http://dev.mailjet.com/] using the following code in user.rb model. For GROUPINGS, you can get the Group ID by clicking "import to" and looking at the URL https://us6.admin.mailjet.com/lists/members/import?id=1234&grp=9999&int=1
|
67
|
-
|
68
|
-
def mailjet_list_subscribe_options
|
69
|
-
{'FNAME' => self.first_name, 'LNAME' => self.last_name, 'GROUPINGS'=> { 0 => {'id' => 9999, 'groups' => "Signed Up" } } }
|
70
|
-
end
|
71
|
-
|
72
|
-
For all the configuration settings, take a look at the {model documenation}[http://rubydoc.info/github/zedalaye/devise_mailjet/master/Devise/Models/Mailjet#].
|
73
|
-
|
74
|
-
== Documentation
|
75
|
-
|
76
|
-
Full documentation is available at {rdoc.info}[http://rdoc.info/github/zedalaye/devise_mailjet/master/frames].
|
77
|
-
|
78
|
-
== Demo Application
|
79
|
-
|
80
|
-
A demo application is available at {github}[https://github.com/zedalaye/devise_mailjet_demo].
|
81
|
-
|
82
|
-
== Example Usage
|
83
|
-
|
84
|
-
Users will join the default mailing list if join_mailing_list is set to true when the user is created. To manually add
|
85
|
-
a user:
|
86
|
-
|
87
|
-
User.find(1).add_to_mailjet_list('Site Administrators List')
|
88
|
-
|
89
|
-
To manually remove a user:
|
90
|
-
|
91
|
-
User.find(1).remove_from_mailjet_list('Site Administrators List')
|
92
|
-
|
93
|
-
NOTE: You MUST have the users permission to add them to a mailing list.
|
94
|
-
|
95
|
-
== Customization
|
96
|
-
|
97
|
-
To have the user join more than one list, or to override the lists that the user will join, override
|
98
|
-
mailjet_lists_to_join in your model. Your method should return a single list, or an array of lists.
|
99
|
-
|
100
|
-
def mailjet_lists_to_join
|
101
|
-
lists = ["Site Users List"]
|
102
|
-
lists << "Site Admins List" if admin?
|
103
|
-
return lists
|
104
|
-
end
|
105
|
-
|
106
|
-
If all users will join the same list or lists, just set the mailing_list_name configuration option.
|
107
|
-
|
108
|
-
== Contributions
|
109
|
-
|
110
|
-
Please help this software improve by submitting pull requests, preferably with tests.
|
111
|
-
|
112
|
-
View our {contributors}[https://github.com/zedalaye/devise_mailjet/contributors].
|
113
|
-
|
114
|
-
== Copyright
|
115
|
-
|
116
|
-
Copyright (c) 2011 {Justin Cunningham}[http://littlebitofcode.com] and 2014 {Pierre Yager}[http://levosgien.net]. See MIT_LICENSE for details.
|
1
|
+
= Devise MailJet
|
2
|
+
|
3
|
+
Devise MailJet adds a MailJet option to {devise}[https://github.com/plataformatec/devise] that easily enables users
|
4
|
+
to join your mailing list when they create an account.
|
5
|
+
|
6
|
+
{Delayed Job}[https://github.com/collectiveidea/delayed_job] is used automatically if your project uses it, and the
|
7
|
+
mapping between list names and list ids is cached automatically.
|
8
|
+
|
9
|
+
== Getting started
|
10
|
+
|
11
|
+
In your Gemfile, add devise_mailjet after devise:
|
12
|
+
|
13
|
+
gem "devise"
|
14
|
+
gem "devise_mailjet" # Last officially released gem
|
15
|
+
|
16
|
+
In your User model, add :mailjet to the devise call and make :join_mailing_list accessible:
|
17
|
+
|
18
|
+
devise :database_authenticatable, ..., :mailjet
|
19
|
+
attr_accessor :join_mailing_list
|
20
|
+
|
21
|
+
In your mailjet initializer (config/initializers/mailjet.rb), set your API key and mailing list name:
|
22
|
+
|
23
|
+
Mailjet.configure do |config|
|
24
|
+
config.api_key = 'your_api_key'
|
25
|
+
config.secret_key = 'your_secret_key'
|
26
|
+
config.default_from = 'you@example.com'
|
27
|
+
end
|
28
|
+
|
29
|
+
In your device initializer (config/initializers/device.rb), you can configue Mailjet specifics:
|
30
|
+
|
31
|
+
Devise.setup do |config|
|
32
|
+
# other Devise config parameters...
|
33
|
+
config.mailing_list_name = 'List Name'
|
34
|
+
config.mailing_list_opt_in_by_default = false
|
35
|
+
end
|
36
|
+
|
37
|
+
If you are using the default Devise registration views, the Join Mailing List checkbox is added automatically, if not,
|
38
|
+
either include the form partial in your new registration form:
|
39
|
+
|
40
|
+
<%= render :partial => "devise/shared/mailjet/form", :locals => {:form => f} %>
|
41
|
+
|
42
|
+
Or manually add a "Join Mailing List" checkbox to your new registration form:
|
43
|
+
|
44
|
+
<%= form.check_box :join_mailing_list %>
|
45
|
+
|
46
|
+
If you are using Simple Form, you can use:
|
47
|
+
|
48
|
+
<%= f.input :join_mailing_list, :as => :boolean %>
|
49
|
+
|
50
|
+
== Configuration
|
51
|
+
|
52
|
+
Create an initializer, and set your MailJet API key. To generate a new API key, go to the account tab in your
|
53
|
+
MailJet account and select API Keys & Authorized Apps, then add a key.
|
54
|
+
|
55
|
+
Mailjet.configure do |config|
|
56
|
+
config.api_key = 'your_api_key'
|
57
|
+
config.secret_key = 'your_secret_key'
|
58
|
+
config.default_from = 'you@example.com'
|
59
|
+
end'
|
60
|
+
|
61
|
+
Create a mailing list, and set the mailing list name in the initializer. To create a MailJet list, from your account
|
62
|
+
go to the Lists tab, then hit create list.
|
63
|
+
|
64
|
+
Devise.mailing_list_name = 'List Name'
|
65
|
+
|
66
|
+
Add options from the {MailJet API Docs}[http://dev.mailjet.com/] using the following code in user.rb model. For GROUPINGS, you can get the Group ID by clicking "import to" and looking at the URL https://us6.admin.mailjet.com/lists/members/import?id=1234&grp=9999&int=1
|
67
|
+
|
68
|
+
def mailjet_list_subscribe_options
|
69
|
+
{'FNAME' => self.first_name, 'LNAME' => self.last_name, 'GROUPINGS'=> { 0 => {'id' => 9999, 'groups' => "Signed Up" } } }
|
70
|
+
end
|
71
|
+
|
72
|
+
For all the configuration settings, take a look at the {model documenation}[http://rubydoc.info/github/zedalaye/devise_mailjet/master/Devise/Models/Mailjet#].
|
73
|
+
|
74
|
+
== Documentation
|
75
|
+
|
76
|
+
Full documentation is available at {rdoc.info}[http://rdoc.info/github/zedalaye/devise_mailjet/master/frames].
|
77
|
+
|
78
|
+
== Demo Application
|
79
|
+
|
80
|
+
A demo application is available at {github}[https://github.com/zedalaye/devise_mailjet_demo].
|
81
|
+
|
82
|
+
== Example Usage
|
83
|
+
|
84
|
+
Users will join the default mailing list if join_mailing_list is set to true when the user is created. To manually add
|
85
|
+
a user:
|
86
|
+
|
87
|
+
User.find(1).add_to_mailjet_list('Site Administrators List')
|
88
|
+
|
89
|
+
To manually remove a user:
|
90
|
+
|
91
|
+
User.find(1).remove_from_mailjet_list('Site Administrators List')
|
92
|
+
|
93
|
+
NOTE: You MUST have the users permission to add them to a mailing list.
|
94
|
+
|
95
|
+
== Customization
|
96
|
+
|
97
|
+
To have the user join more than one list, or to override the lists that the user will join, override
|
98
|
+
mailjet_lists_to_join in your model. Your method should return a single list, or an array of lists.
|
99
|
+
|
100
|
+
def mailjet_lists_to_join
|
101
|
+
lists = ["Site Users List"]
|
102
|
+
lists << "Site Admins List" if admin?
|
103
|
+
return lists
|
104
|
+
end
|
105
|
+
|
106
|
+
If all users will join the same list or lists, just set the mailing_list_name configuration option.
|
107
|
+
|
108
|
+
== Contributions
|
109
|
+
|
110
|
+
Please help this software improve by submitting pull requests, preferably with tests.
|
111
|
+
|
112
|
+
View our {contributors}[https://github.com/zedalaye/devise_mailjet/contributors].
|
113
|
+
|
114
|
+
== Copyright
|
115
|
+
|
116
|
+
Copyright (c) 2011 {Justin Cunningham}[http://littlebitofcode.com] and 2014 {Pierre Yager}[http://levosgien.net]. See MIT_LICENSE for details.
|
@@ -1,28 +1,28 @@
|
|
1
|
-
<%= bootstrap_devise_error_messages! %>
|
2
|
-
<div class="panel panel-default">
|
3
|
-
<div class="panel-heading">
|
4
|
-
<h4><%= t('.sign_up', :default => "Sign up") %></h4>
|
5
|
-
</div>
|
6
|
-
<div class="panel-body">
|
7
|
-
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { role: "form" }) do |f| %>
|
8
|
-
<div class="form-group">
|
9
|
-
<%= f.label :email %>
|
10
|
-
<%= f.email_field :email, class: "form-control" %>
|
11
|
-
</div>
|
12
|
-
<div class="form-group">
|
13
|
-
<%= f.label :password %><br />
|
14
|
-
<%= f.password_field :password, class: "form-control" %>
|
15
|
-
</div>
|
16
|
-
<div class="form-group">
|
17
|
-
<%= f.label :password_confirmation %>
|
18
|
-
<%= f.password_field :password_confirmation, class: "form-control" %>
|
19
|
-
</div>
|
20
|
-
|
21
|
-
<%= render :partial => "devise/shared/mailjet/form", :locals => {:form => f} %>
|
22
|
-
|
23
|
-
<%= f.submit t('.sign_up', :default => "Sign up"), class: "btn btn-primary" %>
|
24
|
-
<% end %>
|
25
|
-
</div>
|
26
|
-
</div>
|
27
|
-
|
1
|
+
<%= bootstrap_devise_error_messages! %>
|
2
|
+
<div class="panel panel-default">
|
3
|
+
<div class="panel-heading">
|
4
|
+
<h4><%= t('.sign_up', :default => "Sign up") %></h4>
|
5
|
+
</div>
|
6
|
+
<div class="panel-body">
|
7
|
+
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { role: "form" }) do |f| %>
|
8
|
+
<div class="form-group">
|
9
|
+
<%= f.label :email %>
|
10
|
+
<%= f.email_field :email, class: "form-control" %>
|
11
|
+
</div>
|
12
|
+
<div class="form-group">
|
13
|
+
<%= f.label :password %><br />
|
14
|
+
<%= f.password_field :password, class: "form-control" %>
|
15
|
+
</div>
|
16
|
+
<div class="form-group">
|
17
|
+
<%= f.label :password_confirmation %>
|
18
|
+
<%= f.password_field :password_confirmation, class: "form-control" %>
|
19
|
+
</div>
|
20
|
+
|
21
|
+
<%= render :partial => "devise/shared/mailjet/form", :locals => {:form => f} %>
|
22
|
+
|
23
|
+
<%= f.submit t('.sign_up', :default => "Sign up"), class: "btn btn-primary" %>
|
24
|
+
<% end %>
|
25
|
+
</div>
|
26
|
+
</div>
|
27
|
+
|
28
28
|
<%= render "devise/shared/links" %>
|
@@ -1,4 +1,4 @@
|
|
1
|
-
<div class="form-group">
|
2
|
-
<%= form.label :join_mailing_list %>
|
3
|
-
<%= form.check_box :join_mailing_list %>
|
1
|
+
<div class="form-group">
|
2
|
+
<%= form.label :join_mailing_list %>
|
3
|
+
<%= form.check_box :join_mailing_list %>
|
4
4
|
</div>
|
@@ -1,71 +1,73 @@
|
|
1
|
-
require 'mailjet'
|
2
|
-
|
3
|
-
module Devise
|
4
|
-
module Models
|
5
|
-
module Mailjet
|
6
|
-
class MailjetListApiMapper
|
7
|
-
# find the list using the mailjet API and save it to a memory cache
|
8
|
-
def list_name_to_id(list_name)
|
9
|
-
@lists ||= {}
|
10
|
-
unless @lists.has_key?(list_name)
|
11
|
-
l = ::Mailjet::Contactslist.all(name: list_name, limit: 1).first
|
12
|
-
l = ::Mailjet::Contactslist.create(name: list_name) unless l && l.name == list_name
|
13
|
-
@lists[list_name] = l.id
|
14
|
-
end
|
15
|
-
@lists[list_name]
|
16
|
-
end
|
17
|
-
|
18
|
-
# find the contact using the mailjet API and save it to a memory cache
|
19
|
-
def contact_email_to_id(email)
|
20
|
-
@contacts ||= {}
|
21
|
-
unless @contacts.has_key?(email)
|
22
|
-
c = ::Mailjet::Contact.find(email) # email is a valid key for finding contact resources
|
23
|
-
c = ::Mailjet::Contact.create(email: email) unless c && c.email == email
|
24
|
-
@contacts[email] = c.id
|
25
|
-
end
|
26
|
-
@contacts[email]
|
27
|
-
end
|
28
|
-
|
29
|
-
# subscribes the user to the named mailing list(s). list_names can be the name of one list, or an array of
|
30
|
-
# several.
|
31
|
-
#
|
32
|
-
# NOTE: Do not use this method unless the user has opted in.
|
33
|
-
def subscribe_to_lists(list_names, email)
|
34
|
-
walk_recipients(list_names, email) do |lr, list_id, contact_id|
|
35
|
-
if lr.nil?
|
36
|
-
::Mailjet::Listrecipient.create(
|
37
|
-
elsif lr.is_unsubscribed
|
38
|
-
lr.update_attributes(is_unsubscribed: false,
|
39
|
-
end
|
40
|
-
end
|
41
|
-
rescue ::Mailjet::ApiError
|
42
|
-
# ignore
|
43
|
-
end
|
44
|
-
|
45
|
-
# unsubscribe the user from the named mailing list(s). list_names can be the name of one list, or an array of
|
46
|
-
# several.
|
47
|
-
def unsubscribe_from_lists(list_names, email)
|
48
|
-
walk_recipients(list_names, email) do |lr,
|
49
|
-
lr.update_attributes(is_unsubscribed: true,
|
50
|
-
end
|
51
|
-
rescue ::Mailjet::ApiError
|
52
|
-
# ignore
|
53
|
-
end
|
54
|
-
|
55
|
-
class ListLookupError < RuntimeError; end
|
56
|
-
|
57
|
-
private
|
58
|
-
|
59
|
-
def walk_recipients(list_names, email)
|
60
|
-
contact_id = contact_email_to_id(email)
|
61
|
-
list_names
|
62
|
-
|
63
|
-
|
64
|
-
lr = ::Mailjet::Listrecipient.all(
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
1
|
+
require 'mailjet'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Models
|
5
|
+
module Mailjet
|
6
|
+
class MailjetListApiMapper
|
7
|
+
# find the list using the mailjet API and save it to a memory cache
|
8
|
+
def list_name_to_id(list_name)
|
9
|
+
@lists ||= {}
|
10
|
+
unless @lists.has_key?(list_name)
|
11
|
+
l = ::Mailjet::Contactslist.all(name: list_name, limit: 1).first
|
12
|
+
l = ::Mailjet::Contactslist.create(name: list_name) unless l && l.name == list_name
|
13
|
+
@lists[list_name] = l.id
|
14
|
+
end
|
15
|
+
@lists[list_name]
|
16
|
+
end
|
17
|
+
|
18
|
+
# find the contact using the mailjet API and save it to a memory cache
|
19
|
+
def contact_email_to_id(email)
|
20
|
+
@contacts ||= {}
|
21
|
+
unless @contacts.has_key?(email)
|
22
|
+
c = ::Mailjet::Contact.find(email) # email is a valid key for finding contact resources
|
23
|
+
c = ::Mailjet::Contact.create(email: email) unless c && c.email == email
|
24
|
+
@contacts[email] = c.id
|
25
|
+
end
|
26
|
+
@contacts[email]
|
27
|
+
end
|
28
|
+
|
29
|
+
# subscribes the user to the named mailing list(s). list_names can be the name of one list, or an array of
|
30
|
+
# several.
|
31
|
+
#
|
32
|
+
# NOTE: Do not use this method unless the user has opted in.
|
33
|
+
def subscribe_to_lists(list_names, email)
|
34
|
+
walk_recipients(list_names, email) do |lr, list_id, contact_id|
|
35
|
+
if lr.nil?
|
36
|
+
::Mailjet::Listrecipient.create(list_id: list_id, contact_id: contact_id, is_unsubscribed: 'false')
|
37
|
+
elsif lr.is_unsubscribed
|
38
|
+
lr.update_attributes(is_unsubscribed: 'false', unsubscribed_at: nil)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
rescue ::Mailjet::ApiError
|
42
|
+
# ignore
|
43
|
+
end
|
44
|
+
|
45
|
+
# unsubscribe the user from the named mailing list(s). list_names can be the name of one list, or an array of
|
46
|
+
# several.
|
47
|
+
def unsubscribe_from_lists(list_names, email)
|
48
|
+
walk_recipients(list_names, email) do |lr, _l, _c|
|
49
|
+
lr.update_attributes(is_unsubscribed: 'true', unsubscribed_at: nil) if lr && !lr.is_unsubscribed
|
50
|
+
end
|
51
|
+
rescue ::Mailjet::ApiError
|
52
|
+
# ignore
|
53
|
+
end
|
54
|
+
|
55
|
+
class ListLookupError < RuntimeError; end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def walk_recipients(list_names, email)
|
60
|
+
contact_id = contact_email_to_id(email)
|
61
|
+
Array(list_names).each do |list_name|
|
62
|
+
list_id = list_name_to_id(list_name)
|
63
|
+
# Beware: [GET] parameters are not the same than [POST/PUT] parameters
|
64
|
+
lr = ::Mailjet::Listrecipient.all(contacts_list: list_id, contact: contact_id, limit: 1).first
|
65
|
+
# Make sure the API returned the record we were looking for
|
66
|
+
lr = nil unless lr.list_id == list_id && lr.contact_id == contact_id
|
67
|
+
yield lr, list_id, contact_id if block_given?
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -7,12 +7,20 @@ module Devise
|
|
7
7
|
def perform(action, list_names, email, config)
|
8
8
|
if config.is_a?(Hash)
|
9
9
|
::Mailjet.configure do |c|
|
10
|
+
c.api_version = 'v3'
|
10
11
|
c.api_key = config['api_key']
|
11
12
|
c.secret_key = config['secret_key']
|
12
13
|
c.default_from = config['default_from']
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
17
|
+
## Uncomment to debug
|
18
|
+
# RestClient.log = Object.new.tap do |proxy|
|
19
|
+
# def proxy.<<(message)
|
20
|
+
# Sidekiq.logger.info message
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
|
16
24
|
mapper = MailjetListApiMapper.new
|
17
25
|
if action == 'subscribe'
|
18
26
|
mapper.subscribe_to_lists(list_names, email)
|
data/lib/devise_mailjet/model.rb
CHANGED
@@ -1,106 +1,110 @@
|
|
1
|
-
module Devise
|
2
|
-
module Models
|
3
|
-
# Mailjet is responsible for joining users to mailjet lists when the create accounts with devise
|
4
|
-
# When a user is created, and join_mailing_list is set to true, they will automatically be added to one or more
|
5
|
-
# mailing lists returned by mailjet_lists_to_join.
|
6
|
-
#
|
7
|
-
# Configuration
|
8
|
-
#
|
9
|
-
# mailing_list_name: Default mailing list for user to join. This can be an array of strings, or just one string.
|
10
|
-
# By default, this is "Site List". If this will be configurable for each user, override
|
11
|
-
# mailjet_lists_to_join returning the list name or an array of list names for the user to
|
12
|
-
# join.
|
13
|
-
#
|
14
|
-
# mailing_list_opt_in_by_default: Determines if the checkbox for the user to opt-in to the mailing list should
|
15
|
-
# be checked by default, or not. Defaults to true.
|
16
|
-
#
|
17
|
-
# Examples:
|
18
|
-
#
|
19
|
-
# User.find(1).add_to_mailjet_list('Site Administrators List')
|
20
|
-
# User.find(1).remove_from_mailjet_list('Site Administrators List')
|
21
|
-
#
|
22
|
-
# u = User.new
|
23
|
-
# u.join_mailing_list = true
|
24
|
-
# u.save
|
25
|
-
module Mailjet
|
26
|
-
extend ActiveSupport::Concern
|
27
|
-
|
28
|
-
included do
|
29
|
-
after_create :commit_mailing_list_join
|
30
|
-
after_update :commit_mailing_list_join
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.required_fields(klass)
|
34
|
-
[ :join_mailing_list ]
|
35
|
-
end
|
36
|
-
|
37
|
-
# Set this to true to have the user automatically join the mailjet_lists_to_join
|
38
|
-
def join_mailing_list=(join)
|
39
|
-
join.downcase! if join.is_a?(String)
|
40
|
-
write_attribute(:join_mailing_list, ['yes','true',true,'1',1].include?(join))
|
41
|
-
end
|
42
|
-
|
43
|
-
def join_mailing_list
|
44
|
-
(new_record?) ? self.class.mailing_list_opt_in_by_default : read_attribute(:join_mailing_list)
|
45
|
-
end
|
46
|
-
|
47
|
-
# The mailing list or lists the user will join
|
48
|
-
# Should return
|
49
|
-
# configuration option.
|
50
|
-
# your model.
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
mapper.
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
1
|
+
module Devise
|
2
|
+
module Models
|
3
|
+
# Mailjet is responsible for joining users to mailjet lists when the create accounts with devise
|
4
|
+
# When a user is created, and join_mailing_list is set to true, they will automatically be added to one or more
|
5
|
+
# mailing lists returned by mailjet_lists_to_join.
|
6
|
+
#
|
7
|
+
# Configuration
|
8
|
+
#
|
9
|
+
# mailing_list_name: Default mailing list for user to join. This can be an array of strings, or just one string.
|
10
|
+
# By default, this is "Site List". If this will be configurable for each user, override
|
11
|
+
# mailjet_lists_to_join returning the list name or an array of list names for the user to
|
12
|
+
# join.
|
13
|
+
#
|
14
|
+
# mailing_list_opt_in_by_default: Determines if the checkbox for the user to opt-in to the mailing list should
|
15
|
+
# be checked by default, or not. Defaults to true.
|
16
|
+
#
|
17
|
+
# Examples:
|
18
|
+
#
|
19
|
+
# User.find(1).add_to_mailjet_list('Site Administrators List')
|
20
|
+
# User.find(1).remove_from_mailjet_list('Site Administrators List')
|
21
|
+
#
|
22
|
+
# u = User.new
|
23
|
+
# u.join_mailing_list = true
|
24
|
+
# u.save
|
25
|
+
module Mailjet
|
26
|
+
extend ActiveSupport::Concern
|
27
|
+
|
28
|
+
included do
|
29
|
+
after_create :commit_mailing_list_join
|
30
|
+
after_update :commit_mailing_list_join
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.required_fields(klass)
|
34
|
+
[ :join_mailing_list ]
|
35
|
+
end
|
36
|
+
|
37
|
+
# Set this to true to have the user automatically join the mailjet_lists_to_join
|
38
|
+
def join_mailing_list=(join)
|
39
|
+
join.downcase! if join.is_a?(String)
|
40
|
+
write_attribute(:join_mailing_list, ['yes', 'true', true, '1', 1].include?(join))
|
41
|
+
end
|
42
|
+
|
43
|
+
def join_mailing_list
|
44
|
+
(new_record?) ? self.class.mailing_list_opt_in_by_default : read_attribute(:join_mailing_list)
|
45
|
+
end
|
46
|
+
|
47
|
+
# The mailing list or lists the user will join
|
48
|
+
# Should return nil, a single string or an array of strings.
|
49
|
+
# By default, returns the mailing_list_name configuration option. If you want to customize the lists based on
|
50
|
+
# other information, override this method in your model.
|
51
|
+
# Returning nil disables the (un)subscription to the mailing lists
|
52
|
+
def mailjet_lists_to_join
|
53
|
+
self.class.mailing_list_name
|
54
|
+
end
|
55
|
+
|
56
|
+
# Add the user to the mailjet list with the specified name
|
57
|
+
def add_to_mailjet_list(list_name)
|
58
|
+
if defined?(Sidekiq::Worker)
|
59
|
+
MailjetWorker.perform_async(:subscribe, list_name, self.email, mailjet_config)
|
60
|
+
else
|
61
|
+
mapper = mailjet_list_mapper.respond_to?(:delay) ? mailjet_list_mapper.delay : mailjet_list_mapper
|
62
|
+
# options = self.respond_to?(:mailjet_list_subscribe_options) ? mailjet_list_subscribe_options : {}
|
63
|
+
mapper.subscribe_to_lists(list_name, self.email)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# remove the user from the mailjet list with the specified name
|
68
|
+
def remove_from_mailjet_list(list_name)
|
69
|
+
if defined?(Sidekiq::Worker)
|
70
|
+
MailjetWorker.perform_async(:unsubscribe, list_name, self.email, mailjet_config)
|
71
|
+
else
|
72
|
+
mapper = mailjet_list_mapper.respond_to?(:delay) ? mailjet_list_mapper.delay : mailjet_list_mapper
|
73
|
+
mapper.unsubscribe_from_lists(list_name, self.email)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Commit the user to the mailing list if they have selected to join
|
78
|
+
def commit_mailing_list_join
|
79
|
+
lists = mailjet_lists_to_join
|
80
|
+
return if Array(lists).empty?
|
81
|
+
|
82
|
+
if self.join_mailing_list
|
83
|
+
add_to_mailjet_list(lists)
|
84
|
+
else
|
85
|
+
remove_from_mailjet_list(lists)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# mapper that helps convert list names to mailjet ids
|
90
|
+
def mailjet_list_mapper
|
91
|
+
@@mailjet_list_api_mapper ||= MailjetListApiMapper.new
|
92
|
+
end
|
93
|
+
|
94
|
+
module ClassMethods
|
95
|
+
Devise::Models.config(self, :mailing_list_name)
|
96
|
+
Devise::Models.config(self, :mailing_list_opt_in_by_default)
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def mailjet_config
|
102
|
+
{
|
103
|
+
'api_key' => ::Mailjet.config.api_key,
|
104
|
+
'secret_key' => ::Mailjet.config.secret_key,
|
105
|
+
'default_from' => ::Mailjet.config.default_from
|
106
|
+
}
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise_mailjet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Cunningham
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-05-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
|
-
rubygems_version: 3.1.
|
111
|
+
rubygems_version: 3.1.6
|
112
112
|
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: Easy MailJet integration for Devise
|