devise_mailjet 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e54b0e5430f9fd1e5c9fedc92190caffbe4adfb
4
+ data.tar.gz: f3964d2f000ff3bcad2f68da8676b02608a1137f
5
+ SHA512:
6
+ metadata.gz: cd182ae9e33c74538caba2d7cc94594b95d2efdbf456720cf2385c746314275e9bae02bf7f71a7f009beaad98683283254b44ec726a7c8b3fffbdb52444b12cb
7
+ data.tar.gz: eb1f2aecffd249046e3704765a90ac5cf635e4bb92508e0ff81c26012cd18d037dd2aaa19a36573852be3d5fcbdeef74be0ed768bf6468658d58bcec6ef926c7
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .idea
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in devise_mailjet.gemspec
4
+ gemspec
data/MIT_LICENSE ADDED
@@ -0,0 +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
14
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,103 @@
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.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +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
+
28
+ <%= render "devise/shared/links" %>
@@ -0,0 +1,4 @@
1
+ <div class="form-group">
2
+ <%= form.label :join_mailing_list %>
3
+ <%= form.check_box :join_mailing_list %>
4
+ </div>
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "devise_mailjet/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "devise_mailjet"
7
+ s.version = DeviseMailjet::VERSION
8
+ s.authors = ["Justin Cunningham", "Pierre Yager"]
9
+ s.email = ["justin@compucatedsolutions.com", "pierre@levosgien.net"]
10
+ s.homepage = "http://zedalaye.github.com/devise_mailjet/"
11
+ s.summary = %q{Easy MailJet integration for Devise}
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
+ s.licenses = 'MIT'
14
+
15
+ s.rubyforge_project = "devise_mailjet"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ {
23
+ 'rails' => '~> 4.1',
24
+ 'devise' => '~> 3.3',
25
+ 'devise-bootstrap-views' => '~> 0.0',
26
+ 'mailjet' => '~> 1.0'
27
+ }.each do |lib, version|
28
+ s.add_runtime_dependency(lib, *version)
29
+ end
30
+
31
+ # specify any dependencies here; for example:
32
+ # s.add_development_dependency "rspec"
33
+ # s.add_runtime_dependency "rest-client"
34
+ end
@@ -0,0 +1,33 @@
1
+ require 'devise'
2
+
3
+ require "devise_mailjet/version"
4
+
5
+ module DeviseMailjet
6
+ class Engine < Rails::Engine
7
+ end
8
+ # Your code goes here...
9
+ end
10
+
11
+ module Devise
12
+ # Public: Default mailing list for user to join. This can be an array of strings, or just one string.
13
+ # By default, this is "Site List". If this will be configurable for each user, override
14
+ # mailjet_lists_to_join returning the list name or an array of list names for the user to
15
+ # join.
16
+ # Set mailing_list_name in the Devise configuration file (config/initializers/devise.rb)
17
+ #
18
+ # Devise.mailing_list_name = "Your Mailing List Name"
19
+ mattr_accessor :mailing_list_name
20
+ @@mailing_list_name = "Newsletter"
21
+
22
+ # Public: Determines if the checkbox for the user to opt-in to the mailing list should
23
+ # be checked by default, or not. Defaults to true.
24
+ # Set mailing_list_opt_in_by_default in the Devise configuration file (config/initializers/devise.rb)
25
+ #
26
+ # Devise.mailing_list_opt_in_by_default = false
27
+ mattr_accessor :mailing_list_opt_in_by_default
28
+ @@mailing_list_opt_in_by_default = true
29
+ end
30
+
31
+ Devise.add_module :mailjet, :model => 'devise_mailjet/model'
32
+
33
+ require 'devise_mailjet/mailjet_list_api_mapper'
@@ -0,0 +1,114 @@
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
@@ -0,0 +1,84 @@
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
@@ -0,0 +1,3 @@
1
+ module DeviseMailjet
2
+ VERSION = "0.0.5"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_mailjet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Justin Cunningham
8
+ - Pierre Yager
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: devise
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.3'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: devise-bootstrap-views
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '0.0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '0.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: mailjet
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ description: Devise MailJet adds a MailJet option to devise that easily enables users
71
+ to join your mailing list when they create an account.
72
+ email:
73
+ - justin@compucatedsolutions.com
74
+ - pierre@levosgien.net
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - Gemfile
81
+ - MIT_LICENSE
82
+ - README.rdoc
83
+ - Rakefile
84
+ - app/views/devise/registrations/new.html.erb
85
+ - app/views/devise/shared/mailjet/_form.html.erb
86
+ - devise_mailjet.gemspec
87
+ - lib/devise_mailjet.rb
88
+ - lib/devise_mailjet/mailjet_list_api_mapper.rb
89
+ - lib/devise_mailjet/model.rb
90
+ - lib/devise_mailjet/version.rb
91
+ homepage: http://zedalaye.github.com/devise_mailjet/
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project: devise_mailjet
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Easy MailJet integration for Devise
115
+ test_files: []