devise_mailjet 0.1.0 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/MIT_LICENSE +13 -13
- data/README.rdoc +116 -102
- data/app/views/devise/registrations/new.html.erb +27 -27
- data/app/views/devise/shared/mailjet/_form.html.erb +3 -3
- data/devise_mailjet.gemspec +5 -7
- 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 +12 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d88e4905cc4fd26d04d667f52e0303a8c3bd503328f6bf25a1bfcf423bca5ab8
|
4
|
+
data.tar.gz: 261f22143e7f1b488b61a290a2d3dd24fe16e76575d4c44f7d402c3a39ba39df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2de692758a1cbea975bd47bce0fdabfa7cabe2dcd99e557ef32470d57f9feae3498476bedbe6b9d48107ec0b4050475731875e5e533edc3932f2883540d1de7
|
7
|
+
data.tar.gz: b7084e1472a6f273ab5c487b38ae895037bfa60f2f140ab5e30b284637496e9d23a5fe3c76fe1601ed72c985dfcf7bc8f9d055bd1b0eff901521283b515d0040
|
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,102 +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
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
-
|
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>
|
data/devise_mailjet.gemspec
CHANGED
@@ -7,23 +7,21 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = DeviseMailjet::VERSION
|
8
8
|
s.authors = ["Justin Cunningham", "Pierre Yager"]
|
9
9
|
s.email = ["justin@compucatedsolutions.com", "pierre@levosgien.net"]
|
10
|
-
s.homepage = "
|
10
|
+
s.homepage = "https://github.com/zedalaye/devise_mailjet"
|
11
11
|
s.summary = %q{Easy MailJet integration for Devise}
|
12
12
|
s.description = %q{Devise MailJet adds a MailJet option to devise that easily enables users to join your mailing list when they create an account.}
|
13
13
|
s.licenses = 'MIT'
|
14
14
|
|
15
|
-
s.rubyforge_project = "devise_mailjet"
|
16
|
-
|
17
15
|
s.files = `git ls-files`.split("\n")
|
18
16
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
18
|
s.require_paths = ["lib"]
|
21
19
|
|
22
20
|
{
|
23
|
-
'rails' => '<
|
24
|
-
'devise' => '< 5
|
25
|
-
'devise-bootstrap-views' => '< 1
|
26
|
-
'mailjet' => '< 2
|
21
|
+
'rails' => '< 7',
|
22
|
+
'devise' => '< 5',
|
23
|
+
'devise-bootstrap-views' => '< 1',
|
24
|
+
'mailjet' => '< 2'
|
27
25
|
}.each do |lib, version|
|
28
26
|
s.add_runtime_dependency(lib, *version)
|
29
27
|
end
|
@@ -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 && 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.6
|
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-07-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -17,56 +17,56 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "<"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '7'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "<"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
27
|
+
version: '7'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: devise
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - "<"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '5
|
34
|
+
version: '5'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "<"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '5
|
41
|
+
version: '5'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: devise-bootstrap-views
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
46
|
- - "<"
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: '1
|
48
|
+
version: '1'
|
49
49
|
type: :runtime
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
53
|
- - "<"
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version: '1
|
55
|
+
version: '1'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: mailjet
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
60
|
- - "<"
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version: '2
|
62
|
+
version: '2'
|
63
63
|
type: :runtime
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
67
|
- - "<"
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version: '2
|
69
|
+
version: '2'
|
70
70
|
description: Devise MailJet adds a MailJet option to devise that easily enables users
|
71
71
|
to join your mailing list when they create an account.
|
72
72
|
email:
|
@@ -89,7 +89,7 @@ files:
|
|
89
89
|
- lib/devise_mailjet/mailjet_worker.rb
|
90
90
|
- lib/devise_mailjet/model.rb
|
91
91
|
- lib/devise_mailjet/version.rb
|
92
|
-
homepage:
|
92
|
+
homepage: https://github.com/zedalaye/devise_mailjet
|
93
93
|
licenses:
|
94
94
|
- MIT
|
95
95
|
metadata: {}
|
@@ -108,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
|
-
|
112
|
-
rubygems_version: 2.6.14
|
111
|
+
rubygems_version: 3.2.20
|
113
112
|
signing_key:
|
114
113
|
specification_version: 4
|
115
114
|
summary: Easy MailJet integration for Devise
|