Class: Unresponsys::Member

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/unresponsys/member.rb

Defined Under Namespace

Classes: Events, ExtensionTables

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)

    # getter
    self.class.send(:attr_reader, str)

    # setter
    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

#listObject (readonly)

Returns the value of attribute list.



5
6
7
# File 'lib/unresponsys/member.rb', line 5

def list
  @list
end

Instance Method Details

#eventsObject



66
67
68
# File 'lib/unresponsys/member.rb', line 66

def events
  @events ||= Events.new(self)
end

#extension_tablesObject



80
81
82
# File 'lib/unresponsys/member.rb', line 80

def extension_tables
  @extension_tables ||= ExtensionTables.new(self)
end

#saveObject



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

#to_hObject



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

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