Class: Unresponsys::Row

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, fields) ⇒ Row

Returns a new instance of Row.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/unresponsys/row.rb', line 7

def initialize(table, fields)
  @table  = table
  @fields = fields

  @fields.each_pair do |key, val|
    str = key.downcase.chomp('_')
    var = "@#{str}".to_sym
    val = val.to_ruby
    self.instance_variable_set(var, val)

    if key == 'ID_' || key == 'RIID_'
      self.class.send(:attr_reader, str)
    else
      self.class.send(:define_method, "#{str}=") do |val|
        val = val.to_ruby
        self.instance_variable_set(var, val)
      end
    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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/unresponsys/row.rb', line 71

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
    @fields[field_name] = ''
    val = val.to_ruby
    self.instance_variable_set(var, val)
  else
    self.instance_variable_get(var)
  end
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

Instance Method Details

#destroyObject



61
62
63
64
65
66
67
68
# File 'lib/unresponsys/row.rb', line 61

def destroy
  fail 'Not yet implemented' if @table.extension_table?

  options   = { query: { qa: 'ID_', id: @id.to_responsys, fs: 'all' } }
  response  = @table.client.delete("/folders/#{@table.folder.name}/suppData/#{@table.name}/members", options)

  response['errorMessage'].blank?
end

#saveObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/unresponsys/row.rb', line 28

def save
  record_data = { fieldNames: [], records: [{ fieldValues: [] }], mapTemplateName: nil }

  to_h.each do |key, val|
    record_data[:fieldNames] << key
    record_data[:records][0][:fieldValues] << val
  end

  options = {
    body: {
      recordData: record_data,
      insertOnNoMatch: true,
      updateOnMatch: 'REPLACE_ALL',
    }
  }

  if @table.supplemental_table?
    url = "/folders/#{@table.folder.name}/suppData/#{@table.name}"
  else
    options[:body][:matchColumn] = 'RIID'
    url = "/lists/#{@table.list.name}/listExtensions/#{@table.name}"
  end

  options[:body] = options[:body].to_json
  r = client.post(url, options)

  if @table.supplemental_table?
    r['errorMessage'].blank?
  else
    r[0]['errorMessage'].blank?
  end
end

#to_hObject



87
88
89
90
91
92
93
94
95
# File 'lib/unresponsys/row.rb', line 87

def to_h
  hash = {}
  @fields.each_pair do |key, val|
    var = "@#{key.downcase.chomp('_')}".to_sym
    val = self.instance_variable_get(var)
    hash[key] = val.to_responsys
  end
  hash
end