Skip to content

Commit

Permalink
Include offset in return value of #records
Browse files Browse the repository at this point in the history
Currently, there are 2 modes for retrieving records; pagination on or
off.

If pagination is on (`paginate: true`), we keep making fetch
requests until the response does not include an `offset` for the next
page.

If pagination is off (`paginate: false`), we simply fetch the first page
and stop there.

In order to allow users to control pagination themselves (rather than
simply on or off, as above) we need to provide access to the `offset`
value returned by Airtable.
  • Loading branch information
adamransom authored and mbaird committed Feb 9, 2024
1 parent 3c5cab4 commit 921ed95
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/airrecord/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

module Airrecord
class Table
class RecordSet < SimpleDelegator
attr_reader :records, :offset

def initialize(records:, offset:)
@records = records
@offset = offset
__setobj__(@records)
end
end

class << self
attr_accessor :base_key, :table_name
attr_writer :api_key
Expand Down Expand Up @@ -99,7 +109,7 @@ def records(filter: nil, sort: nil, view: nil, offset: nil, paginate: true, fiel
))
end

records
RecordSet.new(records: records, offset: parsed_response["offset"])
else
client.handle_error(response.status, parsed_response)
end
Expand Down
8 changes: 7 additions & 1 deletion test/table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ def test_walrus_uses_default_key
end

def test_retrieve_records
assert_instance_of Array, @table.records
assert_equal 2, @table.records.size
end

def test_records_includes_offset
stub_request([{"Name" => "1"}, {"Name" => "2"}], offset: 'dasfuhiu')

assert_equal 'dasfuhiu', @table.records(paginate: false).offset
end

def test_different_clients_with_different_api_keys
Expand Down

0 comments on commit 921ed95

Please sign in to comment.