norairrecord 0.1.4 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/norairrecord/client.rb +2 -1
- data/lib/norairrecord/table.rb +32 -9
- data/lib/norairrecord/version.rb +1 -1
- data/lib/norairrecord.rb +2 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2413a6757fd7aa66b83507ab161d8305bd233b29739652cc0c05302df835ed0
|
4
|
+
data.tar.gz: 468978e1acae10b550e887affa319a24a6fa4a41ab6200b0193d36866d31364c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26ee966a87a85da8c545c9ab8a49c6d654dd2fdb33891e21954b13eedbd1041c9a18e03ba06cf903a7930800166cbb8d7906db3c20ad8ea6dc92d5e250e7f68a
|
7
|
+
data.tar.gz: 696a61147478f307b86e7adceb7464ccbc1a023756a076feadc4240f27e6e2482ea9289c47b38e9995e255df20f66da16b1a675e4599baf41a54e04bc58acb01
|
data/README.md
CHANGED
@@ -26,6 +26,7 @@ stuff not in the OG:
|
|
26
26
|
* custom endpoint URL
|
27
27
|
* handy for inspecting/ratelimiting
|
28
28
|
* `Norairrecord.base_url = "https://somewhere_else"`
|
29
|
+
* or `ENV['AIRTABLE_ENDPOINT_URL']`
|
29
30
|
* custom UA
|
30
31
|
* `Norairrecord.user_agent = "i'm the reason why you're getting 429s!"`
|
31
32
|
* `Table#airtable_url`
|
@@ -48,3 +49,7 @@ stuff not in the OG:
|
|
48
49
|
Friend.all
|
49
50
|
=> [<Person>, <Person>, <Shark>]
|
50
51
|
```
|
52
|
+
* `Norairrecord::RecordNotFoundError`
|
53
|
+
* never again wonder if an error is because you goofed up an ID or you're getting ratelimited
|
54
|
+
* `where` argument on `has_many` lookups
|
55
|
+
* `Table#first`, `Table#first_where`
|
data/lib/norairrecord/client.rb
CHANGED
@@ -16,7 +16,7 @@ module Norairrecord
|
|
16
16
|
|
17
17
|
def connection
|
18
18
|
@connection ||= Faraday.new(
|
19
|
-
url: Norairrecord.base_url || "https://api.airtable.com",
|
19
|
+
url: Norairrecord.base_url || ENV['AIRTABLE_ENDPOINT_URL'] || "https://api.airtable.com",
|
20
20
|
headers: {
|
21
21
|
"Authorization" => "Bearer #{api_key}",
|
22
22
|
"User-Agent" => Norairrecord.user_agent || "Airrecord (nora's version)/#{Norairrecord::VERSION}",
|
@@ -40,6 +40,7 @@ module Norairrecord
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def handle_error(status, error)
|
43
|
+
raise RecordNotFoundError if status == 404
|
43
44
|
if error.is_a?(Hash) && error['error']
|
44
45
|
raise Error, "HTTP #{status}: #{error['error']['type']}: #{error['error']['message']}"
|
45
46
|
else
|
data/lib/norairrecord/table.rb
CHANGED
@@ -3,8 +3,15 @@ require 'rubygems' # For Gem::Version
|
|
3
3
|
module Norairrecord
|
4
4
|
class Table
|
5
5
|
class << self
|
6
|
-
|
7
|
-
|
6
|
+
attr_writer :api_key, :base_key, :table_name
|
7
|
+
|
8
|
+
def base_key
|
9
|
+
@base_key || (superclass < Table ? superclass.base_key : nil)
|
10
|
+
end
|
11
|
+
|
12
|
+
def table_name
|
13
|
+
@table_name || (superclass < Table ? superclass.table_name : nil)
|
14
|
+
end
|
8
15
|
|
9
16
|
def client
|
10
17
|
@@clients ||= {}
|
@@ -16,12 +23,12 @@ module Norairrecord
|
|
16
23
|
end
|
17
24
|
|
18
25
|
def has_many(method_name, options)
|
19
|
-
define_method(method_name.to_sym) do
|
26
|
+
define_method(method_name.to_sym) do |where: nil, sort: nil|
|
20
27
|
# Get association ids in reverse order, because Airtable's UI and API
|
21
28
|
# sort associations in opposite directions. We want to match the UI.
|
22
29
|
ids = (self[options.fetch(:column)] || []).reverse
|
23
30
|
table = Kernel.const_get(options.fetch(:class))
|
24
|
-
return table.find_many(ids) unless options[:single]
|
31
|
+
return table.find_many(ids, sort:, where:) unless options[:single]
|
25
32
|
|
26
33
|
(id = ids.first) ? table.find(id) : nil
|
27
34
|
end
|
@@ -54,12 +61,13 @@ module Norairrecord
|
|
54
61
|
end
|
55
62
|
end
|
56
63
|
|
57
|
-
def find_many(ids)
|
64
|
+
def find_many(ids, where: nil, sort: nil)
|
58
65
|
return [] if ids.empty?
|
59
66
|
|
60
67
|
or_args = ids.map { |id| "RECORD_ID() = '#{id}'"}.join(',')
|
61
68
|
formula = "OR(#{or_args})"
|
62
|
-
|
69
|
+
formula = "AND(#{formula},#{where})" if where
|
70
|
+
records(filter: formula, sort:).sort_by { |record| or_args.index(record.id) }
|
63
71
|
end
|
64
72
|
|
65
73
|
def update(id, update_hash = {}, options = {})
|
@@ -140,9 +148,24 @@ module Norairrecord
|
|
140
148
|
client.handle_error(response.status, parsed_response)
|
141
149
|
end
|
142
150
|
end
|
151
|
+
|
152
|
+
def first(options = {})
|
153
|
+
records(**options.merge(max_records: 1)).first
|
154
|
+
end
|
155
|
+
|
156
|
+
def first_where(filter, options = {})
|
157
|
+
first(options.merge(filter:))
|
158
|
+
end
|
159
|
+
|
160
|
+
def where(filter, options = {})
|
161
|
+
records(**options.merge(filter:))
|
162
|
+
end
|
163
|
+
|
143
164
|
alias all records
|
144
165
|
end
|
145
166
|
|
167
|
+
|
168
|
+
|
146
169
|
attr_reader :fields, :id, :created_at, :updated_keys
|
147
170
|
|
148
171
|
# This is an awkward definition for Ruby 3 to remain backwards compatible.
|
@@ -260,7 +283,7 @@ module Norairrecord
|
|
260
283
|
def transaction(&block)
|
261
284
|
txn_updates = {}
|
262
285
|
|
263
|
-
singleton_class.
|
286
|
+
singleton_class.define_method(:original_setter, method(:[]=))
|
264
287
|
|
265
288
|
define_singleton_method(:[]=) do |key, value|
|
266
289
|
txn_updates[key] = value
|
@@ -278,8 +301,8 @@ module Norairrecord
|
|
278
301
|
rescue => e
|
279
302
|
raise
|
280
303
|
ensure
|
281
|
-
singleton_class.
|
282
|
-
singleton_class.remove_method
|
304
|
+
singleton_class.define_method(:[]=, method(:original_setter))
|
305
|
+
singleton_class.remove_method(:original_setter)
|
283
306
|
end
|
284
307
|
result
|
285
308
|
end
|
data/lib/norairrecord/version.rb
CHANGED
data/lib/norairrecord.rb
CHANGED
@@ -11,7 +11,8 @@ module Norairrecord
|
|
11
11
|
attr_accessor :api_key, :throttle, :base_url, :user_agent
|
12
12
|
|
13
13
|
Error = Class.new(StandardError)
|
14
|
-
UnknownTypeError = Class.new(
|
14
|
+
UnknownTypeError = Class.new(Error)
|
15
|
+
RecordNotFoundError = Class.new(Error)
|
15
16
|
|
16
17
|
def throttle?
|
17
18
|
return true if @throttle.nil?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: norairrecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nora
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -113,7 +113,7 @@ homepage: https://github.com/24c02/norairrecord
|
|
113
113
|
licenses:
|
114
114
|
- MIT
|
115
115
|
metadata: {}
|
116
|
-
post_install_message:
|
116
|
+
post_install_message:
|
117
117
|
rdoc_options: []
|
118
118
|
require_paths:
|
119
119
|
- lib
|
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
129
|
version: '0'
|
130
130
|
requirements: []
|
131
131
|
rubygems_version: 3.5.16
|
132
|
-
signing_key:
|
132
|
+
signing_key:
|
133
133
|
specification_version: 4
|
134
134
|
summary: Airtable client
|
135
135
|
test_files: []
|