Class: Airtable::Table
Overview
Object corresponding to an Airtable Table
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Attributes inherited from Resource
Instance Method Summary collapse
- #create_field(field_data) ⇒ Airtable::Field
- #create_records(records) ⇒ Array<Airtable::Record>
-
#data ⇒ Hash
Return table model, retrieve if not present.
-
#delete_records(record_ids) ⇒ Array
Deleted record ids.
-
#dump ⇒ Integer
Deletes all table’s records.
- #fields ⇒ Array<Airtable::Field>
-
#initialize(token, base, id, data = nil) ⇒ Table
constructor
A new instance of Table.
-
#record(record_id) ⇒ Airtable::Record
Instantiate record in table.
- #records ⇒ Array<Airtable::Record>
-
#table_url ⇒ Object
protected
Endpoint for tables.
- #update(table_data) ⇒ Airtable::Table
- #upsert_records(records, fields_to_merge_on) ⇒ Array<Airtable::Record>
Methods inherited from Resource
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
#base ⇒ Object (readonly)
Returns the value of attribute base.
5 6 7 |
# File 'lib/airtable/table.rb', line 5 def base @base end |
#name ⇒ Object (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
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 |
#data ⇒ Hash
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.
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 |
#dump ⇒ Integer
Deletes all table’s 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 |
#fields ⇒ Array<Airtable::Field>
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
48 |
# File 'lib/airtable/table.rb', line 48 def record(record_id) = Airtable::Record.new(@token, @base.id, @id, record_id) |
#records ⇒ Array<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_url ⇒ Object (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
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 |