mailchimp_subscriber 0.1.0 → 0.1.22

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,53 +1 @@
1
- module MailchimpSubscriber
2
- require 'hominid'
3
- attr_accessor :api_key, :email, :fields, :content_type, :double_optin, :update_existing, :replace_interests, :send_welcome, :delete_member, :send_goodbye, :send_notify
4
-
5
- def subscribe_to(list_name,options={})
6
- after_create do |record|
7
- begin
8
- subscribe(record,list_name,options)
9
- rescue RuntimeError
10
- end
11
- end
12
-
13
- after_destroy do |record|
14
- begin
15
- unsubscribe(record,list_name,options)
16
- rescue RuntimeError
17
- end
18
- end
19
- end
20
-
21
- def self.connect(mailchimp_api_key)
22
- Hominid::API.new(mailchimp_api_key)
23
- end
24
-
25
- def self.parse_options(record,options)
26
- @api_key = options[:using][:api_key]
27
- @email = record.send(options[:using][:email])
28
- @fields = options[:using][:fields] ? options[:using][:fields].inject({}) { |h, (k, v)| h[k] = record.send(v); h } : {}
29
- @content_type = options[:using][:content_type] || 'html'
30
- @double_optin = options[:using][:double_optin] || false
31
- @update_existing = options[:using][:update_existing] || true
32
- @replace_interests = options[:using][:replace_interests] || false
33
- @send_welcome = options[:using][:send_welcome] || false
34
- @delete_member = options[:using][:delete_member] || true
35
- @send_goodbye = options[:using][:send_goodbye] || false
36
- @send_notify = options[:using][:send_notify] || false
37
- end
38
-
39
- def self.subscribe(record,list_name,options)
40
- parse_options(record, options)
41
- client = connect(@api_key)
42
- list_id = client.find_list_by_name(list_name)['id']
43
- client.list_subscribe(list_id, @email, @fields, @content_type, @double_optin, @update_existing, @replace_interests, @send_welcome)
44
- end
45
-
46
- def self.unsubscribe(record,list_name,options)
47
- parse_options(record, options)
48
- client = connect(@api_key)
49
- list_id = client.find_list_by_name(list_name)["id"]
50
- client.list_unsubscribe(list_id, @email, @delete_member, @send_goodbye, @send_notify)
51
- end
52
-
53
- end
1
+ require 'mailchimp_subscriber/railtie'
@@ -0,0 +1,59 @@
1
+ module MailchimpSubscriber
2
+ require 'hominid'
3
+ attr_accessor :api_key, :email, :fields, :content_type, :double_optin, :update_existing, :replace_interests, :send_welcome, :delete_member, :send_goodbye, :send_notify
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def subscribe_to(list_name,options={})
11
+ after_create do |record|
12
+ begin
13
+ MailchimpSubscriber.subscribe(record,list_name,options)
14
+ rescue RuntimeError
15
+ end
16
+ end
17
+
18
+ after_destroy do |record|
19
+ begin
20
+ MailchimpSubscriber.unsubscribe(record,list_name,options)
21
+ rescue RuntimeError
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.connect(mailchimp_api_key)
28
+ Hominid::API.new(mailchimp_api_key)
29
+ end
30
+
31
+ def self.parse_options(record,options)
32
+ @api_key = options[:using][:api_key]
33
+ @email = record.send(options[:using][:email])
34
+ @fields = options[:using][:fields] ? options[:using][:fields].inject({}) { |h, (k, v)| h[k] = record.send(v); h } : {}
35
+ @content_type = options[:using][:content_type] || 'html'
36
+ @double_optin = options[:using][:double_optin] || false
37
+ @update_existing = options[:using][:update_existing] || true
38
+ @replace_interests = options[:using][:replace_interests] || false
39
+ @send_welcome = options[:using][:send_welcome] || false
40
+ @delete_member = options[:using][:delete_member] || true
41
+ @send_goodbye = options[:using][:send_goodbye] || false
42
+ @send_notify = options[:using][:send_notify] || false
43
+ end
44
+
45
+ def self.subscribe(record,list_name,options)
46
+ parse_options(record, options)
47
+ client = connect(@api_key)
48
+ list_id = client.find_list_by_name(list_name)['id']
49
+ client.list_subscribe(list_id, @email, @fields, @content_type, @double_optin, @update_existing, @replace_interests, @send_welcome)
50
+ end
51
+
52
+ def self.unsubscribe(record,list_name,options)
53
+ parse_options(record, options)
54
+ client = connect(@api_key)
55
+ list_id = client.find_list_by_name(list_name)["id"]
56
+ client.list_unsubscribe(list_id, @email, @delete_member, @send_goodbye, @send_notify)
57
+ end
58
+
59
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails'
2
+
3
+ module MailchimpSubscriber
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'mailchimp_subscriber' do |app|
6
+ require 'mailchimp_subscriber/extensions'
7
+ ::ActiveRecord::Base.send :extend, MailchimpSubscriber::ClassMethods
8
+ end
9
+ end
10
+ end
@@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "mailchimp_subscriber"
5
- s.version = '0.1.0'
5
+ s.version = '0.1.22'
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Brandon Harris"]
8
8
  s.email = ["brandon@brandon-harris.com"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailchimp_subscriber
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 55
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 22
10
+ version: 0.1.22
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brandon Harris
@@ -60,6 +60,8 @@ extra_rdoc_files: []
60
60
 
61
61
  files:
62
62
  - lib/mailchimp_subscriber.rb
63
+ - lib/mailchimp_subscriber/extensions.rb
64
+ - lib/mailchimp_subscriber/railtie.rb
63
65
  - mailchimp_subscriber.gemspec
64
66
  homepage: https://github.com/bbwharris/mailchimp_subscriber
65
67
  licenses: []