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, api_response) ⇒ Table

Returns a new instance of Table.



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

def initialize(token, base_id, api_response)
  @token = token
  @base_id = base_id
  api_response.deep_symbolize_keys.each do |key, value|
    instance_variable_set(:"@#{key}", value)
  end
  self.class.headers({ 'Authorization': "Bearer #{@token}", 'Content-Type': 'application/json' })
end

Instance Attribute Details

#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

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

Note:

API maximum of 10 records at a time



46
47
48
49
50
51
52
53
# File 'lib/airtable/table.rb', line 46

def add_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, @base_id, @id, _1) }
end

#delete_records(record_ids) ⇒ Array

Returns Deleted record ids.

Returns:

  • (Array)

    Deleted record ids

See Also:



57
58
59
60
61
62
63
64
# File 'lib/airtable/table.rb', line 57

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

#dumpObject

Deletes all table’s records



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

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

#record(record_id) ⇒ Airtable::Table

Instantiate record in table

Returns:



28
29
30
# File 'lib/airtable/table.rb', line 28

def record(record_id)
  Airtable::Table.new(@token, @base_id, @id, record_id)
end

#recordsArray<Airtable::Record>



18
19
20
21
22
23
24
# File 'lib/airtable/table.rb', line 18

def records
  response = self.class.get(table_url)

  check_and_raise_error(response)

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

#table_urlObject (protected)



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

def table_url = "/v0/#{@base_id}/#{@id}"

#update(table_data) ⇒ Airtable::Table



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

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
end