devise_mailchimp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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_mailchimp.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,86 @@
1
+ = Devise MailChimp
2
+
3
+ Devise MailChimp adds a MailChimp 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_mailchimp after devise:
12
+
13
+ gem "devise"
14
+ gem "devise_mailchimp" # Last officially released gem
15
+
16
+ In your User model, add :mailchimp to the devise call:
17
+
18
+ devise :database_authenticatable, ..., :mailchimp
19
+
20
+ In your devise initializer (config/initializers/devise.rb), set your API key and mailing list name:
21
+
22
+ Devise.mailchimp_api_key = 'your_api_key'
23
+ Devise.mailing_list_name = 'List Name'
24
+
25
+ If you are using the default Devise registration views, the Join Mailing List checkbox is added automatically, if not,
26
+ either include the form partial in your new registration form:
27
+
28
+ <%= render :partial => "devise/shared/mailchimp/form", :locals => {:form => f} %>
29
+
30
+ Or manually add a "Join Mailing List" checkbox to your new registration form:
31
+
32
+ <%=form.check_box :join_mailing_list%>
33
+
34
+ == Configuration
35
+
36
+ Create an initializer, and set your MailChimp API key. To generate a new API key, go to the account tab in your
37
+ MailChimp account and select API Keys & Authorized Apps, then add a key.
38
+
39
+ Devise.mailchimp_api_key = 'your_api_key'
40
+
41
+ Create a mailing list, and set the mailing list name in the initializer. To create a MailChimp list, from your account
42
+ go to the Lists tab, then hit create list.
43
+
44
+ Devise.mailing_list_name = 'List Name'
45
+
46
+ == Documentation
47
+
48
+ Full documentation is available at {rdoc.info}[http://rdoc.info/github/jcnnghm/devise_mailchimp/master/frames].
49
+
50
+ == Demo Application
51
+
52
+ A demo application is available at {github}[https://github.com/jcnnghm/devise_mailchimp_demo].
53
+
54
+ == Example Usage
55
+
56
+ Users will join the default mailing list if join_mailing_list is set to true when the user is created. To manually add
57
+ a user:
58
+
59
+ User.find(1).add_to_mailchimp_list('Site Administrators List')
60
+
61
+ To manually remove a user:
62
+
63
+ User.find(1).remove_from_mailchimp_list('Site Administrators List')
64
+
65
+ NOTE: You MUST have the users permission to add them to a mailing list.
66
+
67
+ == Customization
68
+
69
+ To have the user join more than one list, or to override the lists that the user will join, override
70
+ mailchimp_lists_to_join in your model. Your method should return a single list, or an array of lists.
71
+
72
+ def mailchimp_lists_to_join
73
+ lists = ["Site Users List"]
74
+ lists << "Site Admins List" if admin?
75
+ return lists
76
+ end
77
+
78
+ If all users will join the same list or lists, just set the mailing_list_name configuration option.
79
+
80
+ == Contributions
81
+
82
+ Please help this software improve by submitting pull requests, preferably with tests.
83
+
84
+ == Copyright
85
+
86
+ Copyright (c) 2011 {Justin Cunningham}[http://littlebitofcode.com]. See MIT_LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ <h2>Sign up</h2>
2
+
3
+ <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
4
+ <%= devise_error_messages! %>
5
+
6
+ <div><%= f.label :email %><br />
7
+ <%= f.email_field :email %></div>
8
+
9
+ <div><%= f.label :password %><br />
10
+ <%= f.password_field :password %></div>
11
+
12
+ <div><%= f.label :password_confirmation %><br />
13
+ <%= f.password_field :password_confirmation %></div>
14
+
15
+ <%= render :partial => "devise/shared/mailchimp/form", :locals => {:form => f} %>
16
+
17
+ <div><%= f.submit "Sign up" %></div>
18
+ <% end %>
19
+
20
+ <%= render :partial => "devise/shared/links" %>
@@ -0,0 +1,2 @@
1
+ <div><%= form.label :join_mailing_list %><br />
2
+ <%=form.check_box :join_mailing_list%></div>
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "devise_mailchimp/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "devise_mailchimp"
7
+ s.version = DeviseMailchimp::VERSION
8
+ s.authors = ["Justin Cunningham"]
9
+ s.email = ["justin@compucatedsolutions.com"]
10
+ s.homepage = "http://jcnnghm.github.com/devise_mailchimp/"
11
+ s.summary = %q{Easy MailChimp integration for Devise}
12
+ s.description = %q{Devise MailChimp adds a MailChimp option to devise that easily enables users to join your mailing list when they create an account.}
13
+
14
+ s.rubyforge_project = "devise_mailchimp"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ {
22
+ 'rails' => ['>= 3.0.0', '< 3.2'],
23
+ 'devise' => '~> 1.4.8',
24
+ 'hominid' => "~> 3.0.2"
25
+ }.each do |lib, version|
26
+ s.add_runtime_dependency(lib, *version)
27
+ end
28
+
29
+ # specify any dependencies here; for example:
30
+ # s.add_development_dependency "rspec"
31
+ # s.add_runtime_dependency "rest-client"
32
+ end
@@ -0,0 +1,42 @@
1
+ require 'devise'
2
+
3
+ require "devise_mailchimp/version"
4
+
5
+ module DeviseMailchimp
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
+ # mailchimp_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 = "Site List"
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
+
30
+ # Public: The API key for accessing the mailchimp service. To generate a new API key, go to the
31
+ # account tab in your MailChimp account and select API Keys & Authorized Apps, then add
32
+ # a key. This defaults to 'your_api_key'
33
+ # Set mailchimp_api_key in the Devise configuration file (config/initializers/devise.rb)
34
+ #
35
+ # Devise.mailchimp_api_key = "your_api_key"
36
+ mattr_accessor :mailchimp_api_key
37
+ @@mailchimp_api_key = 'your_api_key'
38
+ end
39
+
40
+ Devise.add_module :mailchimp, :model => 'devise_mailchimp/model'
41
+
42
+ require 'devise_mailchimp/mailchimp_list_api_mapper'
@@ -0,0 +1,78 @@
1
+ require 'hominid'
2
+
3
+ module Devise
4
+ module Models
5
+ module Mailchimp
6
+ class MailchimpListApiMapper
7
+ LIST_CACHE_KEY = "devise_mailchimp/lists"
8
+
9
+ # craete a new ApiMapper with the provided API key
10
+ def initialize(api_key)
11
+ @api_key = api_key
12
+ end
13
+
14
+ # 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
15
+ def name_to_id(list_name)
16
+ load_cached_lists
17
+ if @lists.has_key?(list_name)
18
+ return @lists[list_name]
19
+ else
20
+ list_id = hominid.find_list_id_by_name(list_name)
21
+ if list_id.nil?
22
+ raise ListLookupError
23
+ else
24
+ @lists[list_name] = list_id
25
+ save_cached_lists
26
+ return @lists[list_name]
27
+ end
28
+ end
29
+ end
30
+
31
+ # subscribes the user to the named mailing list(s). list_names can be the name of one list, or an array of
32
+ # several.
33
+ #
34
+ # NOTE: Do not use this method unless the user has opted in.
35
+ def subscribe_to_lists(list_names, email)
36
+ list_names = [list_names] unless list_names.is_a?(Array)
37
+ list_names.each do |list_name|
38
+ list_id = name_to_id(list_name)
39
+ hominid.list_subscribe(list_id, email, {}, 'html', false, true, true, false)
40
+ end
41
+ end
42
+
43
+ # unsubscribe the user from the named mailing list(s). list_names can be the name of one list, or an array of
44
+ # several.
45
+ def unsubscribe_from_lists(list_names, email)
46
+ list_names = [list_names] unless list_names.is_a?(Array)
47
+ list_names.each do |list_name|
48
+ list_id = name_to_id(list_name)
49
+ hominid.list_unsubscribe(list_id, email, false, false, false)
50
+ # don't delete, send goodbye, or send notification
51
+ end
52
+ end
53
+
54
+
55
+ class ListLookupError < RuntimeError; end
56
+
57
+ private
58
+
59
+ # load the list from the cache
60
+ def load_cached_lists
61
+ @lists ||= Rails.cache.fetch(LIST_CACHE_KEY) do
62
+ {}
63
+ end.dup
64
+ end
65
+
66
+ # save the modified list back to the cache
67
+ def save_cached_lists
68
+ Rails.cache.write(LIST_CACHE_KEY, @lists)
69
+ end
70
+
71
+ # the hominid api helper
72
+ def hominid
73
+ @hominid ||= Hominid::API.new(@api_key)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,86 @@
1
+ module Devise
2
+ module Models
3
+ # Mailchimp is responsible for joining users to mailchimp 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 mailchimp_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
+ # mailchimp_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
+ # mailchimp_api_key: The API key for accessing the mailchimp service. To generate a new API key, go to the
18
+ # account tab in your MailChimp account and select API Keys & Authorized Apps, then add
19
+ # a key. This defaults to 'your_api_key'
20
+ #
21
+ # Examples:
22
+ #
23
+ # User.find(1).add_to_mailchimp_list('Site Administrators List')
24
+ # User.find(1).remove_from_mailchimp_list('Site Administrators List')
25
+ #
26
+ # u = User.new
27
+ # u.join_mailing_list = true
28
+ # u.save
29
+ module Mailchimp
30
+ extend ActiveSupport::Concern
31
+
32
+ included do
33
+ after_create :commit_mailing_list_join
34
+ end
35
+
36
+ # Set this to true to have the user automatically join the mailchimp_lists_to_join
37
+ def join_mailing_list=(join)
38
+ join.downcase! if join.is_a?(String)
39
+ true_values = ['yes','true',true,'1',1]
40
+ join = true_values.include?(join)
41
+ @join_mailing_list = join
42
+ end
43
+
44
+ #
45
+ def join_mailing_list
46
+ @join_mailing_list.nil? ? self.class.mailing_list_opt_in_by_default : @join_mailing_list
47
+ end
48
+
49
+ # The mailing list or lists the user will join
50
+ # Should return either a single string or an array of strings. By default, returns the mailing_list_name
51
+ # configuration option. If you want to customize the lists based on other information, override this method in
52
+ # your model.
53
+ def mailchimp_lists_to_join
54
+ self.class.mailing_list_name
55
+ end
56
+
57
+ # Add the user to the mailchimp list with the specified name
58
+ def add_to_mailchimp_list(list_name)
59
+ mapper = mailchimp_list_mapper.respond_to?(:delay) ? mailchimp_list_mapper.delay : mailchimp_list_mapper
60
+ mapper.subscribe_to_lists(list_name, self.email)
61
+ end
62
+
63
+ # remove the user from the mailchimp list with the specified name
64
+ def remove_from_mailchimp_list(list_name)
65
+ mapper = mailchimp_list_mapper.respond_to?(:delay) ? mailchimp_list_mapper.delay : mailchimp_list_mapper
66
+ mapper.unsubscribe_from_lists(list_name, self.email)
67
+ end
68
+
69
+ # Commit the user to the mailing list if they have selected to join
70
+ def commit_mailing_list_join
71
+ add_to_mailchimp_list(mailchimp_lists_to_join) if @join_mailing_list
72
+ end
73
+
74
+ # mapper that helps convert list names to mailchimp ids
75
+ def mailchimp_list_mapper
76
+ @@mailchimp_list_mapper ||= MailchimpListApiMapper.new(self.class.mailchimp_api_key)
77
+ end
78
+
79
+ module ClassMethods
80
+ Devise::Models.config(self, :mailchimp_api_key)
81
+ Devise::Models.config(self, :mailing_list_name)
82
+ Devise::Models.config(self, :mailing_list_opt_in_by_default)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module DeviseMailchimp
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_mailchimp
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Justin Cunningham
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-27 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ - - <
36
+ - !ruby/object:Gem::Version
37
+ hash: 3
38
+ segments:
39
+ - 3
40
+ - 2
41
+ version: "3.2"
42
+ type: :runtime
43
+ version_requirements: *id001
44
+ - !ruby/object:Gem::Dependency
45
+ name: devise
46
+ prerelease: false
47
+ requirement: &id002 !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ hash: 23
53
+ segments:
54
+ - 1
55
+ - 4
56
+ - 8
57
+ version: 1.4.8
58
+ type: :runtime
59
+ version_requirements: *id002
60
+ - !ruby/object:Gem::Dependency
61
+ name: hominid
62
+ prerelease: false
63
+ requirement: &id003 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 3
71
+ - 0
72
+ - 2
73
+ version: 3.0.2
74
+ type: :runtime
75
+ version_requirements: *id003
76
+ description: Devise MailChimp adds a MailChimp option to devise that easily enables users to join your mailing list when they create an account.
77
+ email:
78
+ - justin@compucatedsolutions.com
79
+ executables: []
80
+
81
+ extensions: []
82
+
83
+ extra_rdoc_files: []
84
+
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - MIT_LICENSE
89
+ - README.rdoc
90
+ - Rakefile
91
+ - app/views/devise/registrations/new.html.erb
92
+ - app/views/devise/shared/mailchimp/_form.html.erb
93
+ - devise_mailchimp.gemspec
94
+ - lib/devise_mailchimp.rb
95
+ - lib/devise_mailchimp/mailchimp_list_api_mapper.rb
96
+ - lib/devise_mailchimp/model.rb
97
+ - lib/devise_mailchimp/version.rb
98
+ has_rdoc: true
99
+ homepage: http://jcnnghm.github.com/devise_mailchimp/
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options: []
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ requirements: []
126
+
127
+ rubyforge_project: devise_mailchimp
128
+ rubygems_version: 1.3.7
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Easy MailChimp integration for Devise
132
+ test_files: []
133
+