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 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 <tt>subscribed_to_list</tt> column in some other migration.
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
- namespace :subscribed_to do
3
- match "mail_chimp" => "mail_chimp_web_hooks#create", :via => :post
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
@@ -17,7 +17,7 @@ module SubscribedTo
17
17
  end
18
18
  end
19
19
 
20
- hash_accessor :api_key, :lists, :secret_key, :enabled_models
20
+ hash_accessor :active, :api_key, :lists, :secret_key, :enabled_models
21
21
 
22
22
  def initialize(config = {})
23
23
  merge!(config)
@@ -24,7 +24,7 @@ module SubscribedTo
24
24
  #
25
25
  # 5) Click "Update"
26
26
  class WebHook
27
- LIMIT = 120
27
+ LIMIT = 10
28
28
  attr_accessor :enabled_models
29
29
 
30
30
  # Handles MailChimp webhook request.
@@ -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
 
@@ -2,7 +2,7 @@ module SubscribedTo
2
2
  module Version #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 3
5
- PATCH = 0
5
+ PATCH = 1
6
6
  BUILD = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
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
- include InstanceMethods
48
- include MailChimp::InstanceMethods if SubscribedTo.service == :mail_chimp
49
-
50
- @list_key = id.to_sym
51
-
52
- # We need to reference which models are enabled, and which list they belong to when processing the webhooks.
53
- SubscribedTo.mail_chimp_config.enabled_models[self.list_id].blank? ?
54
- SubscribedTo.mail_chimp_config.enabled_models[self.list_id] = [self.to_s] :
55
- SubscribedTo.mail_chimp_config.enabled_models[self.list_id] << self.to_s
56
-
57
- class_eval do
58
- after_create :subscribe_to_list
59
- after_update :update_list_member
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 2:01 ago
89
- pretend_now_is(Time.zone.now - 121.seconds) do
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
@@ -1,4 +1,6 @@
1
1
  SubscribedTo.setup do |config|
2
+ config.active = true
3
+
2
4
  config.service = :mail_chimp
3
5
 
4
6
  config.mail_chimp do |mail_chimp_config|
@@ -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 2:01 ago
31
- pretend_now_is(Time.zone.now - 120.seconds) do
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 1:59 from last update
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 2:01 from last update
50
- pretend_now_is(Time.zone.now + 120.seconds) do
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 2:01 ago
74
- pretend_now_is(Time.zone.now - 121.seconds) do
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 2:01 ago
103
- pretend_now_is(Time.zone.now - 121.seconds) do
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{subscribed_to}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["whtt-eric"]
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.0
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: 4004874530137465166
236
+ hash: 4150440090271491481
237
237
  segments:
238
238
  - 0
239
239
  version: "0"