Class: Woods::Notion::Client
- Inherits:
-
Object
- Object
- Woods::Notion::Client
- Defined in:
- lib/woods/notion/client.rb
Overview
Thin wrapper around the Notion REST API (v2022-06-28).
Uses Net::HTTP (stdlib) for zero external dependencies. All requests are throttled through a RateLimiter to respect Notion's 3 req/sec limit.
Constant Summary collapse
- BASE_URL =
rubocop:disable Metrics/ClassLength
'https://api.notion.com/v1'- NOTION_VERSION =
'2022-06-28'- MAX_RETRIES =
3- DEFAULT_TIMEOUT =
30
Instance Method Summary collapse
-
#create_page(database_id:, properties:, children: []) ⇒ Hash
Create a page in a Notion database.
-
#find_page_by_title(database_id:, title:) ⇒ Hash?
Find a page by its title property value.
-
#initialize(api_token:, rate_limiter: RateLimiter.new) ⇒ Client
constructor
A new instance of Client.
-
#query_all(database_id:, filter: nil) ⇒ Array<Hash>
Query all pages from a database, auto-paginating.
-
#query_database(database_id:, filter: nil, sorts: nil) ⇒ Hash
Query a database with optional filter and sort.
-
#update_page(page_id:, properties:) ⇒ Hash
Update an existing page's properties.
Constructor Details
#initialize(api_token:, rate_limiter: RateLimiter.new) ⇒ Client
Returns a new instance of Client.
31 32 33 34 35 36 |
# File 'lib/woods/notion/client.rb', line 31 def initialize(api_token:, rate_limiter: RateLimiter.new) raise ArgumentError, 'api_token is required' if api_token.nil? || api_token.to_s.empty? @api_token = api_token @rate_limiter = rate_limiter end |
Instance Method Details
#create_page(database_id:, properties:, children: []) ⇒ Hash
Create a page in a Notion database.
44 45 46 47 48 49 50 51 52 |
# File 'lib/woods/notion/client.rb', line 44 def create_page(database_id:, properties:, children: []) body = { parent: { database_id: database_id }, properties: properties } body[:children] = children if children.any? request(:post, 'pages', body) end |
#find_page_by_title(database_id:, title:) ⇒ Hash?
Find a page by its title property value.
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/woods/notion/client.rb', line 107 def find_page_by_title(database_id:, title:) response = query_database( database_id: database_id, filter: { property: 'title', title: { equals: title } } ) results = response['results'] || [] results.first end |
#query_all(database_id:, filter: nil) ⇒ Array<Hash>
Query all pages from a database, auto-paginating.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/woods/notion/client.rb', line 82 def query_all(database_id:, filter: nil) all_results = [] cursor = nil loop do body = {} body[:filter] = filter if filter body[:start_cursor] = cursor if cursor response = request(:post, "databases/#{database_id}/query", body) all_results.concat(response['results'] || []) break unless response['has_more'] cursor = response['next_cursor'] end all_results end |
#query_database(database_id:, filter: nil, sorts: nil) ⇒ Hash
Query a database with optional filter and sort.
69 70 71 72 73 74 75 |
# File 'lib/woods/notion/client.rb', line 69 def query_database(database_id:, filter: nil, sorts: nil) body = {} body[:filter] = filter if filter body[:sorts] = sorts if sorts request(:post, "databases/#{database_id}/query", body) end |
#update_page(page_id:, properties:) ⇒ Hash
Update an existing page's properties.
59 60 61 |
# File 'lib/woods/notion/client.rb', line 59 def update_page(page_id:, properties:) request(:patch, "pages/#{page_id}", { properties: properties }) end |