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



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

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.



103
104
105
# File 'lib/postal/member.rb', line 103

def demographics
  @demographics
end

#emailObject

Returns the value of attribute email.



103
104
105
# File 'lib/postal/member.rb', line 103

def email
  @email
end

#idObject

Returns the value of attribute id.



103
104
105
# File 'lib/postal/member.rb', line 103

def id
  @id
end

#list_nameObject

Returns the value of attribute list_name.



103
104
105
# File 'lib/postal/member.rb', line 103

def list_name
  @list_name
end

#nameObject

Returns the value of attribute name.



103
104
105
# File 'lib/postal/member.rb', line 103

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



23
24
25
26
27
28
29
# File 'lib/postal/member.rb', line 23

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
# 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
  begin
    if soap_members = Postal.driver.selectMembers(args)
      return parse_members(soap_members)
    else
      return nil
    end
  rescue
    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.



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

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 Savon::SOAP::Fault
    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.



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

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

#unsubscribeObject



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

def unsubscribe
  list_name = @list_name
  return Postal.driver.unsubscribe(list_name, @id, @email)
end

#update_attributes(attributes = {}) ⇒ Object

Update the demographics for a user



148
149
150
151
152
# File 'lib/postal/member.rb', line 148

def update_attributes(attributes={})
  list_name = @list_name
  demos = attributes.collect { |key,value| {'Name' => key, 'Value' => value} }
  return Postal.driver.updateMemberDemographics(list_name, @id, @email, demos)
end

#update_attributes!(attributes = {}) ⇒ Object

Throw an error if demographics couldn’t be saved



156
157
158
159
160
161
162
# File 'lib/postal/member.rb', line 156

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