Class: Airtable::Table

Inherits:
Resource show all
Defined in:
lib/airtable/table.rb

Overview

Object corresponding to an Airtable Table

Instance Attribute Summary collapse

Attributes inherited from Resource

#id, #token

Instance Method Summary collapse

Methods inherited from Resource

#check_and_raise_error

Constructor Details

#initialize(token, base, id, data = nil) ⇒ Table

Returns a new instance of Table.



7
8
9
10
11
12
# File 'lib/airtable/table.rb', line 7

def initialize(token, base, id, data = nil)
  super(token)
  @base = base
  @id = id
  @data = data
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/airtable/table.rb', line 5

def base
  @base
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/airtable/table.rb', line 5

def name
  @name
end

Instance Method Details

#create_field(field_data) ⇒ Airtable::Field



21
22
23
24
25
26
27
28
# File 'lib/airtable/table.rb', line 21

def create_field(field_data)
  response = self.class.post("/v0/meta/bases/#{@base.id}/tables/#{@id}/fields",
                             body: field_data.to_json).parsed_response

  check_and_raise_error(response)

  Airtable::Field.new(@token, self, response['id'], response)
end

#create_records(records) ⇒ Array<Airtable::Record>

Note:

API maximum of 10 records at a time

Parameters:

  • Record (Array)

    objects to create

Returns:

See Also:



65
66
67
68
69
70
71
72
# File 'lib/airtable/table.rb', line 65

def create_records(records)
  response = self.class.post(table_url,
                             body: { records: Array(records).map { |fields| { fields: } } }.to_json).parsed_response

  check_and_raise_error(response)

  response['records'].map { Airtable::Record.new(@token, self, _1['id'], _1) }
end

#dataHash

Return table model, retrieve if not present



17
# File 'lib/airtable/table.rb', line 17

def data = @data ||= base.tables.find { _1.id == @id }.data

#delete_records(record_ids) ⇒ Array

Note:

API maximum of 10 records at a time

Returns Deleted record ids.

Parameters:

  • IDs (Array)

    of records to delete

Returns:

  • (Array)

    Deleted record ids

See Also:



91
92
93
94
95
96
97
98
# File 'lib/airtable/table.rb', line 91

def delete_records(record_ids)
  params = Array(record_ids).compact.map { "records[]=#{_1}" }.join('&')
  response = self.class.delete("#{table_url}?#{params}").parsed_response

  check_and_raise_error(response)

  record_ids
end

#dumpInteger

Deletes all table’s records

Returns:

  • (Integer)

    Number of deleted records



102
103
104
105
106
107
108
109
# File 'lib/airtable/table.rb', line 102

def dump
  ids = records.map(&:id)
  ids.each_slice(10) do |record_id_set|
    delete_records(record_id_set)
    sleep(0.2)
  end
  ids.size
end

#fieldsArray<Airtable::Field>

Returns:



43
# File 'lib/airtable/table.rb', line 43

def fields = @fields ||= data['fields'].map { Airtable::Field.new(@token, self, _1['id'], _1) }

#record(record_id) ⇒ Airtable::Record

Instantiate record in table

Parameters:

  • record_id (String)

    ID of record

Returns:



48
# File 'lib/airtable/table.rb', line 48

def record(record_id) = Airtable::Record.new(@token, @base.id, @id, record_id)

#recordsArray<Airtable::Record>



32
33
34
35
36
37
38
39
40
# File 'lib/airtable/table.rb', line 32

def records
  @records ||= begin
    response = self.class.get(table_url)

    check_and_raise_error(response)

    response['records'].map { Airtable::Record.new(@token, self, _1['id'], _1) }
  end
end

#table_urlObject (protected)

Endpoint for tables



114
# File 'lib/airtable/table.rb', line 114

def table_url = "/v0/#{@base.id}/#{@id}"

#update(table_data) ⇒ Airtable::Table



52
53
54
55
56
57
58
59
# File 'lib/airtable/table.rb', line 52

def update(table_data)
  response = self.class.patch("/v0/meta/bases/#{@base.id}/tables/#{@id}",
                              body: table_data.to_json).parsed_response

  check_and_raise_error(response)

  Airtable::Table.new @token, @base.id, response['id'], response
end

#upsert_records(records, fields_to_merge_on) ⇒ Array<Airtable::Record>

Note:

API maximum of 10 records at a time

Parameters:

  • Record (Array)

    objects to upsert

Returns:

See Also:



78
79
80
81
82
83
84
85
# File 'lib/airtable/table.rb', line 78

def upsert_records(records, fields_to_merge_on)
  response = self.class.patch(table_url,
                              body: { performUpsert: { fieldsToMergeOn: fields_to_merge_on }, records: Array(records).map { |fields| { fields: } } }.to_json).parsed_response

  check_and_raise_error(response)

  response['records'].map { Airtable::Record.new(@token, self, _1['id'], _1) }
end