Class: Airtable::Table
Overview
Object corresponding to an Airtable Table
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Attributes inherited from Resource
Instance Method Summary collapse
- #add_records(records) ⇒ Array<Airtable::Record>
-
#delete_records(record_ids) ⇒ Array
Deleted record ids.
-
#dump ⇒ Object
Deletes all table’s records.
-
#initialize(token, base_id, api_response) ⇒ Table
constructor
A new instance of Table.
-
#record(record_id) ⇒ Airtable::Table
Instantiate record in table.
- #records ⇒ Array<Airtable::Record>
- #table_url ⇒ Object protected
- #update(table_data) ⇒ Airtable::Table
Methods inherited from Resource
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
#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
#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.
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 |
#dump ⇒ Object
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
28 29 30 |
# File 'lib/airtable/table.rb', line 28 def record(record_id) Airtable::Table.new(@token, @base_id, @id, record_id) end |
#records ⇒ Array<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_url ⇒ Object (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 |