airtable2 0.2.4 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93c86827ca58e71692144e71bb18ae5ab0db94fb0c1686982357e62516bee07f
4
- data.tar.gz: e4300c97ab34a37fc370ff796761d436afca0e7cd2a4d629c7debef61a490d71
3
+ metadata.gz: 183776a7059c794118524cb86bf5d22bc59b94626cba3181dd6b3169f084b341
4
+ data.tar.gz: 983079a2e93fba3fd1b594743c7598ad9f8e62d8d2ee1ea46b15ef3ee9a63e84
5
5
  SHA512:
6
- metadata.gz: 13a8c56ad23fe5caedb41d782baf7c39b860d6bb8f157c2d3756a71ed1bf8fcd198fe84df70038d21b4e913702a989b2981394c66b2a26a6dd7ad8f50a1a6636
7
- data.tar.gz: cdf035a603f61131726559639541b5f11cfe706625533d01c2dd7c6cc523a850442fd832e4e79027e0f6e2fb405cea2f5d4c476124f024410eccd4443a52d618
6
+ metadata.gz: 944fc1084b80554fba6071a29298c13bc888050af4450fccdff76b91ac4144a78e36d53bda708d2e7aa84c1902a90147f25344a1509e672f3468535903c3de24
7
+ data.tar.gz: ddc2f9d227aeaca38c8921efb305ace62ae888b125371057c73e8557901d7c517205b36b2a407417d4e4f5888418ca37b6f078e3c29b48f59cc558db0215d46e
data/README.md CHANGED
@@ -27,21 +27,38 @@ First, be sure to register for an [airtable](https://airtable.com) account, set
27
27
  @client = Airtable::Client.new('your.token.goes.here')
28
28
  ```
29
29
 
30
- ### Accessing Data
30
+ ### Simple Usage
31
+
32
+ #### Reading
31
33
 
32
34
  Now we can access our base
33
35
 
34
36
  ```ruby
37
+ # Retrieve with a specific ID
35
38
  @base = @client.base('appExAmPlE')
39
+ # or
40
+ @base = @client.bases.first
36
41
  ```
37
42
 
38
43
  and its tables
39
44
 
40
45
  ```ruby
41
- @tables = @base.tables
46
+ # Retrieve with a specific ID
47
+ @table = @base.table('tblExAmPle')
48
+ # or
49
+ @table = @base.tables.first
50
+ ```
51
+
52
+ and a table's records,
53
+
54
+ ```ruby
55
+ # Retrieve with a specific ID
56
+ @record = @table.record('recExAmPle')
57
+ # or
58
+ @record = @table.records.first
42
59
  ```
43
60
 
44
- and a table's records, so you can navigate the belongs_to/has_many relationships the way God intended:
61
+ so you can navigate the belongs_to/has_many relationships the way God intended:
45
62
 
46
63
  ```ruby
47
64
  @record = @client.bases.first.tables.first.records.first
@@ -50,9 +67,15 @@ and a table's records, so you can navigate the belongs_to/has_many relationships
50
67
 
51
68
  Note that objects' child records are memoized to avoid unnecessary API calls. If sensitive to stale data, be sure to use the objects instantiated most recently.
52
69
 
53
- ### Manipulating Tables
70
+ To get the fields of a table, its simply
71
+
72
+ ```ruby
73
+ @fields = @table.fields
74
+ ```
75
+
76
+ #### Writing
54
77
 
55
- Create a new table with:
78
+ Create a table in a base like so
56
79
 
57
80
  ```ruby
58
81
  @table = @base.create_table({ name: 'Names', description: 'A list of names', fields: [{ name: 'name', type: 'singleLineText' }] })
@@ -64,32 +87,38 @@ You can update at a table's metadata with the `update` method:
64
87
  @table.update({ description: 'Updated description' })
65
88
  ```
66
89
 
67
- ### Querying Records
68
-
69
- Once you have access to a table from above, we can query a set of records in the table with:
90
+ You can add a column to a table...
70
91
 
71
92
  ```ruby
72
- @records = @table.records
93
+ @field = @table.add_field({'description': 'Whether I have visited this apartment yet.', 'name': 'Visited', 'type': 'checkbox', 'options': { 'color': 'greenBright', 'icon': 'check'} })
73
94
  ```
74
95
 
75
- ### Inserting Records
96
+ ...and update it
97
+
98
+ ```ruby
99
+ @field = @field.update({'description': 'Whether I have rented this apartment yet.', 'name': 'Rented'})
100
+ ```
76
101
 
77
102
  A single record or an array of records can be inserted using the `create_records` method on a table (max 10 at a time):
78
103
 
79
104
  ```ruby
105
+ # Single
80
106
  @table.create_records({ 'Name': 'name value', 'Age': 35 })
107
+ # Array
108
+ @table.create_records([{ 'Name': 'name value', 'Age': 35 }, { 'Name': 'another name value', 'Age': 40 }])
81
109
  ```
82
110
 
83
- ### Deleting Records
84
-
85
- A single record or an array of records can be destroyed by passing their ids to the `delete_records` method on a table:
111
+ A single record or an array of records can be destroyed by passing their ids to the `delete_records` method on a table (max 10 at a time):
86
112
 
87
113
  ```ruby
88
- @record = @table.records[0]
89
- @table.delete_records(@record.id)
114
+ @records = @table.records
115
+ # Single
116
+ @table.delete_records(@records.first.id)
117
+ # Array
118
+ @table.delete_records(@records.map(&:ids))
90
119
  ```
91
120
 
92
- Or as a convenience, you can delete all records with the `dump` method
121
+ Or as a convenience, you can delete all records with the `dump` method, which will abide by the API's rate limiting.
93
122
 
94
123
  ```ruby
95
124
  @table.dump
data/lib/airtable/base.rb CHANGED
@@ -28,7 +28,7 @@ class Airtable::Base < Airtable::Resource
28
28
 
29
29
  check_and_raise_error(response)
30
30
 
31
- response['tables'].map { Airtable::Table.new(@token, @id, _1['id'], _1) }
31
+ response['tables'].map { Airtable::Table.new(@token, self, _1['id'], _1) }
32
32
  end
33
33
  end
34
34
 
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Object corresponding to an Airtable Field
4
+ class Airtable::Field < Airtable::Resource
5
+ attr_reader :base, :table, :name, :type, :options
6
+
7
+ def initialize(token, table, id, data = nil)
8
+ super(token)
9
+ @table = table
10
+ @base = @table.base
11
+ @id = id
12
+ data&.each_key do |key|
13
+ instance_variable_set(:"@#{key}", data[key])
14
+ end
15
+ end
16
+
17
+ # @return [Airtable::Field]
18
+ # @see https://airtable.com/developers/web/api/update-field
19
+ def update(field_data)
20
+ response = self.class.patch(field_url,
21
+ body: field_data.to_json).parsed_response
22
+
23
+ check_and_raise_error(response)
24
+
25
+ Airtable::Field.new(@token, @table, response)
26
+ end
27
+
28
+ protected
29
+
30
+ # Endpoint for tables
31
+ def field_url = "/v0/meta/bases/#{@base.id}/tables/#{@table.id}/fields/#{@id}"
32
+ end
@@ -2,10 +2,12 @@
2
2
 
3
3
  # Object corresponding to an Airtable Record
4
4
  class Airtable::Record < Airtable::Resource
5
- def initialize(token, base_id, table_id, id, data = nil)
5
+ attr_reader :table, :base
6
+
7
+ def initialize(token, table, id, data = nil)
6
8
  super(token)
7
- @base_id = base_id
8
- @table_id = table_id
9
+ @table = table
10
+ @base = @table.base
9
11
  @id = id
10
12
  @data = data
11
13
  end
@@ -23,10 +25,6 @@ class Airtable::Record < Airtable::Resource
23
25
  end
24
26
  end
25
27
 
26
- # Instantiate record's table
27
- # @return [Airtable::Table]
28
- def table = Airtable::Table.new(token, @base_id, @table_id)
29
-
30
28
  # @return [Airtable::Record]
31
29
  # @see https://airtable.com/developers/web/api/update-record
32
30
  def update(record_data)
@@ -35,11 +33,11 @@ class Airtable::Record < Airtable::Resource
35
33
 
36
34
  check_and_raise_error(response)
37
35
 
38
- Airtable::Record.new @token, @base_id, @table_id, response['id'], response
36
+ Airtable::Record.new @token, @base.id, @table.id, response['id'], response
39
37
  end
40
38
 
41
39
  protected
42
40
 
43
41
  # Endpoint for records
44
- def record_url = "/v0/#{@base_id}/#{@table_id}/#{@id}"
42
+ def record_url = "/v0/#{@base.id}/#{@table.id}/#{@id}"
45
43
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  # Object corresponding to an Airtable Table
4
4
  class Airtable::Table < Airtable::Resource
5
- attr_reader :name
5
+ attr_reader :name, :base
6
6
 
7
- def initialize(token, base_id, id, data = nil)
7
+ def initialize(token, base, id, data = nil)
8
8
  super(token)
9
- @base_id = base_id
9
+ @base = base
10
10
  @id = id
11
11
  @data = data
12
12
  end
@@ -16,9 +16,16 @@ class Airtable::Table < Airtable::Resource
16
16
  # @see https://airtable.com/developers/web/api/get-base-schema
17
17
  def data = @data ||= base.tables.find { _1.id == @id }.data
18
18
 
19
- # Instantiate table's base
20
- # @return [Airtable::Base]
21
- def base = Airtable::Base.new(token, @base_id)
19
+ # @return [Airtable::Field]
20
+ # @see https://airtable.com/developers/web/api/create-field
21
+ def create_field(field_data)
22
+ response = self.class.post("/v0/meta/bases/#{@base.id}/tables/#{@id}/fields",
23
+ body: field_data.to_json).parsed_response
24
+
25
+ check_and_raise_error(response)
26
+
27
+ Airtable::Field.new(@token, self, response['id'], response)
28
+ end
22
29
 
23
30
  # @return [Array<Airtable::Record>]
24
31
  # @see https://airtable.com/developers/web/api/list-records
@@ -28,24 +35,27 @@ class Airtable::Table < Airtable::Resource
28
35
 
29
36
  check_and_raise_error(response)
30
37
 
31
- response['records'].map { Airtable::Record.new(@token, @base_id, @id, _1['id'], _1) }
38
+ response['records'].map { Airtable::Record.new(@token, self, _1['id'], _1) }
32
39
  end
33
40
  end
34
41
 
42
+ # @return [Array<Airtable::Field>]
43
+ def fields = @fields ||= data['fields'].map { Airtable::Field.new(@token, self, _1['id'], _1) }
44
+
35
45
  # Instantiate record in table
36
46
  # @param record_id [String] ID of record
37
47
  # @return [Airtable::Record]
38
- def record(record_id) = Airtable::Record.new(@token, @base_id, @id, record_id)
48
+ def record(record_id) = Airtable::Record.new(@token, @base.id, @id, record_id)
39
49
 
40
50
  # @return [Airtable::Table]
41
51
  # @see https://airtable.com/developers/web/api/update-table
42
52
  def update(table_data)
43
- response = self.class.patch("/v0/meta/bases/#{@base_id}/tables/#{@id}",
53
+ response = self.class.patch("/v0/meta/bases/#{@base.id}/tables/#{@id}",
44
54
  body: table_data.to_json).parsed_response
45
55
 
46
56
  check_and_raise_error(response)
47
57
 
48
- Airtable::Table.new @token, @base_id, response['id'], response
58
+ Airtable::Table.new @token, @base.id, response['id'], response
49
59
  end
50
60
 
51
61
  # @param [Array] Record objects to create
@@ -58,7 +68,7 @@ class Airtable::Table < Airtable::Resource
58
68
 
59
69
  check_and_raise_error(response)
60
70
 
61
- response['records'].map { Airtable::Record.new(@token, @base_id, @id, _1['id'], _1) }
71
+ response['records'].map { Airtable::Record.new(@token, self, _1['id'], _1) }
62
72
  end
63
73
 
64
74
  # @param [Array] IDs of records to delete
@@ -75,15 +85,18 @@ class Airtable::Table < Airtable::Resource
75
85
  end
76
86
 
77
87
  # Deletes all table's records
88
+ # @return [Integer] Number of deleted records
78
89
  def dump
79
- records.map(&:id).each_slice(10) do |record_id_set|
90
+ ids = records.map(&:id)
91
+ ids.each_slice(10) do |record_id_set|
80
92
  delete_records(record_id_set)
81
93
  sleep(0.2)
82
94
  end
95
+ ids.size
83
96
  end
84
97
 
85
98
  protected
86
99
 
87
100
  # Endpoint for tables
88
- def table_url = "/v0/#{@base_id}/#{@id}"
101
+ def table_url = "/v0/#{@base.id}/#{@id}"
89
102
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Version
4
4
  module Airtable
5
- VERSION = '0.2.4'
5
+ VERSION = '0.2.6'
6
6
  end
data/lib/airtable.rb CHANGED
@@ -5,6 +5,7 @@ require 'json'
5
5
 
6
6
  require 'airtable/version'
7
7
  require 'airtable/resource'
8
+ require 'airtable/field'
8
9
  require 'airtable/record'
9
10
  require 'airtable/table'
10
11
  require 'airtable/base'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: airtable2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Seroff
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-10-28 00:00:00.000000000 Z
13
+ date: 2024-10-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty
@@ -171,6 +171,7 @@ files:
171
171
  - lib/airtable/base.rb
172
172
  - lib/airtable/client.rb
173
173
  - lib/airtable/error.rb
174
+ - lib/airtable/field.rb
174
175
  - lib/airtable/record.rb
175
176
  - lib/airtable/resource.rb
176
177
  - lib/airtable/table.rb