Class: Responsys::Member

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

Defined Under Namespace

Classes: UndefinedExtensions

Constant Summary collapse

@@fields =
[]
@@system_fields =
[]
@@extension_fields =

:file => [:attribute_1, :attribute_2, :attribute_3]

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Member

Returns a new instance of Member.



18
19
20
# File 'lib/member.rb', line 18

def initialize(options = {})
  @attributes = options
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/member.rb', line 8

def attributes
  @attributes
end

Class Method Details

.add_field(f, system = false) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/member.rb', line 148

def add_field (f, system = false)
  # Prevent redundant fields
  return if @@fields.include?(f)

  @@fields << f
  @@system_fields << f if system

  define_method f do
    attributes[f]
  end

  define_method to_responsys_field(f) do
    attributes[f]
  end

  define_method "#{f}=" do |v|
    attributes[f] = v
  end
end

.clear_fields!Object



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

def clear_fields!
  @@fields.clear
  @@system_fields.clear
end

.extension_fieldsObject



47
48
49
# File 'lib/member.rb', line 47

def extension_fields
  @@extension_fields
end

.extension_fields=(h) ⇒ Object

:extension_table => [:field_1, :field_2, :field_3]



52
53
54
# File 'lib/member.rb', line 52

def extension_fields=(h)
  @@extension_fields = h
end

.fieldsObject



31
32
33
# File 'lib/member.rb', line 31

def fields
  @@fields
end

.has_field?(f) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/member.rb', line 140

def has_field? (f)
  @@fields.include?(f)
end

.has_system_field?(f) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/member.rb', line 144

def has_system_field? (f)
  @@fields.include?(f) && @@system_fields.include?(f)
end

.import_file(file_name) ⇒ Object

Parses Responsys tab-delimitted feedback file



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/member.rb', line 77

def import_file(file_name)
  members = []
  headers = nil
  table = FasterCSV.read(file_name)
  table.each do |row|
    if headers.nil?
      headers = row
    else
      member = Member.new
      headers.each do |header|
        header = from_responsys_field(header)
        value = row.shift
        member.send("#{header}=", value) if member.respond_to?("#{header}=")
      end
      members << member
    end
  end
  members
end

.member_fieldsObject



39
40
41
42
43
44
45
# File 'lib/member.rb', line 39

def member_fields
  a = []
  extension_fields.values.each do |i|
    a += i
  end
  fields.reject{ |i| a.include?(i) }
end

.parse_feedback_csv(file_name) ⇒ Object

Parses the response CSV file from Responys



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/member.rb', line 57

def parse_feedback_csv(file_name)
  results = {}
  csv = FasterCSV.read(file_name)
  headers = csv.first
  csv.shift
  csv.each do |row|
    member = Member.new
    i = 0
    headers.each do |header|
      value = row[i]
      header = from_responsys_field(header)
      member.send("#{header}=", value) if member.respond_to?("#{header}=")
      i += 1
    end
    results[member] = row.first
  end
  results
end

.responsys_fields(a = nil) ⇒ Object



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

def responsys_fields(a = nil)
  (a || @@fields).map do |i|
    to_responsys_field(i)
  end
end

.system_fieldsObject



35
36
37
# File 'lib/member.rb', line 35

def system_fields
  @@system_fields
end

.to_csv_extension_files(members, root_directory, options = {}) ⇒ Object

Produces multiple CSV files based on what has been defined for profile extensions



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/member.rb', line 105

def to_csv_extension_files(members, root_directory, options = {})
  options[:headers] = true if options[:headers].nil?
  options[:access] ||= "w"
  options[:file_token] ||= ""

  raise UndefinedExtensions if @@extension_fields.nil? || @@extension_fields.empty?
  extension_fields = []
  @@extension_fields.values.each do |i|
    extension_fields += i
  end
  extension_fields.flatten!

  # Create the primary profile CSV
  user_attributes = @@fields.reject { |i| extension_fields.include?(i) }
  user_attributes = [:customer_id] + user_attributes unless user_attributes.include?(:customer_id)
  build_csv_file(members, "#{root_directory}/member#{options[:file_token]}.csv", user_attributes, options[:headers], options[:access])

  # Create the profile extension CSVs
  @@extension_fields.each_pair do |file_name, attributes|
    attributes = [:customer_id] + attributes unless attributes.include?(:customer_id)
    build_csv_file(members, "#{root_directory}/#{file_name}#{options[:file_token]}.csv", attributes, options[:headers], options[:access])
  end
end

.to_csv_file(members, file_name, options = {}) ⇒ Object



97
98
99
100
101
102
# File 'lib/member.rb', line 97

def to_csv_file(members, file_name, options = {})
  options[:headers] = true if options[:headers].nil?
  options[:access] ||= "w"
  options[:file_token] ||= ""
  build_csv_file(members, file_name + options[:file_token], @@fields, options[:headers], options[:access])
end

Instance Method Details

#values(selected_fields = @@fields) ⇒ Object



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

def values(selected_fields = @@fields)
  a = []
  selected_fields.each do |field|
    a << attributes[field]
  end
  a
end