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



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/airtable/table.rb', line 59

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



96
97
98
# File 'lib/airtable/table.rb', line 96

def destroy(id)
  self.class.delete(worksheet_url + "/" + id).parsed_response
end

#find(id) ⇒ Object

Returns record based given row id



53
54
55
56
# File 'lib/airtable/table.rb', line 53

def find(id)
  result = self.class.get(worksheet_url + "/" + id).parsed_response
  Record.new(result_attributes(result)) if result.present? && result["id"]
end

#raise_bad_formula_errorObject

Raises:

  • (ArgumentError)


48
49
50
# File 'lib/airtable/table.rb', line 48

def raise_bad_formula_error
  raise ArgumentError.new("The value for filter should be a String.")
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

#select(options = {}) ⇒ Object

Query for records using a string formula Options: limit = 100, offset = “as345g”, sort = [“Name”, “asc”],

fields = [Name, Email], formula = "Count > 5", view = "Main View"

select(limit: 10, sort: [“Name”, “asc”], formula: “Order < 2”)



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/airtable/table.rb', line 35

def select(options={})
  options['sortField'], options['sortDirection'] = options.delete(:sort) if options[:sort]
  options['maxRecords'] = options.delete(:limit) if options[:limit]

  if options[:formula]
    raise_bad_formula_error unless options[:formula].is_a? String
    options['filterByFormula'] = options.delete(:formula)
  end

  results = self.class.get(worksheet_url, query: options).parsed_response
  RecordSet.new(results)
end

#update(record) ⇒ Object

Replaces record in airtable based on id



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/airtable/table.rb', line 72

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

#update_record_fields(record_id, fields_for_update) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/airtable/table.rb', line 84

def update_record_fields(record_id, fields_for_update)
  result = self.class.patch(worksheet_url + "/" + record_id,
    :body => { "fields" => fields_for_update }.to_json,
    :headers => { "Content-type" => "application/json" }).parsed_response
  if result.present? && result["id"].present?
    Record.new(result_attributes(result))
  else # failed
    false
  end
end