Class: Unresponsys::Member
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/unresponsys/member.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(list, fields) ⇒ Member
Returns a new instance of Member.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/unresponsys/member.rb', line 7
def initialize(list, fields)
fields = default_fields.merge(fields)
@list = list
@changed = []
fields.each_pair do |key, val|
str = key.downcase.chomp('_')
var = "@#{str}".to_sym
val = val.to_ruby
self.instance_variable_set(var, val)
self.class.send(:attr_reader, str)
next if immutable_fields.include?(key)
@changed << key
self.class.send(:define_method, "#{str}=") do |val|
@changed << key
val = val.to_ruby
self.instance_variable_set(var, val)
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
allow to access custom fields on new record
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/unresponsys/member.rb', line 50
def method_missing(sym, *args, &block)
setter = sym.to_s.include?('=')
str = sym.to_s.chomp('=')
var = "@#{str}".to_sym
val = args.first
if setter
field_name = str.upcase
@changed << field_name
val = val.to_ruby
self.instance_variable_set(var, val)
else
self.instance_variable_get(var)
end
end
|
Instance Attribute Details
Returns the value of attribute list.
5
6
7
|
# File 'lib/unresponsys/member.rb', line 5
def list
@list
end
|
Instance Method Details
66
67
68
|
# File 'lib/unresponsys/member.rb', line 66
def events
@events ||= Events.new(self)
end
|
#extension_tables ⇒ Object
70
71
72
|
# File 'lib/unresponsys/member.rb', line 70
def extension_tables
@extension_tables ||= ExtensionTables.new(self)
end
|
74
75
76
|
# File 'lib/unresponsys/member.rb', line 74
def messages
@messages ||= Messages.new(self)
end
|
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/unresponsys/member.rb', line 32
def save
record_data = { fieldNames: [], records: [[]], mapTemplateName: nil }
to_h.each do |key, val|
record_data[:fieldNames] << key
record_data[:records][0] << val
end
options = { body: { recordData: record_data, mergeRule: MergeRule.new.to_h }.to_json }
r = client.post("/lists/#{@list.name}/members", options)
return false if r['recordData']['records'][0][0].include?('MERGEFAILED')
@changed = ['EMAIL_ADDRESS_']
self.instance_variable_set(:@riid, r['recordData']['records'][0][0])
true
end
|
78
79
80
81
82
83
84
85
86
|
# File 'lib/unresponsys/member.rb', line 78
def to_h
hash = {}
@changed.uniq.each do |key|
var = "@#{key.downcase.chomp('_')}".to_sym
val = self.instance_variable_get(var)
hash[key] = val.to_responsys
end
hash
end
|