-
Notifications
You must be signed in to change notification settings - Fork 4
/
search_pagination.rb
56 lines (46 loc) · 1006 Bytes
/
search_pagination.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require "dotenv/load"
require "notion-sdk-ruby"
class ExhaustiveSearch
def initialize(query)
@query = query
@has_more = true
@next_cursor = nil
@page = 1
end
def search
data = []
until done_searching?
puts "Searching page #{@page} for 'grocery'"
result = notion_client.search(search_params)
data << result.data
@has_more = result.has_more
@next_cursor = result.next_cursor
@page += 1
sleep 1
end
data.flatten
end
private
def done_searching?
!@has_more
end
# Use #compact because the Notion API does not like null
# values in request bodies.
def search_params
@query
.merge({start_cursor: @next_cursor})
.compact
end
def notion_client
@notion_client ||= Notion::Client.new(token: ENV["NOTION_API_KEY"])
end
end
data = ExhaustiveSearch.new({
query: "grocery",
sort: {
direction: "ascending",
timestamp: "last_edited_time"
},
page_size: 3
}).search
puts data