devise_mailjet 0.0.5 → 0.2.5

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
- SHA1:
3
- metadata.gz: 2e54b0e5430f9fd1e5c9fedc92190caffbe4adfb
4
- data.tar.gz: f3964d2f000ff3bcad2f68da8676b02608a1137f
2
+ SHA256:
3
+ metadata.gz: 5e022a3ce24f32f25f5c79ac63d21f7a07a84fb62a6c8ab65c4795aa358d556d
4
+ data.tar.gz: caefb3f78f7fdb9ebff1c7c546a85c3111212ea8ce6b9be8dbc39b06eb1f91f6
5
5
  SHA512:
6
- metadata.gz: cd182ae9e33c74538caba2d7cc94594b95d2efdbf456720cf2385c746314275e9bae02bf7f71a7f009beaad98683283254b44ec726a7c8b3fffbdb52444b12cb
7
- data.tar.gz: eb1f2aecffd249046e3704765a90ac5cf635e4bb92508e0ff81c26012cd18d037dd2aaa19a36573852be3d5fcbdeef74be0ed768bf6468658d58bcec6ef926c7
6
+ metadata.gz: 942e1be3abb33a6568bd890cd3f045b024ec2b31e3444302960db6f4dd5fa2d10bc4717caef642422dc9147ad2d24fb58fd05eeb5f7d67504138aa4e3b5ac6ff
7
+ data.tar.gz: d97fd5b201f2035cd320dbe41e379deb0e71b9fb5b8cce859a97f932e4a2f0961bf9cf22cbc9157c87931f7da9b1f7f29c71ba50e7aa5c1c916175a883849274
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,103 +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_accessible :join_mailing_list
20
-
21
- In your devise initializer (config/initializers/devise.rb), set your API key and mailing list name:
22
-
23
- Devise.mailjet_api_key = 'your_api_key'
24
- Devise.mailing_list_name = 'List Name'
25
- Devise.double_opt_in = false
26
- Devise.send_welcome_email = false
27
-
28
- If you are using the default Devise registration views, the Join Mailing List checkbox is added automatically, if not,
29
- either include the form partial in your new registration form:
30
-
31
- <%= render :partial => "devise/shared/mailjet/form", :locals => {:form => f} %>
32
-
33
- Or manually add a "Join Mailing List" checkbox to your new registration form:
34
-
35
- <%= form.check_box :join_mailing_list %>
36
-
37
- If you are using Simple Form, you can use:
38
-
39
- <%= f.input :join_mailing_list, :as => :boolean %>
40
-
41
- == Configuration
42
-
43
- Create an initializer, and set your MailJet API key. To generate a new API key, go to the account tab in your
44
- MailJet account and select API Keys & Authorized Apps, then add a key.
45
-
46
- Devise.mailjet_api_key = 'your_api_key'
47
-
48
- Create a mailing list, and set the mailing list name in the initializer. To create a MailJet list, from your account
49
- go to the Lists tab, then hit create list.
50
-
51
- Devise.mailing_list_name = 'List Name'
52
-
53
- 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
54
-
55
- def mailjet_list_subscribe_options
56
- {'FNAME' => self.first_name, 'LNAME' => self.last_name, 'GROUPINGS'=> { 0 => {'id' => 9999, 'groups' => "Signed Up" } } }
57
- end
58
-
59
- For all the configuration settings, take a look at the {model documenation}[http://rubydoc.info/github/zedalaye/devise_mailjet/master/Devise/Models/Mailjet#].
60
-
61
- == Documentation
62
-
63
- Full documentation is available at {rdoc.info}[http://rdoc.info/github/zedalaye/devise_mailjet/master/frames].
64
-
65
- == Demo Application
66
-
67
- A demo application is available at {github}[https://github.com/zedalaye/devise_mailjet_demo].
68
-
69
- == Example Usage
70
-
71
- Users will join the default mailing list if join_mailing_list is set to true when the user is created. To manually add
72
- a user:
73
-
74
- User.find(1).add_to_mailjet_list('Site Administrators List')
75
-
76
- To manually remove a user:
77
-
78
- User.find(1).remove_from_mailjet_list('Site Administrators List')
79
-
80
- NOTE: You MUST have the users permission to add them to a mailing list.
81
-
82
- == Customization
83
-
84
- To have the user join more than one list, or to override the lists that the user will join, override
85
- mailjet_lists_to_join in your model. Your method should return a single list, or an array of lists.
86
-
87
- def mailjet_lists_to_join
88
- lists = ["Site Users List"]
89
- lists << "Site Admins List" if admin?
90
- return lists
91
- end
92
-
93
- If all users will join the same list or lists, just set the mailing_list_name configuration option.
94
-
95
- == Contributions
96
-
97
- Please help this software improve by submitting pull requests, preferably with tests.
98
-
99
- View our {contributors}[https://github.com/zedalaye/devise_mailjet/contributors].
100
-
101
- == Copyright
102
-
103
- 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>
@@ -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 = "http://zedalaye.github.com/devise_mailjet/"
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' => '~> 4.1',
24
- 'devise' => '~> 3.3',
25
- 'devise-bootstrap-views' => '~> 0.0',
26
- 'mailjet' => '~> 1.0'
21
+ 'rails' => '< 6.1',
22
+ 'devise' => '< 5.0',
23
+ 'devise-bootstrap-views' => '< 1.0',
24
+ 'mailjet' => '< 2.0'
27
25
  }.each do |lib, version|
28
26
  s.add_runtime_dependency(lib, *version)
29
27
  end
@@ -31,3 +31,7 @@ end
31
31
  Devise.add_module :mailjet, :model => 'devise_mailjet/model'
32
32
 
33
33
  require 'devise_mailjet/mailjet_list_api_mapper'
34
+
35
+ if defined?(Sidekiq::Worker)
36
+ require 'devise_mailjet/mailjet_worker'
37
+ end
@@ -1,114 +1,73 @@
1
- require 'mailjet'
2
-
3
- module Devise
4
- module Models
5
- module Mailjet
6
- class MailjetListApiMapper
7
- LISTS_CACHE_KEY = "devise_mailjet/lists"
8
- CONTACTS_CACHE_KEY = "devise_mailjet/contacts"
9
-
10
- # looks the name up in the cache. if it doesn't find it, looks it up using the api and saves it to the cache
11
- def list_name_to_id(list_name)
12
- load_cached_lists
13
- unless @lists.has_key?(list_name)
14
- list = mailjet_list.first(name: list_name)
15
- list = mailjet_list.create(name: list_name) if list.nil?
16
- @lists[list_name] = list.id
17
- save_cached_lists
18
- end
19
- @lists[list_name]
20
- end
21
-
22
- def contact_email_to_id(email)
23
- load_cached_contacts
24
- unless @contacts.has_key?(email)
25
- contact = mailjet_contact.find(email) # email is a valid key for finding contact resources
26
- contact = mailjet_contact.create(email: email) if contact.nil?
27
- @contacts[email] = contact.id
28
- save_cached_contacts
29
- end
30
- @contacts[email]
31
- end
32
-
33
- # subscribes the user to the named mailing list(s). list_names can be the name of one list, or an array of
34
- # several.
35
- #
36
- # NOTE: Do not use this method unless the user has opted in.
37
- def subscribe_to_lists(list_names, email)
38
- walk_recipients(list_names, email) do |lr, list_id, contact_id|
39
- if lr.nil?
40
- mailjet_rcpt.create('ListID' => list_id, 'ContactID' => contact_id, is_active: true)
41
- elsif lr.is_unsubscribed
42
- lr.is_unsubscribed = false
43
- lr.is_active = true
44
- lr.save
45
- end
46
- end
47
- end
48
-
49
- # unsubscribe the user from the named mailing list(s). list_names can be the name of one list, or an array of
50
- # several.
51
- def unsubscribe_from_lists(list_names, email)
52
- walk_recipients(list_names, email) do |lr, _, _|
53
- if lr && !lr.is_unsubscribed
54
- lr.is_unsubscribed = true
55
- lr.is_active = false
56
- lr.save
57
- end
58
- end
59
- end
60
-
61
- class ListLookupError < RuntimeError; end
62
-
63
- private
64
-
65
- def walk_recipients(list_names, email)
66
- contact_id = contact_email_to_id(email)
67
- list_names = [list_names] unless list_names.is_a?(Array)
68
- list_names.each do |list_name|
69
- list_id = list_name_to_id(list_name)
70
- lr = mailjet_rcpt.first('ContactsList' => list_id, 'Contact' => contact_id)
71
- yield lr, list_id, contact_id if block_given?
72
- end
73
- end
74
-
75
- # load the list from the cache
76
- def load_cached_lists
77
- @lists ||= Rails.cache.fetch(LISTS_CACHE_KEY) do
78
- {}
79
- end.dup
80
- end
81
-
82
- # save the modified list back to the cache
83
- def save_cached_lists
84
- Rails.cache.write(LISTS_CACHE_KEY, @lists)
85
- end
86
-
87
- # load contacts from the cache
88
- def load_cached_contacts
89
- @contacts ||= Rails.cache.fetch(CONTACTS_CACHE_KEY) do
90
- {}
91
- end.dup
92
- end
93
-
94
- # save the modified contacts back to the cache
95
- def save_cached_contacts
96
- Rails.cache.write(CONTACTS_CACHE_KEY, @contacts)
97
- end
98
-
99
- # the mailjet api helpers
100
- def mailjet_contact
101
- ::Mailjet::Contact
102
- end
103
-
104
- def mailjet_list
105
- ::Mailjet::Contactslist
106
- end
107
-
108
- def mailjet_rcpt
109
- ::Mailjet::Listrecipient
110
- end
111
- end
112
- end
113
- end
114
- 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
@@ -0,0 +1,34 @@
1
+ module Devise
2
+ module Models
3
+ module Mailjet
4
+ class MailjetWorker
5
+ include Sidekiq::Worker
6
+
7
+ def perform(action, list_names, email, config)
8
+ if config.is_a?(Hash)
9
+ ::Mailjet.configure do |c|
10
+ c.api_version = 'v3'
11
+ c.api_key = config['api_key']
12
+ c.secret_key = config['secret_key']
13
+ c.default_from = config['default_from']
14
+ end
15
+ end
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
+
24
+ mapper = MailjetListApiMapper.new
25
+ if action == 'subscribe'
26
+ mapper.subscribe_to_lists(list_names, email)
27
+ elsif action == 'unsubscribe'
28
+ mapper.unsubscribe_from_lists(list_names, email)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,84 +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 either a single string or an array of strings. By default, returns the mailing_list_name
49
- # configuration option. If you want to customize the lists based on other information, override this method in
50
- # your model.
51
- def mailjet_lists_to_join
52
- self.class.mailing_list_name
53
- end
54
-
55
- # Add the user to the mailjet list with the specified name
56
- def add_to_mailjet_list(list_name)
57
- mapper = mailjet_list_mapper.respond_to?(:delay) ? mailjet_list_mapper.delay : mailjet_list_mapper
58
- # options = self.respond_to?(:mailjet_list_subscribe_options) ? mailjet_list_subscribe_options : {}
59
- mapper.subscribe_to_lists(list_name, self.email)
60
- end
61
-
62
- # remove the user from the mailjet list with the specified name
63
- def remove_from_mailjet_list(list_name)
64
- mapper = mailjet_list_mapper.respond_to?(:delay) ? mailjet_list_mapper.delay : mailjet_list_mapper
65
- mapper.unsubscribe_from_lists(list_name, self.email)
66
- end
67
-
68
- # Commit the user to the mailing list if they have selected to join
69
- def commit_mailing_list_join
70
- add_to_mailjet_list(mailjet_lists_to_join) if self.join_mailing_list
71
- end
72
-
73
- # mapper that helps convert list names to mailjet ids
74
- def mailjet_list_mapper
75
- @@mailjet_list_mapper ||= MailjetListApiMapper.new
76
- end
77
-
78
- module ClassMethods
79
- Devise::Models.config(self, :mailing_list_name)
80
- Devise::Models.config(self, :mailing_list_opt_in_by_default)
81
- end
82
- end
83
- end
84
- end
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
@@ -1,3 +1,3 @@
1
1
  module DeviseMailjet
2
- VERSION = "0.0.5"
2
+ VERSION = "0.2.5"
3
3
  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.0.5
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Cunningham
@@ -9,64 +9,64 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-26 00:00:00.000000000 Z
12
+ date: 2021-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - "<"
19
19
  - !ruby/object:Gem::Version
20
- version: '4.1'
20
+ version: '6.1'
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: '4.1'
27
+ version: '6.1'
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: '3.3'
34
+ version: '5.0'
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: '3.3'
41
+ version: '5.0'
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: '0.0'
48
+ version: '1.0'
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: '0.0'
55
+ version: '1.0'
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: '1.0'
62
+ version: '2.0'
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: '1.0'
69
+ version: '2.0'
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:
@@ -86,9 +86,10 @@ files:
86
86
  - devise_mailjet.gemspec
87
87
  - lib/devise_mailjet.rb
88
88
  - lib/devise_mailjet/mailjet_list_api_mapper.rb
89
+ - lib/devise_mailjet/mailjet_worker.rb
89
90
  - lib/devise_mailjet/model.rb
90
91
  - lib/devise_mailjet/version.rb
91
- homepage: http://zedalaye.github.com/devise_mailjet/
92
+ homepage: https://github.com/zedalaye/devise_mailjet
92
93
  licenses:
93
94
  - MIT
94
95
  metadata: {}
@@ -107,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
108
  - !ruby/object:Gem::Version
108
109
  version: '0'
109
110
  requirements: []
110
- rubyforge_project: devise_mailjet
111
- rubygems_version: 2.2.2
111
+ rubygems_version: 3.1.6
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Easy MailJet integration for Devise