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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/unresponsys/row.rb', line 65

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

#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
60
61
62
# File 'lib/unresponsys/row.rb', line 28

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

  @fields.each_pair do |key, val|
    record_data[:fieldNames] << key
    var = "@#{key.downcase.chomp('_')}".to_sym
    val = self.instance_variable_get(var)
    val = val.to_responsys
    record_data[:records][0][:fieldValues] << val
  end

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

  if @table.class == Unresponsys::SupplementalTable
    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.class == Unresponsys::SupplementalTable
    r['errorMessage'].blank?
  else
    r[0]['errorMessage'].blank?
  end
end