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_options, get, handle_errors, post
Class Method Details
.find(id) ⇒ MonkeyWrench::List
Finds a given list by ID
39 40 41 |
# File 'lib/monkey_wrench/list.rb', line 39 def self.find(id) new(:id => id) end |
.find_all ⇒ Array<MonkeyWrench::List> Also known as: all
Finds all lists
49 50 51 52 53 |
# File 'lib/monkey_wrench/list.rb', line 49 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
13 14 15 |
# File 'lib/monkey_wrench/list.rb', line 13 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)
28 29 30 |
# File 'lib/monkey_wrench/list.rb', line 28 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.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/monkey_wrench/list.rb', line 99 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
152 153 154 155 156 157 158 159 |
# File 'lib/monkey_wrench/list.rb', line 152 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/monkey_wrench/list.rb', line 71 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
238 239 240 241 242 |
# File 'lib/monkey_wrench/list.rb', line 238 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.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/monkey_wrench/list.rb', line 182 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
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/monkey_wrench/list.rb', line 213 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
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/monkey_wrench/list.rb', line 132 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 |