airtable2 0.2.3 → 0.2.5

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: 32bae2cc155c84b948c193c8ea5677288ea7123aeded90f6e6121ba196d0a4c7
4
- data.tar.gz: 8efc8479bf004389e25aee1bdc8b88b60dfbc395078dfa4935f4a65afa93bbe4
3
+ metadata.gz: 289287db343e46821fa736305ee64eb1a9fc08a58c662b3877cf10643ea63737
4
+ data.tar.gz: b25cca2262cc2d91ff241fbd479c58efcebdc77cb95dbe9a869f19d990593e11
5
5
  SHA512:
6
- metadata.gz: 9f0993c56aa18abf1fc80089ebf8b08d377ff70476c04b4df619dd6637a4439bf034a0af2b8f90660244464ceeefc137fd836cd99493b00415ea7108cead000b
7
- data.tar.gz: b1b30151a67ff2ce1f2eb479327ef1a959fa4ca9d671ecd33553d3a75c746f1193010e5d3e1ab457cfbd3ca9e25d385a2c85cc5d850e1a3b2766ddfd65160647
6
+ metadata.gz: 97493ce00abb1c1306f7c8d5efc0b0b0c21b9155a8f64458138e11e61b5fc9f504241137391f3bd4184fab4434d8bc0e11401407034a1ae5561b85cd64393fb5
7
+ data.tar.gz: 38f2292dc125c1f3cffb1c6a35f48ea79824029b1bd9d1b0824c09bbc8705a8b8c3791962f9bce4a9e7bd1105d3290b9b05740661cc82297815331cac028acfc
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) }
71
+ response['records'].map { Airtable::Record.new(@token, @base.id, @id, _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.3'
5
+ VERSION = '0.2.5'
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.3
4
+ version: 0.2.5
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