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



88
89
90
91
92
93
94
95
# File 'lib/postal/member.rb', line 88

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.



84
85
86
# File 'lib/postal/member.rb', line 84

def demographics
  @demographics
end

#emailObject

Returns the value of attribute email.



84
85
86
# File 'lib/postal/member.rb', line 84

def email
  @email
end

#idObject

Returns the value of attribute id.



84
85
86
# File 'lib/postal/member.rb', line 84

def id
  @id
end

#list_nameObject

Returns the value of attribute list_name.



84
85
86
# File 'lib/postal/member.rb', line 84

def list_name
  @list_name
end

#nameObject

Returns the value of attribute name.



84
85
86
# File 'lib/postal/member.rb', line 84

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



28
29
30
31
32
33
34
# File 'lib/postal/member.rb', line 28

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
16
17
18
19
20
21
22
23
24
# 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)
    members = soap_members.collect do |member|
      demographics = {}
      member.demographics.each { |demo| demographics.merge!({ demo.name.to_sym => demo.value }) }
      Member.new(:email => member.emailAddress, :name => member.fullName, :id => member.memberID, :list_name => member.listName, :demographics => demographics)
    end
    if members.size == 1
      return members.first
    else
      return members
    end
  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.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/postal/member.rb', line 99

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.



113
114
115
116
117
118
119
# File 'lib/postal/member.rb', line 113

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



123
124
125
126
127
128
# File 'lib/postal/member.rb', line 123

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



132
133
134
135
136
137
138
# File 'lib/postal/member.rb', line 132

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