subscribed_to 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -1
- data/config/routes.rb +4 -2
- data/lib/subscribed_to/mail_chimp/config.rb +1 -1
- data/lib/subscribed_to/mail_chimp/web_hook.rb +1 -1
- data/lib/subscribed_to/mail_chimp.rb +12 -0
- data/lib/subscribed_to/version.rb +1 -1
- data/lib/subscribed_to.rb +26 -23
- data/spec/app/controllers/subscribed_to/mail_chimp_web_hooks_spec.rb +2 -2
- data/spec/dummy/config/initializers/subscribed_to.rb +2 -0
- data/spec/lib/subscribed_to/mail_chimp/web_hook_spec.rb +9 -9
- data/subscribed_to.gemspec +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -54,9 +54,11 @@ Other options:
|
|
54
54
|
|
55
55
|
rails generate subscribed_to:install MODEL
|
56
56
|
|
57
|
-
If you run the generator with <tt>--skip-migration</tt>, then you must be sure to to include the
|
57
|
+
If you run the generator with <tt>--skip-migration</tt>, then you must be sure to to include the
|
58
|
+
<tt>subscribed_to_list</tt> and <tt>mail_chimp_id</tt> columns in some other migration.
|
58
59
|
|
59
60
|
t.boolean :subscribed_to_list, :default => false
|
61
|
+
t.integer :mail_chimp_id
|
60
62
|
|
61
63
|
Be sure to run the migration before you continue.
|
62
64
|
|
@@ -66,6 +68,10 @@ After the initializer has been generated, you must set your mail service configu
|
|
66
68
|
|
67
69
|
==== Mail Chimp
|
68
70
|
|
71
|
+
Activate the gem. Can be disabled for development, staging, etc environtments. Activated only in production environment by default.
|
72
|
+
|
73
|
+
config.active = true
|
74
|
+
|
69
75
|
Set your API key
|
70
76
|
|
71
77
|
mail_chimp_config.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us1"
|
data/config/routes.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
|
3
|
-
|
2
|
+
if SubscribedTo.active
|
3
|
+
namespace :subscribed_to do
|
4
|
+
match "mail_chimp" => "mail_chimp_web_hooks#create", :via => :post
|
5
|
+
end
|
4
6
|
end
|
5
7
|
end
|
@@ -5,6 +5,18 @@ require 'hominid'
|
|
5
5
|
module SubscribedTo
|
6
6
|
# Module for MailChimp subscription interaction
|
7
7
|
module MailChimp
|
8
|
+
module ClassMethods
|
9
|
+
# Returns the list id for the class as defined in mail_chimp
|
10
|
+
def list_id
|
11
|
+
SubscribedTo.mail_chimp_config.lists[@list_key][:id]
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns the hash of merge vars for the class as defined in mail_chimp
|
15
|
+
def merge_vars
|
16
|
+
SubscribedTo.mail_chimp_config.lists[@list_key][:merge_vars]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
8
20
|
module InstanceMethods
|
9
21
|
private
|
10
22
|
|
data/lib/subscribed_to.rb
CHANGED
@@ -4,7 +4,15 @@ require 'active_record_extensions'
|
|
4
4
|
require 'subscribed_to/mail_chimp'
|
5
5
|
|
6
6
|
module SubscribedTo
|
7
|
+
# Activate the gem.
|
8
|
+
#
|
9
|
+
# Can be disabled for development, staging, etc environtments.
|
10
|
+
# Activated only in production environment by default.
|
11
|
+
mattr_accessor :active
|
12
|
+
@@active = Rails.env == "production"
|
13
|
+
|
7
14
|
# Mailing list service to interact with.
|
15
|
+
#
|
8
16
|
# Options: :mail_chimp, :constant_contact
|
9
17
|
# Currently only supports Mail Chimp
|
10
18
|
mattr_accessor :service
|
@@ -44,31 +52,26 @@ module SubscribedTo
|
|
44
52
|
# The only paramter it takes is a symbol which corresponds to a list in the <tt>mail_chimp_config.lists</tt> hash.
|
45
53
|
# subscribed_to :mailing_list
|
46
54
|
def subscribed_to(id)
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
if SubscribedTo.active # don't activate all the gem goodies if we're not active
|
56
|
+
include InstanceMethods
|
57
|
+
if SubscribedTo.service == :mail_chimp
|
58
|
+
extend MailChimp::ClassMethods
|
59
|
+
include MailChimp::InstanceMethods
|
60
|
+
end
|
61
|
+
|
62
|
+
@list_key = id.to_sym
|
63
|
+
|
64
|
+
# We need to reference which models are enabled, and which list they belong to when processing the webhooks.
|
65
|
+
SubscribedTo.mail_chimp_config.enabled_models[self.list_id].blank? ?
|
66
|
+
SubscribedTo.mail_chimp_config.enabled_models[self.list_id] = [self.to_s] :
|
67
|
+
SubscribedTo.mail_chimp_config.enabled_models[self.list_id] << self.to_s
|
68
|
+
|
69
|
+
class_eval do
|
70
|
+
after_create :subscribe_to_list
|
71
|
+
after_update :update_list_member
|
72
|
+
end
|
60
73
|
end
|
61
74
|
end
|
62
|
-
|
63
|
-
# Returns the list id for the class as defined in mail_chimp
|
64
|
-
def list_id
|
65
|
-
SubscribedTo.mail_chimp_config.lists[@list_key][:id]
|
66
|
-
end
|
67
|
-
|
68
|
-
# Returns the hash of merge vars for the class as defined in mail_chimp
|
69
|
-
def merge_vars
|
70
|
-
SubscribedTo.mail_chimp_config.lists[@list_key][:merge_vars]
|
71
|
-
end
|
72
75
|
end
|
73
76
|
|
74
77
|
# Provides instance methods which should be overwritten in service modules
|
@@ -85,8 +85,8 @@ describe SubscribedTo::MailChimpWebHooksController do
|
|
85
85
|
|
86
86
|
context "for an existing user" do
|
87
87
|
before(:each) do
|
88
|
-
# user was updated
|
89
|
-
pretend_now_is(Time.zone.now -
|
88
|
+
# user was updated 0:11 ago
|
89
|
+
pretend_now_is(Time.zone.now - 11.seconds) do
|
90
90
|
@user = Factory.build(:subscribed_user)
|
91
91
|
@user.stubs(:subscribe_to_list)
|
92
92
|
@user.save
|
@@ -27,14 +27,14 @@ describe SubscribedTo::MailChimp::WebHook do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should rate limit updates via the api to once every 2 minutes" do
|
30
|
-
# user was updated
|
31
|
-
pretend_now_is(Time.zone.now -
|
30
|
+
# user was updated 0:10 ago
|
31
|
+
pretend_now_is(Time.zone.now - 10.seconds) do
|
32
32
|
@user = Factory.build(:non_subscribed_user)
|
33
33
|
@user.stubs(:subscribe_to_list)
|
34
34
|
@user.save
|
35
35
|
end
|
36
36
|
|
37
|
-
# pretend it's
|
37
|
+
# pretend it's 0:09 from last update
|
38
38
|
pretend_now_is(Time.zone.now - 1.seconds) do
|
39
39
|
expect do
|
40
40
|
SubscribedTo::MailChimp::WebHook.process({
|
@@ -46,8 +46,8 @@ describe SubscribedTo::MailChimp::WebHook do
|
|
46
46
|
end.not_to change { @user.reload.email }.to("my.new@email.com")
|
47
47
|
end
|
48
48
|
|
49
|
-
# pretend it's
|
50
|
-
pretend_now_is(Time.zone.now +
|
49
|
+
# pretend it's 0:11 from last update
|
50
|
+
pretend_now_is(Time.zone.now + 10.seconds) do
|
51
51
|
expect do
|
52
52
|
SubscribedTo::MailChimp::WebHook.process({
|
53
53
|
"type" => "upemail",
|
@@ -70,8 +70,8 @@ describe SubscribedTo::MailChimp::WebHook do
|
|
70
70
|
|
71
71
|
context "for a new user" do
|
72
72
|
before(:each) do
|
73
|
-
# user was updated
|
74
|
-
pretend_now_is(Time.zone.now -
|
73
|
+
# user was updated 0:11 ago
|
74
|
+
pretend_now_is(Time.zone.now - 11.seconds) do
|
75
75
|
@user = Factory.build(:non_subscribed_user)
|
76
76
|
@user.stubs(:subscribe_to_list)
|
77
77
|
@user.save
|
@@ -99,8 +99,8 @@ describe SubscribedTo::MailChimp::WebHook do
|
|
99
99
|
|
100
100
|
context "for an existing user" do
|
101
101
|
before(:each) do
|
102
|
-
# user was updated
|
103
|
-
pretend_now_is(Time.zone.now -
|
102
|
+
# user was updated 0:11 ago
|
103
|
+
pretend_now_is(Time.zone.now - 11.seconds) do
|
104
104
|
@user = Factory.build(:subscribed_user)
|
105
105
|
@user.stubs(:subscribe_to_list)
|
106
106
|
@user.save
|
data/subscribed_to.gemspec
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: subscribed_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.3.
|
5
|
+
version: 0.3.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- whtt-eric
|
@@ -233,7 +233,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
233
233
|
requirements:
|
234
234
|
- - ">="
|
235
235
|
- !ruby/object:Gem::Version
|
236
|
-
hash:
|
236
|
+
hash: 4150440090271491481
|
237
237
|
segments:
|
238
238
|
- 0
|
239
239
|
version: "0"
|