Class: Postal::Member

Inherits:
Base
  • Object
show all
Defined in:
lib/postal/member.rb

Constant Summary collapse

DEFAULT_ATTRIBUTES =
{ :id => nil, :email => nil, :name => nil, :list_name => nil, :demographics => {} }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

all, create, create!, find

Constructor Details

#initialize(attributes = {}) ⇒ Member

Create a new member instance



94
95
96
97
98
99
100
101
# File 'lib/postal/member.rb', line 94

def initialize(attributes={})
  attributes = DEFAULT_ATTRIBUTES.merge(attributes)
  @id = attributes[:id]
  @email = attributes[:email]
  @name = attributes[:name]
  @list_name = attributes[:list_name] || Postal.options[:list_name]
  @demographics = attributes[:demographics]
end

Instance Attribute Details

#demographicsObject

Returns the value of attribute demographics.



90
91
92
# File 'lib/postal/member.rb', line 90

def demographics
  @demographics
end

#emailObject

Returns the value of attribute email.



90
91
92
# File 'lib/postal/member.rb', line 90

def email
  @email
end

#idObject

Returns the value of attribute id.



90
91
92
# File 'lib/postal/member.rb', line 90

def id
  @id
end

#list_nameObject

Returns the value of attribute list_name.



90
91
92
# File 'lib/postal/member.rb', line 90

def list_name
  @list_name
end

#nameObject

Returns the value of attribute name.



90
91
92
# File 'lib/postal/member.rb', line 90

def name
  @name
end

Class Method Details

.destroy(*args) ⇒ Object

Will NOT let you delete the entire list’s members (only pass a ListName) Returns the number of members that were deleted, or nil if none were



19
20
21
22
23
24
25
# File 'lib/postal/member.rb', line 19

def destroy(*args)
  unless args.find { |arg| arg.match(/ListName/) }
    args << "ListName=#{Postal.options[:list_name]}"
  end
  # raise Postal::WouldDeleteAllMembers, 'Not passing any parameters (other than ListName) to this method will delete ALL members of a list. If you really want to delete all members of this list, use destroy! instead.' if args.to_a.size == 1 && args.to_a.first.match(/ListName/)
  return Postal.driver.deleteMembers(args)
end

.find_by_filter(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/postal/member.rb', line 6

def find_by_filter(*args)
  unless args.find { |arg| arg.match(/ListName/) }
    args << "ListName=#{Postal.options[:list_name]}"
  end
  if soap_members = Postal.driver.selectMembers(args)
    return parse_members(soap_members)
  else
    return nil
  end
end

Instance Method Details

#saveObject

Save the member to Lyris and returns the member ID that was created. Returns ‘false` if the save fails.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/postal/member.rb', line 105

def save
  # if @list is a list Object then get the name out (all Lyris wants is the name)
  list_name = @list_name
  begin
    @id = Postal.driver.createSingleMember(@email, @name, list_name)
    update_attributes(@demographics) unless @demographics.empty?
    return @id
  rescue SOAP::FaultError
    return false
  end
end

#save!Object

Saves the member to Lyris and returns the member ID that was created. Throws an error if the save fails.



119
120
121
122
123
124
125
# File 'lib/postal/member.rb', line 119

def save!
  if id = save
    return id
  else
    raise Postal::CouldNotCreateMember, 'Could not create a new member. The most likely cause is that the specified list already contains this email address.'
  end
end

#update_attributes(attributes = {}) ⇒ Object

Update the demographics for a user



129
130
131
132
133
134
# File 'lib/postal/member.rb', line 129

def update_attributes(attributes={})
  list_name = @list_name
  demos = attributes.collect { |key,value| Postal::Lmapi::KeyValueType.new(value,key.to_s) }
  member = Postal::Lmapi::SimpleMemberStruct.new(list_name, @id, @email)
  return Postal.driver.updateMemberDemographics(member,demos)
end

#update_attributes!(attributes = {}) ⇒ Object

Throw an error if demographics couldn’t be saved



138
139
140
141
142
143
144
# File 'lib/postal/member.rb', line 138

def update_attributes!(attributes={})
  if update_attributes(attributes)
    return true
  else
    raise Postal::CouldNotUpdateMember, 'Could not update the member. The most likely cause is that your demographics are invalid.'
  end
end