Class: MonkeyWrench::List
- Defined in:
- lib/monkey_wrench/list.rb
Class Method Summary collapse
-
.find(id) ⇒ MonkeyWrench::List
Finds a given list by ID.
-
.find_all ⇒ Array<MonkeyWrench::List>
(also: all)
Finds all lists.
-
.find_by_name(list_name) ⇒ MonkeyWrench::List
Finds a given list by name.
Instance Method Summary collapse
-
#==(other_list) ⇒ Boolean
Will compare another list against the current one and return true if they are the same (based on list ID).
-
#each_member(&block) ⇒ Object
Enumerates over each member and executes the provided block.
-
#member(email) ⇒ MonkeyWrench::Member
Find a member in this list with the given email address.
-
#members(options = {}) ⇒ Array<MonkeyWrench::Member>
Returns all members for this list.
-
#opt_out(emails) ⇒ Hash
Will flag the email(s) as opted-out for all future mailing for this list.
-
#subscribe(contact_details, opts = {}) ⇒ Object
Subscribes a new member to the list.
-
#unsubscribe(emails, opts = {}) ⇒ Hash
Unsubscribes a person (or list of people) from the list.
-
#update_members(members, options = {}) ⇒ Object
Updates details of list members.
Methods inherited from Base
apikey, base_uri, datacenter, default_query_params, default_retry_limit, get, handle_errors, post
Class Method Details
.find(id) ⇒ MonkeyWrench::List
Finds a given list by ID
46 47 48 |
# File 'lib/monkey_wrench/list.rb', line 46 def self.find(id) new(:id => id) end |
.find_all ⇒ Array<MonkeyWrench::List> Also known as: all
Finds all lists
56 57 58 59 60 |
# File 'lib/monkey_wrench/list.rb', line 56 def self.find_all @@lists ||= post({ :method => "lists" }).map do |list| List.new(list) end end |
.find_by_name(list_name) ⇒ MonkeyWrench::List
Finds a given list by name
12 13 14 |
# File 'lib/monkey_wrench/list.rb', line 12 def self.find_by_name(list_name) lists = find_all.detect{|list| list.name == list_name} end |
Instance Method Details
#==(other_list) ⇒ Boolean
Will compare another list against the current one and return true if they are the same (based on list ID)
35 36 37 |
# File 'lib/monkey_wrench/list.rb', line 35 def ==(other_list) other_list.is_a?(self.class) && self.id == other_list.id end |
#each_member(&block) ⇒ Object
Enumerates over each member and executes the provided block. Will automatically page and batch requests for members.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/monkey_wrench/list.rb', line 107 def each_member(&block) page = 0 loop do batch = members(:start => page, :limit => 15000) break if batch.empty? batch.each do |member| yield member end page += 1 end end |
#member(email) ⇒ MonkeyWrench::Member
Find a member in this list with the given email address
160 161 162 163 164 165 166 167 |
# File 'lib/monkey_wrench/list.rb', line 160 def member(email) response = post(:id => self.id, :method => "listMemberInfo", :email_address => email) if response['error'] raise response['error'] else MonkeyWrench::Member.new(response) end end |
#members(options = {}) ⇒ Array<MonkeyWrench::Member>
Returns all members for this list
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/monkey_wrench/list.rb', line 79 def members( = {}) if [:since] [:since] = [:since].strftime("%Y-%m-%d %H:%M:%S") end .merge!(:id => self.id, :method => "listMembers") response = post() if [:full_details] response.map do |response_user| member(response_user["email"]) end else response.map do |response_user| MonkeyWrench::Member.new(response_user) end end end |
#opt_out(emails) ⇒ Hash
Will flag the email(s) as opted-out for all future mailing for this list
246 247 248 249 250 |
# File 'lib/monkey_wrench/list.rb', line 246 def opt_out(emails) emails = [*emails] subscribe(emails.map{|email| { :email => email }}) unsubscribe(emails, :send_goodbye => false, :send_notify => false) end |
#subscribe(contact_details, opts = {}) ⇒ Object
Subscribes a new member to the list
updating an existing member.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/monkey_wrench/list.rb', line 190 def subscribe(contact_details, opts = {}) if contact_details.is_a?(Array) return subscribe_many(contact_details, opts) else if contact_details.is_a?(Hash) email_address = contact_details.delete(:email) opts = opts.merge(contact_details) else email_address = contact_details end subscribe_one(email_address, opts) return { :success => 1, :errors => []} end end |
#unsubscribe(emails, opts = {}) ⇒ Hash
Unsubscribes a person (or list of people) from the list
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/monkey_wrench/list.rb', line 221 def unsubscribe(emails, opts = {}) emails = [*emails] params = { :method => "listBatchUnsubscribe", :id => self.id } params[:delete_member] = opts[:delete_member] if opts.has_key?(:delete_member) params[:send_goodbye] = opts[:send_goodbye] if opts.has_key?(:send_goodbye) params[:send_notify] = opts[:send_notify] if opts.has_key?(:send_notify) params.merge!({ :emails => emails }.to_mailchimp) response = post(params) return { :success => response["success_count"], :errors => response["errors"] } end |
#update_members(members, options = {}) ⇒ Object
Updates details of list members
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/monkey_wrench/list.rb', line 140 def update_members(members, = {}) members = members.is_a?(Array) ? members : [members] .merge!(:id => self.id, :method => "listUpdateMember") members.each do |member| mailchimp_args = {:email_address => member[:email]} member[:email] = member[:new_email] member.delete(:new_email) mailchimp_args.merge!({ :merge_vars => member }.to_mailchimp) post(.merge(mailchimp_args)) end end |