devise_mailjet 0.0.5 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e54b0e5430f9fd1e5c9fedc92190caffbe4adfb
4
- data.tar.gz: f3964d2f000ff3bcad2f68da8676b02608a1137f
3
+ metadata.gz: 492f65b3aeb563982e0e48a9cb77e57ecc0aaf4e
4
+ data.tar.gz: 1ba5a15c05396b6ce73247577c0137460ea26743
5
5
  SHA512:
6
- metadata.gz: cd182ae9e33c74538caba2d7cc94594b95d2efdbf456720cf2385c746314275e9bae02bf7f71a7f009beaad98683283254b44ec726a7c8b3fffbdb52444b12cb
7
- data.tar.gz: eb1f2aecffd249046e3704765a90ac5cf635e4bb92508e0ff81c26012cd18d037dd2aaa19a36573852be3d5fcbdeef74be0ed768bf6468658d58bcec6ef926c7
6
+ metadata.gz: 4a0ee5f2e82ad2b1d0af00eae4be25bfabc685e38ff5bfc6c6f9fa05c05549615142735d82715e2c7d5084026b2ecb2eb77bfe52f3c480de0d325706a2e95ece
7
+ data.tar.gz: e089c81e6f926f559475957326f7f3a100479d25da915de6d21f01f7dd52d58fafd0be60322c2f79494c8d08ef8df472dd9cfe964851935148d1c1d9b2766a61
data/README.rdoc CHANGED
@@ -16,13 +16,12 @@ In your Gemfile, add devise_mailjet after devise:
16
16
  In your User model, add :mailjet to the devise call and make :join_mailing_list accessible:
17
17
 
18
18
  devise :database_authenticatable, ..., :mailjet
19
- attr_accessible :join_mailing_list
19
+ attr_accessor :join_mailing_list
20
20
 
21
21
  In your devise initializer (config/initializers/devise.rb), set your API key and mailing list name:
22
22
 
23
23
  Devise.mailjet_api_key = 'your_api_key'
24
24
  Devise.mailing_list_name = 'List Name'
25
- Devise.double_opt_in = false
26
25
  Devise.send_welcome_email = false
27
26
 
28
27
  If you are using the default Devise registration views, the Join Mailing List checkbox is added automatically, if not,
@@ -100,4 +99,4 @@ View our {contributors}[https://github.com/zedalaye/devise_mailjet/contributors]
100
99
 
101
100
  == Copyright
102
101
 
103
- Copyright (c) 2011 {Justin Cunningham}[http://littlebitofcode.com] and 2014 {Pierre Yager}[http://levosgien.net]. See MIT_LICENSE for details.
102
+ Copyright (c) 2011 {Justin Cunningham}[http://littlebitofcode.com] and 2014 {Pierre Yager}[http://levosgien.net]. See MIT_LICENSE for details.
@@ -20,10 +20,10 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  {
23
- 'rails' => '~> 4.1',
24
- 'devise' => '~> 3.3',
25
- 'devise-bootstrap-views' => '~> 0.0',
26
- 'mailjet' => '~> 1.0'
23
+ 'rails' => '< 5.2',
24
+ 'devise' => '< 5.0',
25
+ 'devise-bootstrap-views' => '< 1.0',
26
+ 'mailjet' => '< 2.0'
27
27
  }.each do |lib, version|
28
28
  s.add_runtime_dependency(lib, *version)
29
29
  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
@@ -4,28 +4,24 @@ module Devise
4
4
  module Models
5
5
  module Mailjet
6
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
7
+ # find the list using the mailjet API and save it to a memory cache
11
8
  def list_name_to_id(list_name)
12
- load_cached_lists
9
+ @lists ||= {}
13
10
  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
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
18
14
  end
19
15
  @lists[list_name]
20
16
  end
21
17
 
18
+ # find the contact using the mailjet API and save it to a memory cache
22
19
  def contact_email_to_id(email)
23
- load_cached_contacts
20
+ @contacts ||= {}
24
21
  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
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
29
25
  end
30
26
  @contacts[email]
31
27
  end
@@ -37,25 +33,23 @@ module Devise
37
33
  def subscribe_to_lists(list_names, email)
38
34
  walk_recipients(list_names, email) do |lr, list_id, contact_id|
39
35
  if lr.nil?
40
- mailjet_rcpt.create('ListID' => list_id, 'ContactID' => contact_id, is_active: true)
36
+ ::Mailjet::Listrecipient.create('ListID' => list_id, 'ContactID' => contact_id, is_active: true)
41
37
  elsif lr.is_unsubscribed
42
- lr.is_unsubscribed = false
43
- lr.is_active = true
44
- lr.save
38
+ lr.update_attributes(is_unsubscribed: false, is_active: true)
45
39
  end
46
40
  end
41
+ rescue ::Mailjet::ApiError
42
+ # ignore
47
43
  end
48
44
 
49
45
  # unsubscribe the user from the named mailing list(s). list_names can be the name of one list, or an array of
50
46
  # several.
51
47
  def unsubscribe_from_lists(list_names, email)
52
48
  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
49
+ lr.update_attributes(is_unsubscribed: true, is_active: false) if lr && !lr.is_unsubscribed
58
50
  end
51
+ rescue ::Mailjet::ApiError
52
+ # ignore
59
53
  end
60
54
 
61
55
  class ListLookupError < RuntimeError; end
@@ -67,47 +61,10 @@ module Devise
67
61
  list_names = [list_names] unless list_names.is_a?(Array)
68
62
  list_names.each do |list_name|
69
63
  list_id = list_name_to_id(list_name)
70
- lr = mailjet_rcpt.first('ContactsList' => list_id, 'Contact' => contact_id)
64
+ lr = ::Mailjet::Listrecipient.all('ContactsList' => list_id, 'Contact' => contact_id, limit: 1).first
71
65
  yield lr, list_id, contact_id if block_given?
72
66
  end
73
67
  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
68
  end
112
69
  end
113
70
  end
@@ -0,0 +1,26 @@
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_key = config['api_key']
11
+ c.secret_key = config['secret_key']
12
+ c.default_from = config['default_from']
13
+ end
14
+ end
15
+
16
+ mapper = MailjetListApiMapper.new
17
+ if action == 'subscribe'
18
+ mapper.subscribe_to_lists(list_names, email)
19
+ elsif action == 'unsubscribe'
20
+ mapper.unsubscribe_from_lists(list_names, email)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -54,31 +54,53 @@ module Devise
54
54
 
55
55
  # Add the user to the mailjet list with the specified name
56
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)
57
+ if defined?(Sidekiq::Worker)
58
+ MailjetWorker.perform_async(:subscribe, list_name, self.email, mailjet_config)
59
+ else
60
+ mapper = mailjet_list_mapper.respond_to?(:delay) ? mailjet_list_mapper.delay : mailjet_list_mapper
61
+ # options = self.respond_to?(:mailjet_list_subscribe_options) ? mailjet_list_subscribe_options : {}
62
+ mapper.subscribe_to_lists(list_name, self.email)
63
+ end
60
64
  end
61
65
 
62
66
  # remove the user from the mailjet list with the specified name
63
67
  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)
68
+ if defined?(Sidekiq::Worker)
69
+ MailjetWorker.perform_async(:unsubscribe, list_name, self.email, mailjet_config)
70
+ else
71
+ mapper = mailjet_list_mapper.respond_to?(:delay) ? mailjet_list_mapper.delay : mailjet_list_mapper
72
+ mapper.unsubscribe_from_lists(list_name, self.email)
73
+ end
66
74
  end
67
75
 
68
76
  # Commit the user to the mailing list if they have selected to join
69
77
  def commit_mailing_list_join
70
- add_to_mailjet_list(mailjet_lists_to_join) if self.join_mailing_list
78
+ if self.join_mailing_list
79
+ add_to_mailjet_list(mailjet_lists_to_join)
80
+ else
81
+ remove_from_mailjet_list(mailjet_lists_to_join)
82
+ end
71
83
  end
72
84
 
73
85
  # mapper that helps convert list names to mailjet ids
74
86
  def mailjet_list_mapper
75
- @@mailjet_list_mapper ||= MailjetListApiMapper.new
87
+ @@mailjet_list_api_mapper ||= MailjetListApiMapper.new
76
88
  end
77
89
 
78
90
  module ClassMethods
79
91
  Devise::Models.config(self, :mailing_list_name)
80
92
  Devise::Models.config(self, :mailing_list_opt_in_by_default)
81
93
  end
94
+
95
+ private
96
+
97
+ def mailjet_config
98
+ {
99
+ 'api_key' => ::Mailjet.config.api_key,
100
+ 'secret_key' => ::Mailjet.config.secret_key,
101
+ 'default_from' => ::Mailjet.config.default_from
102
+ }
103
+ end
82
104
  end
83
105
  end
84
106
  end
@@ -1,3 +1,3 @@
1
1
  module DeviseMailjet
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
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.1.0
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: 2018-01-16 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: '5.2'
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: '5.2'
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,6 +86,7 @@ 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
92
  homepage: http://zedalaye.github.com/devise_mailjet/
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  version: '0'
109
110
  requirements: []
110
111
  rubyforge_project: devise_mailjet
111
- rubygems_version: 2.2.2
112
+ rubygems_version: 2.6.14
112
113
  signing_key:
113
114
  specification_version: 4
114
115
  summary: Easy MailJet integration for Devise