norairrecord 0.1.4 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 910e4c4f488a1f1fb996144d7bc2bd023eca3d386681937af92c337d8f8e2b26
4
- data.tar.gz: 55979229a04ec5ad233f49d2858b1179af6c47dc0eaf3c7f260cc808f55083d9
3
+ metadata.gz: a5f9d1c3992d715d55a1308918a574a00b83042da2e58bfbcc9389ff51e0b6cd
4
+ data.tar.gz: d4372b35b219e5ffd6384c42e3687dd9019dabbb48d8bb622d395eb8aadab90a
5
5
  SHA512:
6
- metadata.gz: 80beee3c36c56253974af2053e669df452f0d4af0ff5ad88b05eabf2afc7673706848563d57b298a7565a88fa831700d77325b46300e48351a0607e7753d87a0
7
- data.tar.gz: 66f75e0a29877db15f9463ae95f65a3b2521f623c0ffa45bf3ddbfb261ff5f67b049d5df68d1f58fcaa21e14988918ecefe4d53d9fab0fcae4c3371a556ab690
6
+ metadata.gz: 0fdcca5fc8ee492d692fe1271d60052d82ddf4c18ff4ec275e464cf9871dd52cf9996e393ac01292bc780a31dc0681c92af1485282e6b11138fea35957ae1b18
7
+ data.tar.gz: f023f648cb134d4de8c0773fd1a2d0882393ea7109af105f2cd9b23fde4d1eeb9d519ee695ac341912a191d3d6a3b6b946efb3d7c8bc6d60ecdaf387dacfcef5
data/README.md CHANGED
@@ -48,3 +48,7 @@ stuff not in the OG:
48
48
  Friend.all
49
49
  => [<Person>, <Person>, <Shark>]
50
50
  ```
51
+ * `Norairrecord::RecordNotFoundError`
52
+ * never again wonder if an error is because you goofed up an ID or you're getting ratelimited
53
+ * `where` argument on `has_many` lookups
54
+ * `Table#first`, `Table#first_where`
@@ -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
@@ -3,8 +3,15 @@ require 'rubygems' # For Gem::Version
3
3
  module Norairrecord
4
4
  class Table
5
5
  class << self
6
- attr_accessor :base_key, :table_name
7
- attr_writer :api_key
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
- records(filter: formula).sort_by { |record| or_args.index(record.id) }
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.
@@ -1,3 +1,3 @@
1
1
  module Norairrecord
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
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(StandardError)
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
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nora
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-06 00:00:00.000000000 Z
11
+ date: 2025-01-24 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: []