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.



19
20
21
22
23
24
25
# File 'lib/zoho-sdk/analytics/table.rb', line 19

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

Instance Attribute Details

#clientClient (readonly)

Returns Client object.

Returns:



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

def client
  @client
end

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#workspaceWorkspace (readonly)

Returns The table’s workspace.

Returns:



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

def workspace
  @workspace
end

Instance Method Details

#<<(row) ⇒ Object

Insert a single row into the table

Parameters:

  • row (Hash)

    Hash of row data. Column names are keys, and cell contents are values.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/zoho-sdk/analytics/table.rb', line 173

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

  params =
    row.reject do |key|
      !key.is_a?(String) || restricted.include?(key)
    end.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) ⇒ Column

Retrieve a table column by name

Parameters:

  • name (String)

    The column name

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zoho-sdk/analytics/table.rb', line 64

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

Create a new column in the table

Parameters:

  • column_name (String)

    The new column’s name

  • type (Symbol)

    Column data type. See Column::DATA_TYPES

  • opts (Hash)

    Optional arguments

Options Hash (**opts):

  • :required (Boolean)

    Should the column be mandatory. Defaults to false.

  • :description (String)

    The column’s description



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/zoho-sdk/analytics/table.rb', line 37

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

Safer delete option. Deletes rows from the table based on the given criteria.

Parameters:

  • criteria (String)

    The row criteria to match when deleting rows.



146
147
148
149
150
151
152
# File 'lib/zoho-sdk/analytics/table.rb', line 146

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

  delete!(criteria)
end

#delete!(criteria = nil) ⇒ Object

Unsafe delete. Deletes rows based on the given criteria. If not provided, all rows are deleted.

Parameters:

  • criteria (String) (defaults to: nil)

    The row criteria to match when deleting rows.



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/zoho-sdk/analytics/table.rb', line 157

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

Import data into the table using one of three methods: :append, :truncate_add, or :update_add. The :append option simply adds rows to the end of the table. When using :truncate_add, all rows are first removed and replaced by the new rows. If :update_add is selected, the :matching option must be provided to match rows to be updated by any imported row data.

Parameters:

  • import_type (Symbol)

    Import type. Must be one of: :append, :truncate_add, :update_add

  • data (Hash)

    The data to import. Must be a hash with column names as keys and cell contents as values.

  • opts (Hash)

    Optional arguments

Options Hash (**opts):

  • :matching (Array)

    Array of column names to match when using :update_add

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/zoho-sdk/analytics/table.rb', line 104

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



27
28
29
# File 'lib/zoho-sdk/analytics/table.rb', line 27

def name
  @table_name
end

#rowsObject



85
86
87
88
89
90
91
# File 'lib/zoho-sdk/analytics/table.rb', line 85

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