Class: MonkeyWrench::List
- Defined in:
- lib/monkey_wrench/list.rb
Class Method Summary collapse
-
.clear! ⇒ Object
Clears the List cache (results from List.find_all are cached for performance reasons).
-
.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.
-
#member?(email) ⇒ Boolean
Check if an email has subscribed to the list.
-
#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
.clear! ⇒ Object
Clears the List cache (results from List.find_all are cached for performance reasons)
65 66 67 |
# File 'lib/monkey_wrench/list.rb', line 65 def self.clear! @@lists = nil end |
.find(id) ⇒ MonkeyWrench::List
Finds a given list by ID
38 39 40 |
# File 'lib/monkey_wrench/list.rb', line 38 def self.find(id) find_all.find{|e| e.id == id} end |
.find_all ⇒ Array<MonkeyWrench::List> Also known as: all
Finds all lists
48 49 50 51 52 |
# File 'lib/monkey_wrench/list.rb', line 48 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)
27 28 29 |
# File 'lib/monkey_wrench/list.rb', line 27 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.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/monkey_wrench/list.rb', line 110 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
163 164 165 166 167 168 169 170 |
# File 'lib/monkey_wrench/list.rb', line 163 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 |
#member?(email) ⇒ Boolean
Check if an email has subscribed to the list
180 181 182 183 184 185 186 187 |
# File 'lib/monkey_wrench/list.rb', line 180 def member?(email) response = post(:id => self.id, :method => "listMemberInfo", :email_address => email) if response['error'] false else true end end |
#members(options = {}) ⇒ Array<MonkeyWrench::Member>
Returns all members for this list
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/monkey_wrench/list.rb', line 82 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
266 267 268 269 270 |
# File 'lib/monkey_wrench/list.rb', line 266 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.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/monkey_wrench/list.rb', line 210 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
241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/monkey_wrench/list.rb', line 241 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
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/monkey_wrench/list.rb', line 143 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 |