Class: Operationcode::Airtable

Inherits:
Object
  • Object
show all
Defined in:
lib/operationcode/airtable.rb,
lib/operationcode/airtable/version.rb

Constant Summary collapse

VERSION =
'0.2.1'

Instance Method Summary collapse

Constructor Details

#initialize(base_id:, table:) ⇒ OperationCode::Airtable

Library for the Operation code Airtable service github.com/Airtable/airtable-ruby Airtable is where we store user information, contacts, and other various settings for the Operation Code platform

Parameters:

  • base_id (String)

    base_id for the airtable base. This can be found on the tables API page

  • table (String)

    the name of the airbase table

Raises:

  • (KeyError)


17
18
19
20
21
22
23
# File 'lib/operationcode/airtable.rb', line 17

def initialize(base_id:, table:)
  api_key = ENV.fetch('AIRTABLE_API_KEY')
  raise(KeyError, 'AIRTABLE_API_KEY is blank') if api_key.delete(' ') == ''

  client = ::Airtable::Client.new(api_key)
  @table = client.table(base_id, table)
end

Instance Method Details

#allArray

Lists all records in the table

Returns:

  • (Array)


27
28
29
# File 'lib/operationcode/airtable.rb', line 27

def all
  @table.all
end

#create(record) ⇒ Airtable::Record

Creates a record in airtables

Parameters:

  • record (Hash)

    A hash containing all, some, or none of the key/value pairs of columns in the table

Returns:

  • (Airtable::Record)

    if successful, false if there was an error



35
36
37
38
# File 'lib/operationcode/airtable.rb', line 35

def create(record)
  record = ::Airtable::Record.new(record)
  @table.create record
end

#find_by(params) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
# File 'lib/operationcode/airtable.rb', line 40

def find_by(params)
  raise ArgumentError unless params.kind_of? Hash
  column, value = params.first
  result = @table.select(limit: 1, formula: "#{column} = \"#{value}\"")
  result == [] ? nil : result
end