Class: ZohoSdk::Analytics::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho-sdk/analytics/table.rb

Constant Summary collapse

IMPORT_TYPES =
{
  :append => "APPEND",
  :truncate_add => "TRUNCATEADD",
  :update_add => "UPDATEADD"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, workspace, client, columns: []) ⇒ Table

Returns a new instance of Table.



16
17
18
19
20
21
22
# File 'lib/zoho-sdk/analytics/table.rb', line 16

def initialize(table_name, workspace, client, columns: [])
  @table_name = table_name
  @workspace = workspace
  @client = client
  @columns = columns
  @exists
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/zoho-sdk/analytics/table.rb', line 14

def client
  @client
end

#columnsObject (readonly)

Returns the value of attribute columns.



13
14
15
# File 'lib/zoho-sdk/analytics/table.rb', line 13

def columns
  @columns
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



12
13
14
# File 'lib/zoho-sdk/analytics/table.rb', line 12

def workspace
  @workspace
end

Instance Method Details

#<<(row) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/zoho-sdk/analytics/table.rb', line 129

def <<(row)
  params = { "ZOHO_ACTION" => "ADDROW" }
  restricted = %w(
    ZOHO_ACTION
    ZOHO_API_VERSION
    ZOHO_OUTPUT_FORMAT
    ZOHO_ERROR_FORMAT
    authtoken
  )

  params = row.reject { |key|
    !key.is_a?(String) || restricted.include?(key)
  }.merge(params)

  res = client.get path: "#{workspace.name}/#{name}", params: params
  if res.success?
    data = JSON.parse(res.body)
    columns = data.dig("response", "result", "column_order")
    values = data.dig("response", "result", "rows", 0)
    columns.zip(values).to_h
  else
    nil
  end
end

#column(name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zoho-sdk/analytics/table.rb', line 48

def column(name)
  res = client.get path: "#{workspace.name}/#{name}", params: {
    "ZOHO_ACTION" => "ISCOLUMNEXIST",
    "ZOHO_COLUMN_NAME" => name
  }
  if res.success?
    data = JSON.parse(res.body)
    if data.dig("response", "result", "iscolumnexist") == "true"
      col = Column.new(name, self, client)
      @columns << col
      col
    else
      nil
    end
  else
    nil
  end
end

#create_column(column_name, type, **opts) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zoho-sdk/analytics/table.rb', line 28

def create_column(column_name, type, **opts)
  if !Column::DATA_TYPES.values.include?(type)
    raise ArgumentError.new("Column type must be one of: #{Column::DATA_TYPES.values.join(', ')}")
  end
  @type = type
  @required = opts[:required] || false
  @description = opts[:description] || ""
  res = client.get path: "#{workspace.name}/#{name}", params: {
    "ZOHO_ACTION" => "ADDCOLUMN",
    "ZOHO_COLUMNNAME" => column_name,
    "ZOHO_DATATYPE" => type.to_s.upcase
  }
  if res.success?
    data = JSON.parse(res.body)
    Column.new(column_name, self, client)
  else
    nil
  end
end

#delete(criteria) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/zoho-sdk/analytics/table.rb', line 105

def delete(criteria)
  if criteria.nil?
    raise ArgumentError.new("Delete criteria must be specified")
  end

  delete!(criteria)
end

#delete!(criteria = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/zoho-sdk/analytics/table.rb', line 113

def delete!(criteria = nil)
  params = {
    "ZOHO_ACTION" => "DELETE"
  }

  params["ZOHO_CRITERIA"] = criteria if !criteria.nil?

  res = client.get path: "#{workspace.name}/#{name}", params: params
  if res.success?
    data = JSON.parse(res.body)
    data.dig("response", "result", "deletedrows").to_i
  else
    nil
  end
end

#import(import_type, data, **opts) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zoho-sdk/analytics/table.rb', line 73

def import(import_type, data, **opts)
  if !IMPORT_TYPES.keys.include?(import_type)
    raise ArgumentError.new("import_type must be one of: #{IMPORT_TYPES.keys.join(', ')}")
  end

  params = {
    "ZOHO_ACTION" => "IMPORT",
    "ZOHO_IMPORT_TYPE" => IMPORT_TYPES[import_type],
    "ZOHO_IMPORT_FILETYPE" => "JSON",
    "ZOHO_ON_IMPORT_ERROR" => "ABORT",
    "ZOHO_CREATE_TABLE" => "false",
    "ZOHO_AUTO_IDENTIFY" => "false"
  }

  if import_type == :update_add
    matching = opts[:matching] || []
    if !matching.is_a?(Array) || matching.size < 1
      raise ArgumentError.new("Must pass at least one column in `matching` option for UPDATEADD")
    end

    params["ZOHO_MATCHING_COLUMNS"] = matching.join(',')
  end

  res = client.post_json path: "#{workspace.name}/#{name}", io: StringIO.new(data.to_json), params: params
  if res.success?
    data = JSON.parse(res.body)
    data.dig("response", "result", "importSummary", "successRowCount")
  else
    nil
  end
end

#nameObject



24
25
26
# File 'lib/zoho-sdk/analytics/table.rb', line 24

def name
  @table_name
end

#rowsObject



67
68
69
70
71
# File 'lib/zoho-sdk/analytics/table.rb', line 67

def rows
  res = client.get path: "#{workspace.name}/#{name}", params: {
    "ZOHO_ACTION" => "EXPORT"
  }
end