Class: Airtable::Table
Constant Summary collapse
- LIMIT_MAX =
Maximum results per request
100
Instance Attribute Summary
Attributes inherited from Resource
#api_key, #app_token, #worksheet_name
Instance Method Summary collapse
-
#all(options = {}) ⇒ Object
Fetch all records iterating through offsets until retrieving the entire collection all(:sort => [“Name”, :desc]).
-
#create(record) ⇒ Object
Creates a record by posting to airtable.
-
#destroy(id) ⇒ Object
Deletes record in table based on id.
-
#find(id) ⇒ Object
Returns record based given row id.
-
#records(options = {}) ⇒ Object
Fetch records from the sheet given the list options Options: limit = 100, offset = “as345g”, sort = [“Name”, “asc”] records(:sort => [“Name”, :desc], :limit => 50, :offset => “as345g”).
-
#update(record) ⇒ Object
Replaces record in airtable based on id.
Methods inherited from Resource
Constructor Details
This class inherits a constructor from Airtable::Resource
Instance Method Details
#all(options = {}) ⇒ Object
Fetch all records iterating through offsets until retrieving the entire collection all(:sort => [“Name”, :desc])
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/airtable/table.rb', line 9 def all(={}) offset = nil results = [] begin .merge!(:limit => LIMIT_MAX, :offset => offset) response = records() results += response.records offset = response.offset end until offset.nil? || offset.empty? || results.empty? results end |
#create(record) ⇒ Object
Creates a record by posting to airtable
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/airtable/table.rb', line 37 def create(record) result = self.class.post(worksheet_url, :body => { "fields" => record.fields }.to_json, :headers => { "Content-type" => "application/json" }).parsed_response if result.present? && result["id"].present? record.override_attributes!(result_attributes(result)) record else # failed false end end |
#destroy(id) ⇒ Object
Deletes record in table based on id
63 64 65 |
# File 'lib/airtable/table.rb', line 63 def destroy(id) self.class.delete(worksheet_url + "/" + id).parsed_response end |
#find(id) ⇒ Object
Returns record based given row id
31 32 33 34 |
# File 'lib/airtable/table.rb', line 31 def find(id) result = self.class.get(worksheet_url + "/" + id).parsed_response Record.new(result_attributes(result)) if result.present? && result["id"] end |
#records(options = {}) ⇒ Object
Fetch records from the sheet given the list options Options: limit = 100, offset = “as345g”, sort = [“Name”, “asc”] records(:sort => [“Name”, :desc], :limit => 50, :offset => “as345g”)
24 25 26 27 28 |
# File 'lib/airtable/table.rb', line 24 def records(={}) ["sortField"], ["sortDirection"] = .delete(:sort) if [:sort] results = self.class.get(worksheet_url, query: ).parsed_response RecordSet.new(results) end |
#update(record) ⇒ Object
Replaces record in airtable based on id
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/airtable/table.rb', line 50 def update(record) result = self.class.put(worksheet_url + "/" + record.id, :body => { "fields" => record.fields_for_update }.to_json, :headers => { "Content-type" => "application/json" }).parsed_response if result.present? && result["id"].present? record.override_attributes!(result_attributes(result)) record else # failed false end end |