Class: Hominid::List

Inherits:
Base
  • Object
show all
Defined in:
lib/hominid/list.rb

Constant Summary

Constants inherited from Base

Base::MAILCHIMP_API_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_api_key, #api_keys, #apply_defaults_to, #call, #clean_merge_tags, #expire_api_key

Constructor Details

#initialize(*args) ⇒ List

Returns a new instance of List.

Raises:



11
12
13
14
15
16
17
# File 'lib/hominid/list.rb', line 11

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.last : {}
  raise StandardError.new('Please provide a List ID.') unless options[:id]
  @list_id = options.delete(:id)
  @attributes = options.delete(:attributes)
  super(options)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/hominid/list.rb', line 9

def attributes
  @attributes
end

#list_idObject (readonly)

List related methods




8
9
10
# File 'lib/hominid/list.rb', line 8

def list_id
  @list_id
end

Class Method Details

.allObject



19
20
21
22
# File 'lib/hominid/list.rb', line 19

def self.all
  # Get all lists for this mailchimp account
  new(:id => 0).call("lists").to_a.collect { |list| List.new(:id => list.delete('id'), :attributes => list) }
end

.find(id_or_web_id) ⇒ Object



39
40
41
42
43
44
# File 'lib/hominid/list.rb', line 39

def self.find(id_or_web_id)
  # List finder method
  all = self.all
  list = self.find_by_id(id_or_web_id.to_s).to_a + self.find_by_web_id(id_or_web_id.to_i).to_a
  return list.blank? ? nil : list.first
end

.find_by_id(id) ⇒ Object



34
35
36
37
# File 'lib/hominid/list.rb', line 34

def self.find_by_id(id)
  # Find list by id
  all.find { |list| (list.list_id == id.to_s) }
end

.find_by_name(name) ⇒ Object



24
25
26
27
# File 'lib/hominid/list.rb', line 24

def self.find_by_name(name)
  # Find list by name
  all.find { |list| list.attributes['name'] =~ /#{name}/ }
end

.find_by_web_id(web_id) ⇒ Object



29
30
31
32
# File 'lib/hominid/list.rb', line 29

def self.find_by_web_id(web_id)
  # Find list by name
  all.find { |list| (list.attributes['web_id'] == web_id.to_i) }
end

Instance Method Details

#create_group(group) ⇒ Object Also known as: interest_group_add



46
47
48
49
# File 'lib/hominid/list.rb', line 46

def create_group(group)
  # Add an interest group to a list
  call("listInterestGroupAdd", @list_id, group)
end

#create_tag(tag, name, required = false) ⇒ Object Also known as: merge_var_add



52
53
54
55
# File 'lib/hominid/list.rb', line 52

def create_tag(tag, name, required = false)
  # Add a merge tag to a list
  call("listMergeVarAdd", @list_id, tag, name, required)
end

#delete_group(group) ⇒ Object Also known as: interest_group_del



58
59
60
61
# File 'lib/hominid/list.rb', line 58

def delete_group(group)
  # Delete an interest group for a list
  call("listInterestGroupDel", @list_id, group)
end

#delete_tag(tag) ⇒ Object Also known as: merge_var_del



64
65
66
67
# File 'lib/hominid/list.rb', line 64

def delete_tag(tag)
  # Delete a merge tag and all its members
  call("listMergeVarDel", @list_id, tag)
end

#groupsObject Also known as: interest_groups



70
71
72
73
# File 'lib/hominid/list.rb', line 70

def groups()
  # Get the interest groups for a list
  call("listInterestGroups", @list_id)
end

#member_info(email) ⇒ Object



77
78
79
80
# File 'lib/hominid/list.rb', line 77

def member_info(email)
  # Get a member of a list
  call("listMemberInfo", @list_id, email)
end

#members(status = "subscribed", since = "2000-01-01 00:00:00", start = 0, limit = 100) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hominid/list.rb', line 82

def members(status = "subscribed", since = "2000-01-01 00:00:00", start = 0, limit = 100)
  # Get members of a list based on status
  # Select members based on one of the following statuses:
  #   'subscribed'
  #   'unsubscribed'
  #   'cleaned'
  #   'updated'
  #
  # Select members that have updated their status or profile by providing
  # a "since" date in the format of YYYY-MM-DD HH:MM:SS
  call("listMembers", @list_id, status, since, start, limit)
end

#merge_tagsObject Also known as: merge_vars



95
96
97
98
# File 'lib/hominid/list.rb', line 95

def merge_tags()
  # Get the merge tags for a list
  call("listMergeVars", @list_id)
end

#subscribe(email, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hominid/list.rb', line 101

def subscribe(email, options = {})
  # Subscribe a member to this list
  merge_tags = clean_merge_tags options[:merge_tags]
  options = apply_defaults_to({:email_type => "html"}.merge(options))
  call(
    "listSubscribe",
    @list_id,
    email,
    merge_tags,
    *options.values_at(
      :email_type,
      :double_opt_in,
      :update_existing,
      :replace_interests,
      :send_welcome
    )
  )
end

#subscribe_many(subscribers, options = {}) ⇒ Object Also known as: batch_subscribe



120
121
122
123
124
125
126
# File 'lib/hominid/list.rb', line 120

def subscribe_many(subscribers, options = {})
  # Subscribe an array of email addresses
  # subscribers(array) = [{:EMAIL => '[email protected]', :EMAIL_TYPE => 'html'}]
  subscribers = subscribers.collect { |subscriber| clean_merge_tags(subscriber) }
  options = apply_defaults_to({:update_existing => true}.merge(options))
  call("listBatchSubscribe", @list_id, subscribers, *options.values_at(:double_opt_in, :update_existing, :replace_interests))
end

#unsubscribe(current_email, options = {}) ⇒ Object



129
130
131
132
133
# File 'lib/hominid/list.rb', line 129

def unsubscribe(current_email, options = {})
  # Unsubscribe a list member
  options = apply_defaults_to({:delete_member => true}.merge(options))
  call("listUnsubscribe", @list_id, current_email, *options.values_at(:delete_member, :send_goodbye, :send_notify))
end

#unsubscribe_many(emails, options = {}) ⇒ Object Also known as: batch_unsubscribe



135
136
137
138
139
140
# File 'lib/hominid/list.rb', line 135

def unsubscribe_many(emails, options = {})
  # Unsubscribe an array of email addresses
  # emails(array) = ['[email protected]', '[email protected]']
  options = apply_defaults_to({:delete_member => true}.merge(options))
  call("listBatchUnsubscribe", @list_id, emails, *options.values_at(:delete_member, :send_goodbye, :send_notify))
end

#update_member(current_email, merge_tags = {}, email_type = "html", replace_interests = true) ⇒ Object



143
144
145
146
# File 'lib/hominid/list.rb', line 143

def update_member(current_email, merge_tags = {}, email_type = "html", replace_interests = true)
  # Update a member of this list
  call("listUpdateMember", @list_id, current_email, merge_tags, email_type, replace_interests)
end