Class: Airtable::Table

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

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

Methods inherited from Resource

#initialize

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(options={})
  offset = nil
  results = []
  begin
    options.merge!(:limit => LIMIT_MAX, :offset => offset)
    response = records(options)
    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(options={})
  options["sortField"], options["sortDirection"] = options.delete(:sort) if options[:sort]
  results = self.class.get(worksheet_url, query: options).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