Module: MailchimpSubscriber

Defined in:
lib/mailchimp_subscriber/railtie.rb,
lib/mailchimp_subscriber/extensions.rb

Defined Under Namespace

Modules: ClassMethods Classes: Railtie

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def api_key
  @api_key
end

#content_typeObject

Returns the value of attribute content_type.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def content_type
  @content_type
end

#delete_memberObject

Returns the value of attribute delete_member.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def delete_member
  @delete_member
end

#double_optinObject

Returns the value of attribute double_optin.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def double_optin
  @double_optin
end

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def email
  @email
end

#fieldsObject

Returns the value of attribute fields.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def fields
  @fields
end

#replace_interestsObject

Returns the value of attribute replace_interests.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def replace_interests
  @replace_interests
end

#send_goodbyeObject

Returns the value of attribute send_goodbye.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def send_goodbye
  @send_goodbye
end

#send_notifyObject

Returns the value of attribute send_notify.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def send_notify
  @send_notify
end

#send_welcomeObject

Returns the value of attribute send_welcome.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def send_welcome
  @send_welcome
end

#update_existingObject

Returns the value of attribute update_existing.



3
4
5
# File 'lib/mailchimp_subscriber/extensions.rb', line 3

def update_existing
  @update_existing
end

Class Method Details

.connect(mailchimp_api_key) ⇒ Object



27
28
29
# File 'lib/mailchimp_subscriber/extensions.rb', line 27

def self.connect(mailchimp_api_key)
  Hominid::API.new(mailchimp_api_key)
end

.included(base) ⇒ Object



5
6
7
# File 'lib/mailchimp_subscriber/extensions.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

.parse_options(record, options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mailchimp_subscriber/extensions.rb', line 31

def self.parse_options(record,options)
  @api_key = options[:using][:api_key]
  @email = record.send(options[:using][:email])
  @fields = options[:using][:fields] ? options[:using][:fields].inject({}) { |h, (k, v)| h[k] = record.send(v); h } : {}
  @content_type = options[:using][:content_type] || 'html'
  @double_optin = options[:using][:double_optin] || false
  @update_existing = options[:using][:update_existing] || true
  @replace_interests = options[:using][:replace_interests] || false
  @send_welcome = options[:using][:send_welcome] || false
  @delete_member = options[:using][:delete_member] || true
  @send_goodbye = options[:using][:send_goodbye] || false
  @send_notify = options[:using][:send_notify] || false
end

.subscribe(record, list_name, options) ⇒ Object



45
46
47
48
49
50
# File 'lib/mailchimp_subscriber/extensions.rb', line 45

def self.subscribe(record,list_name,options)
  parse_options(record, options)
  client = connect(@api_key)
  list_id = client.find_list_by_name(list_name)['id']
  client.list_subscribe(list_id, @email, @fields, @content_type, @double_optin, @update_existing, @replace_interests, @send_welcome)
end

.unsubscribe(record, list_name, options) ⇒ Object



52
53
54
55
56
57
# File 'lib/mailchimp_subscriber/extensions.rb', line 52

def self.unsubscribe(record,list_name,options)
  parse_options(record, options)
  client = connect(@api_key)
  list_id = client.find_list_by_name(list_name)["id"]
  client.list_unsubscribe(list_id, @email, @delete_member, @send_goodbye, @send_notify)
end