devise_campaignable 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: daf89819dba50b5d7e703bfa070061817c2e48db
4
- data.tar.gz: 4b00b9c8067dc9bda6e7130df83a9727c2a1fc93
3
+ metadata.gz: 14a7a24f8cd973480789f970ba22350b78757432
4
+ data.tar.gz: ed115510d3be3239206b6aa0bb541fd31e6374c1
5
5
  SHA512:
6
- metadata.gz: cae27bc83a46707fe46d99266cdd0b2c3d26ad07f60adae178b19b6f623ddb36c1761a9ee5b85ddbdf930bc5e4bd2f6c6b2614e19c3265d3e5052160d443ffe7
7
- data.tar.gz: e4dcfa502a404ec130fa83ae9dc0e07dc3a055e5fc9631cf298428bfef6020059f32ada6d37f22b00a6e957859bcf8a275cc9dcffcbc2b18e2b350a1e5997f15
6
+ metadata.gz: 3da5b3a6e930f9723e2a62ab4dbc45e67b8f4f16fd5460b6365a87d90d890f6785a8fbc3b9ff14973cf06bf113fdea38463a10b4776caa54d9e403710480f9f8
7
+ data.tar.gz: 70d51c5ba5f574011aebce55bfda71235e4e5cc42e7698f608c0d44d22a0f953eb04493c87376e96743b1e80bb264d699b22691bcaf4b077f32e006ab4503281
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec"
24
24
 
25
25
  spec.add_runtime_dependency "devise", ">= 3.2.0"
26
- spec.add_runtime_dependency "gibbon", "~> 1.1"
26
+ spec.add_runtime_dependency "gibbon", "~> 2.0"
27
27
  end
@@ -7,30 +7,20 @@ module Devise
7
7
  # Subscribe an email to the instantiated list.
8
8
  def subscribe(email, merge_vars={})
9
9
  # Logic for mailchimp subcription.
10
- api.subscribe({
11
- :id => @campaignable_list_id,
12
- :email => {
13
- :email => email
14
- },
15
- :double_optin => false, # Don't require email authorization.
16
- :update_existing => true, # Don't error if adding existing subscriber.
17
- :send_welcome => false, # Don't send a welcome email when they're added to the list.
18
- :merge_vars => merge_vars # Include additional variables to be stored.
10
+ api.lists(@campaignable_list_id).members(subscriber_hash(email)).upsert(body: {
11
+ :email_address => email,
12
+ :status => "subscribed",
13
+ :merge_fields => merge_vars # Include additional variables to be stored.
19
14
  })
20
15
  end
21
16
 
22
17
  # Update an existing subscription.
23
18
  def update_subscription(old_email, new_email, merge_vars={})
24
- # Append the new email address into the merge vars.
25
- merge_vars[:new_email] = new_email
26
-
27
- # Mailchimp have a handy helper for updating an existing subscription.
28
- api.update_member({
29
- :id => @campaignable_list_id,
30
- :email => {
31
- :email => old_email
32
- },
33
- :merge_vars => merge_vars # Include additional variables to be stored, including new email
19
+ # Update the existing member details.
20
+ api.lists(@campaignable_list_id).members(subscriber_hash(old_email)).update(body: {
21
+ :email_address => new_email,
22
+ :status => "subscribed",
23
+ :merge_fields => merge_vars # Include additional variables to be stored.
34
24
  })
35
25
  end
36
26
 
@@ -38,46 +28,66 @@ module Devise
38
28
  # This method is available for API Keys belonging to users with the following roles: manager, admin, owner
39
29
  def unsubscribe(email)
40
30
  # Logic for mailchimp unsubscription.
41
- api.unsubscribe({
42
- :id => @campaignable_list_id,
43
- :email => {
44
- :email => email
45
- },
46
- :send_goodbye => false, # Don't send a goodbye email.
47
- :send_notify => false # Don't notify the user of the unsubscription.
48
- })
49
- end
31
+ api.lists(@campaignable_list_id).members(subscriber_hash(email)).update(body: { status: "unsubscribed" })
32
+ end
50
33
 
51
34
  # Subscribe all users as a batch.
52
35
  def batch_subscribe(emails=[])
53
- # Do this using a batch call to the MailChimp API for performance rather than lots of single API calls.
54
- api.batch_subscribe({
55
- :id => @campaignable_list_id,
56
- :batch => emails.map {|email| {:email => { :email => email }} }, # Map all users in the system into a mailchimp collcation.
57
- :double_optin => false, # Don't require email authorization.
58
- :update_existing => true, # Don't error if adding existing subscriber.
59
- :replace_interests => false # Don't send a welcome email when they're added to the list.
60
- })
36
+ # Prepate the batch of subscribe operations to be called.
37
+ operations = []
38
+
39
+ # Create a upsert operations for each of the email addresses.
40
+ emails.each do |email|
41
+ # The operation is manually compiled as the Gibbon
42
+ # gem doesn't give us an easy way of doing this.
43
+ operations.append({
44
+ :method => "POST",
45
+ :path => "/lists/#{@campaignable_list_id}/members",
46
+ :body => {
47
+ :email_address => email,
48
+ :status => "subscribed"
49
+ }.to_json
50
+ })
51
+ end
52
+
53
+ # Make the request to process the batch of operations
54
+ api.batches.create(body: {:operations => operations})
61
55
  end
62
56
 
63
57
  # Unsubscribe all users as a batch.
64
58
  def batch_unsubscribe(emails=[])
65
- # Do this using a batch call to the MailChimp API for performance rather than lots of single API calls.
66
- api.batch_unsubscribe({
67
- :id => @campaignable_list_id,
68
- :batch => emails.map {|email| {:email => { :email => email }} }, # Map all users in the system into a mailchimp collcation.
69
- :send_goodbye => false, # Don't send a goodbye email.
70
- :send_notify => false # Don't notify the user of the unsubscription.
71
- })
59
+ # Prepate the batch of subscribe operations to be called.
60
+ operations = []
61
+
62
+ # Create a upsert operations for each of the email addresses.
63
+ emails.each do |email|
64
+ # The operation is manually compiled as the Gibbon
65
+ # gem doesn't give us an easy way of doing this.
66
+ operations.append({
67
+ :method => "PATCH",
68
+ :path => "/lists/#{@campaignable_list_id}/members/#{subscriber_hash(email)}",
69
+ :body => { :status => "unsubscribed" }.to_json
70
+ })
71
+ end
72
+
73
+ # Make the request to process the batch of operations
74
+ api.batches.create(body: {:operations => operations})
72
75
  end
73
76
 
74
77
  private
75
78
 
79
+ # Convert the members email into a hash
80
+ # to be sent to mailchimp as part of the update calls.
81
+ def subscriber_hash(email)
82
+ # Simple hash of the email address.
83
+ Digest::MD5.hexdigest(email)
84
+ end
85
+
76
86
  # Get an instance of the MailChimp API.
77
87
  def api
78
88
  # Instantiate an instance of Gibbon with the API key provided to this adapter.
79
89
  # We only work with the lists api here, so namespace into that.
80
- Gibbon::API.new(@campaignable_api_key).lists
90
+ Gibbon::Request.new(api_key: @campaignable_api_key)
81
91
  end
82
92
 
83
93
  end
@@ -1,3 +1,3 @@
1
1
  module DeviseCampaignable
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_campaignable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Rawlins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-30 00:00:00.000000000 Z
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '1.1'
75
+ version: '2.0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '1.1'
82
+ version: '2.0'
83
83
  description: Inspired by the now slightly out of date devise_mailchimp this gem works
84
84
  in a similar fashion but with a focus on multi-vendor support, rather than exclusively
85
85
  MailChimp.