notion 1.0.0 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3ebde6eb43b792a313801584c8043c579e9fd356427df080a14c265e28cf2db
4
- data.tar.gz: 9bf0242abe3ad4c15cebbb9a1cdd65ae64b6ec76a1d8e872836a90b5c2ec9404
3
+ metadata.gz: 529d9a75b50731156f263184b27628a2ee6b0b0cd798e3f3b44e5ea3f7106c60
4
+ data.tar.gz: eb5b69ebe9b9d34215d6da17e8c2999761e9242b2618afaaa6583ee09691c65d
5
5
  SHA512:
6
- metadata.gz: 2f0b872d9f1eb4f88f05e4d8e41959291dc4b86bdb0be46582d196de095f174907b78c165c35140c7bc68b41b4cd6b7b7098ae23a3502d8adecc488e9185c03a
7
- data.tar.gz: 8c69f7a02a7374a69590b4b1cdb92767557efaf7f56e5fa7fe3f8450f78c19e4fa9e37b5db8c46e9aef6661dda8dfc56692baeff57912769f052e831af9ae594
6
+ metadata.gz: '09ec6f516f22fac9f2b9c7c2efc3df164f83a745414cb18f092a6df939625435a77883712b86e100d7062536b189ddd8115145a52528cd5cc81767029f3bd250'
7
+ data.tar.gz: b643a46d9682d2ebb612c0d0c37454344f0f9abb580c1cf258887bc7a242afdc22af155c78b87434a00c38a048b206351ad453477d5c914393b1ff4ac68aeb97
data/README.md CHANGED
@@ -1,8 +1,42 @@
1
+
2
+
1
3
  # Unofficial Notion Client for Ruby.
2
- [![Build Status](https://travis-ci.com/danmurphy1217/notion-ruby.svg?branch=master)](https://travis-ci.com/danmurphy1217/notion-ruby) [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop)
4
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f13e49a8807e4fe297273f48bd8d7a61)](https://app.codacy.com/gh/danmurphy1217/notion-ruby?utm_source=github.com&utm_medium=referral&utm_content=danmurphy1217/notion-ruby&utm_campaign=Badge_Grade)
5
+ [![Build Status](https://travis-ci.com/danmurphy1217/notion-ruby.svg?branch=master)](https://travis-ci.com/danmurphy1217/notion-ruby) [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop) [![Gem Version](https://badge.fury.io/rb/notion.svg)](https://badge.fury.io/rb/notion)
6
+
7
+ - Read the [blog post](https://towardsdatascience.com/introducing-the-notion-api-ruby-gem-d47d4a6ef0ca), which outlines why I built this and some of the functionality.
8
+ - Check out the [Gem](https://rubygems.org/gems/notion)!
9
+
10
+ ## Table of Contents
11
+ - [Unofficial Notion Client for Ruby.](#unofficial-notion-client-for-ruby)
12
+ - [Table of Contents](#table-of-contents)
13
+ - [Getting Started](#getting-started)
14
+ - [Installation](#installation)
15
+ - [Retrieving a Page](#retrieving-a-page)
16
+ - [Retrieving a CollectionView Page](#retrieving-a-collectionview-page)
17
+ - [Retrieving a Block within the Page](#retrieving-a-block-within-the-page)
18
+ - [Get a Block](#get-a-block)
19
+ - [Get a Collection View](#get-a-collection-view)
20
+ - [Creating New Blocks](#creating-new-blocks)
21
+ - [Create a block whose parent is the page](#create-a-block-whose-parent-is-the-page)
22
+ - [Create a block whose parent is another block](#create-a-block-whose-parent-is-another-block)
23
+ - [Creating New Collections](#creating-new-collections)
24
+ - [Updating Collection View Cells](#updating-collection-view-cells)
25
+ - [Troubleshooting](#troubleshooting)
26
+ - [No results returned when attempting to get a page](#no-results-returned-when-attempting-to-get-a-page)
27
+ - [Retrieve a full-page Collection View](#retrieve-a-full-page-collection-view)
3
28
 
4
29
  ## Getting Started
5
- To get started using package, you'll first need to retrieve your token_v2 credentials by signing into Notion online, navigating to the developer tools, inspecting the cookies, and finding the value associated with the **token_v2** key.
30
+ ### Installation
31
+ to install the gem:
32
+ ```ruby
33
+ gem install notion
34
+ ```
35
+ Then, place this at the top of your file:
36
+ ```ruby
37
+ require 'notion_api'
38
+ ```
39
+ To get started using the gem, you'll first need to retrieve your token_v2 credentials by signing into Notion online, navigating to the developer tools, inspecting the cookies, and finding the value associated with the **token_v2** key.
6
40
 
7
41
  From here, you can instantiate the Notion Client with the following code:
8
42
  ```ruby
@@ -28,6 +62,11 @@ The following attributes can be read from any block class instance:
28
62
  3. `parent_id`: the parent ID of the block.
29
63
  4. `type`: the type of the block.
30
64
 
65
+ To update the title of the page:
66
+ ![Update the title of a page](https://github.com/danmurphy1217/notion-ruby/blob/master/gifs/change_title.gif)
67
+
68
+ ## Retrieving a CollectionView Page
69
+ This is achieved by passing the ID of the Collection View to the `get_page` method. Currently, the full URL of a Collection View Page is not supported (next up on the features list!). Once you retrieve the Collection View Page, all of the methods exposed to a normal Collection View instance are available (such as `.rows`, `.row(<row_id>)`, and all else outlined in [Updating a Collection](#updating-collection-view-cells)).
31
70
  ## Retrieving a Block within the Page
32
71
  Now that you have retrieved a Notion Page, you have full access to the blocks on that page. You can retrieve a specific block or collection view, retrieve all children IDs (array of children IDs), or retrieve all children (array of children class instances).
33
72
 
@@ -39,6 +78,7 @@ To retrieve a specific block, you can use the `get_block` method. This method ac
39
78
  #<TextBlock id="2cbbe0bf-34cd-409b-9162-64284b33e526" title="TEST" parent_id="d2ce338f-19e8-47f5-86bd-17679f490e66">
40
79
  ```
41
80
  Any Notion Block has access to the following methods:
81
+
42
82
  1. `title=` → change the title of a block.
43
83
  ```ruby
44
84
  >>> @block = @client.get_block("2cbbe0bf-34cd-409b-9162-64284b33e526")
@@ -48,6 +88,8 @@ Any Notion Block has access to the following methods:
48
88
  >>> @block.title
49
89
  "New Title Here"
50
90
  ```
91
+ For example:
92
+ ![Update the title of a block](https://github.com/danmurphy1217/notion-ruby/blob/master/gifs/change%20block%20title.gif)
51
93
  2. `convert` → convert a block to a different type.
52
94
  ```ruby
53
95
  >>> @block = @client.get_block("2cbbe0bf-34cd-409b-9162-64284b33e526")
@@ -59,12 +101,17 @@ Any Notion Block has access to the following methods:
59
101
  >>> @new_block # new class instance returned...
60
102
  #<NotionAPI::CalloutBlock:0x00007ffb75b19ea0 id="2cbbe0bf-34cd-409b-9162-64284b33e526" title="New Title Here" parent_id="d2ce338f-19e8-47f5-86bd-17679f490e66">
61
103
  ```
104
+ For example:
105
+ ![Convert a page](https://github.com/danmurphy1217/notion-ruby/blob/master/gifs/change%20to%20todo%2C%20check.gif)
106
+
62
107
  3. `duplicate`→ duplicate the current block.
63
108
  ```ruby
64
109
  >>> @block = @client.get_block("2cbbe0bf-34cd-409b-9162-64284b33e526")
65
110
  >>> @block.duplicate # block is duplicated and placed directly after the current block
66
111
  >>> @block.duplicate("f13da22b-9012-4c49-ac41-6b7f97bd519e") # the duplicated block is placed after 'f13da22b-9012-4c49-ac41-6b7f97bd519e'
67
112
  ```
113
+ For example:
114
+ ![Convert a page](https://github.com/danmurphy1217/notion-ruby/blob/master/gifs/duplicate.gif)
68
115
  4. `move` → move a block to another location.
69
116
  ```ruby
70
117
  >>> @block = @client.get_block("2cbbe0bf-34cd-409b-9162-64284b33e526")
@@ -72,7 +119,9 @@ Any Notion Block has access to the following methods:
72
119
  >>> @block.move(@target_block) # @block moved to **after** @target_block
73
120
  >>> @block.move(@target_block, "before") # @block moved to **before** @target_block
74
121
  ```
75
- ### Get a Collection View - Table
122
+ For example:
123
+ ![move a block](https://github.com/danmurphy1217/notion-ruby/blob/master/gifs/move_before_and_after.gif)
124
+ ### Get a Collection View
76
125
  To retrieve a collection, you use the `get_collection` method. This method is designed to work with Table collections, but the codebase is actively being updated to support others:
77
126
  ```ruby
78
127
  >>> @page = @client.get_page("https://www.notion.so/danmurphy/TEST-PAGE-d2ce338f19e847f586bd17679f490e66")
@@ -101,6 +150,27 @@ ent.rb
101
150
  ```
102
151
 
103
152
  ## Creating New Blocks
153
+ Here's a high-level example:
154
+ ![create a callout a block](https://github.com/danmurphy1217/notion-ruby/blob/master/gifs/create.gif)
155
+ The block types available to the `create` method are:
156
+ 1. `DividerBlock`
157
+ 2. `TodoBlock`
158
+ 3. `CodeBlock`
159
+ 4. `HeaderBlock`
160
+ 5. `SubHeaderBlock`
161
+ 6. `SubSubHeaderBlock`
162
+ 7. `PageBlock`
163
+ 8. `ToggleBlock`
164
+ 9. `BulletedBlock`
165
+ 10. `NumberedBlock`
166
+ 11. `QuoteBlock`
167
+ 12. `CalloutBlock`
168
+ 13. `LatexBlock`
169
+ 14. `TextBlock`
170
+ 15. `ImageBlock` and
171
+ 16. `TableOfContentsBlock`.
172
+ If you want to create a collection, utilize the `create_collection` method [defined below].
173
+
104
174
  To create a new block, you have a few options:
105
175
  ### Create a block whose parent is the page
106
176
  If you want to create a new block whose parent ID is the **page**, call the `create` method on the PageBlock instance.
@@ -172,11 +242,14 @@ Let's say we have the following JSON data:
172
242
  }
173
243
  ]
174
244
  ```
175
- A new collection containing this data is created with the following code:
245
+ A new table collection view containing this data is created with the following code:
176
246
  ```ruby
177
247
  >>> @page = @client.get_page("https://www.notion.so/danmurphy/Notion-API-Testing-66447bc817f044bc81ed3cf4802e9b00")
178
248
  >>> @page.create_collection("table", "title for table", JSON.parse(File.read("./path/to/emoji_json_data.json")))
179
249
  ```
250
+ Here's an example with a larger dataset:
251
+ ![create a collection view table](https://github.com/danmurphy1217/notion-ruby/blob/master/gifs/create%20collection.gif)
252
+
180
253
  Additionally, say you already have a Table and want to add a new row with it containing the following data:
181
254
  ```ruby
182
255
  {
@@ -194,3 +267,51 @@ Additionally, say you already have a Table and want to add a new row with it con
194
267
  >>> @collection = @page.get_collection("f1664a99-165b-49cc-811c-84f37655908a")
195
268
  >>> @collection.add_row(JSON.parse(File.read("path/to/new_emoji_row.json")))
196
269
  ```
270
+
271
+ The first argument passed to `create_collection` determines which type of collection view to create. In the above example, a "table" is created, but other supported options are:
272
+ 1. list
273
+ 2. board
274
+ 3. calendar
275
+ 4. timeline
276
+ 5. gallery
277
+
278
+ ## Updating Collection View Cells
279
+ When you retrieve a `CollectionViewRow` instance with `.row(<row_id>)` or a list of `CollectionViewRow` instances with `.rows`, a handful of methods are created. Each row instance has access attributes that represent the properties in the Notion Collection View. So, let's say we are working with the following Notion Collection View:
280
+ | emoji | description | category | aliases | tags | unicode_version | ios_version |
281
+ |-------|--------------|---------------------|---------|---------|-----------------|-------------|
282
+ | 😉 | "winking face" | "Smileys & Emotion" | "wink" | "flirt" | "6.0" | "6.0" |
283
+
284
+ If you wanted to update the unicode and ios versions, you could use the following code:
285
+ ```ruby
286
+ >>> collection_view = @page.get_collection("1234567") # the ID of the collection block is 1234567
287
+ >>> rows = collection_view.rows
288
+ >>> row[0].unicode_version = "updated version here!"
289
+ >>> row[0].ios_version = "I was updated too!"
290
+ ```
291
+ Now, your Collection View will look like this:
292
+ | emoji | description | category | aliases | tags | unicode_version | ios_version |
293
+ |-------|--------------|---------------------|---------|---------|-----------------|-------------|
294
+ | 😉 | "winking face" | "Smileys & Emotion" | "wink" | "flirt" | "updated version here!" | "I was updated too!" |
295
+
296
+ You can also add new rows with the `.add_row({<data!>})` method and add new properties with the `.add_property("name_of_property", "type_of_property")` method.
297
+
298
+ **One important thing to be aware of:**
299
+ When adding a row with `.add_row`, the hash of data passed must be in the same order as it appears in your Notion Collection View.
300
+ ## Troubleshooting
301
+ ### No results returned when attempting to get a page
302
+ If an empty hash is returned when you attempt to retrieve a Notion page, you'll need to include the `x-notion-active-user-header` when instantiating the Notion Client.
303
+ The endpoint used by this wrapper to load a page is `/loadPageChunk`, check out the request headers in your developer tools Network tab.
304
+
305
+ From here, you can instantiate the Notion Client with the following code:
306
+ ```ruby
307
+ >>> @client = NotionAPI::Client.new(
308
+ "<insert_v2_token_here>",
309
+ "<insert_x_notion_active_user_header_here>"
310
+ )
311
+ ```
312
+ ### Retrieve a full-page Collection View
313
+ Currently, either a "normal" Page URL or the Page Block ID is accepted to the `get_page` method. Therefore, if you pass the full URL to the CV Table, it will raise an error:
314
+ ```text
315
+ the URL or ID passed to the get_page method must be that of a Page Block.
316
+ ```
317
+ To avoid this, you must pass only the ID of the full-page collection-view to the `get_page` method. This is next up on the features list, so passing the full URL will be supported soon:smile:
@@ -1,983 +1,23 @@
1
- # frozen_string_literal: true
1
+ require_relative "notion_types/template"
2
+ require_relative "notion_types/bulleted_block"
3
+ require_relative "notion_types/callout_block"
4
+ require_relative "notion_types/code_block"
5
+ require_relative "notion_types/collection_view_blocks"
6
+ require_relative "notion_types/column_list_block"
7
+ require_relative "notion_types/divider_block"
8
+ require_relative "notion_types/quote_block"
9
+ require_relative "notion_types/page_block"
10
+ require_relative "notion_types/image_block"
11
+ require_relative "notion_types/latex_block"
12
+ require_relative "notion_types/numbered_block"
13
+ require_relative "notion_types/header_block"
14
+ require_relative "notion_types/sub_header_block"
15
+ require_relative "notion_types/sub_sub_header"
16
+ require_relative "notion_types/table_of_contents_block"
17
+ require_relative "notion_types/text_block"
18
+ require_relative "notion_types/todo_block"
19
+ require_relative "notion_types/toggle_block"
2
20
 
3
- require_relative 'core'
4
- require 'httparty'
5
-
6
- module NotionAPI
7
- # Base Template for all blocks. Inherits core methods from the Block class defined in block.rb
8
- class BlockTemplate < Core
9
- include Utils
10
-
11
- attr_reader :id, :title, :parent_id
12
-
13
- def initialize(id, title, parent_id)
14
- @id = id
15
- @title = title
16
- @parent_id = parent_id
17
- end
18
-
19
- def title=(new_title)
20
- # ! Change the title of a block.
21
- # ! new_title -> new title for the block : ``str``
22
- request_id = extract_id(SecureRandom.hex(16))
23
- transaction_id = extract_id(SecureRandom.hex(16))
24
- space_id = extract_id(SecureRandom.hex(16))
25
- update_title(new_title.to_s, request_id, transaction_id, space_id)
26
- @title = new_title
27
- end
28
-
29
- def convert(block_class_to_convert_to)
30
- # ! convert a block from its current type to another.
31
- # ! block_class_to_convert_to -> the type of block to convert to : ``cls``
32
- if type == block_class_to_convert_to.notion_type
33
- # if converting to same type, skip and return self
34
- self
35
- else
36
- # setup cookies, headers, and grab/create static vars for request
37
- cookies = Core.options['cookies']
38
- headers = Core.options['headers']
39
- request_url = URLS[:UPDATE_BLOCK]
40
-
41
- # set random IDs for request
42
- request_id = extract_id(SecureRandom.hex(16))
43
- transaction_id = extract_id(SecureRandom.hex(16))
44
- space_id = extract_id(SecureRandom.hex(16))
45
- request_ids = {
46
- request_id: request_id,
47
- transaction_id: transaction_id,
48
- space_id: space_id
49
- }
50
-
51
- # build hash's that contain the operations to send to Notions backend
52
- convert_type_hash = Utils::BlockComponents.convert_type(@id, block_class_to_convert_to)
53
- last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
54
- last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
55
-
56
- operations = [
57
- convert_type_hash,
58
- last_edited_time_parent_hash,
59
- last_edited_time_child_hash
60
- ]
61
-
62
- request_body = build_payload(operations, request_ids)
63
- response = HTTParty.post(
64
- request_url,
65
- body: request_body.to_json,
66
- cookies: cookies,
67
- headers: headers
68
- )
69
- unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
70
- Please try again, and if issues persist open an issue in GitHub."; end
71
-
72
- block_class_to_convert_to.new(@id, @title, @parent_id)
73
-
74
- end
75
- end
76
-
77
- def duplicate(target_block = nil)
78
- # ! duplicate the block that this method is invoked upon.
79
- # ! target_block -> the block to place the duplicated block after. Can be any valid Block ID! : ``str``
80
- cookies = Core.options['cookies']
81
- headers = Core.options['headers']
82
- request_url = URLS[:UPDATE_BLOCK]
83
-
84
- new_block_id = extract_id(SecureRandom.hex(16))
85
- request_id = extract_id(SecureRandom.hex(16))
86
- transaction_id = extract_id(SecureRandom.hex(16))
87
- space_id = extract_id(SecureRandom.hex(16))
88
-
89
- root_children = children_ids(@id)
90
- sub_children = []
91
- root_children.each { |root_id| sub_children.push(children_ids(root_id)) }
92
-
93
- request_ids = {
94
- request_id: request_id,
95
- transaction_id: transaction_id,
96
- space_id: space_id
97
- }
98
- body = {
99
- pageId: @id,
100
- chunkNumber: 0,
101
- limit: 100,
102
- verticalColumns: false
103
- }
104
-
105
- user_notion_id = get_notion_id(body)
106
-
107
- block = target_block ? get(target_block) : self # allows dev to place block anywhere!
108
-
109
- duplicate_hash = Utils::BlockComponents.duplicate(type, @title, block.id, new_block_id, user_notion_id, root_children)
110
- set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(block.parent_id, new_block_id)
111
- block_location_hash = Utils::BlockComponents.block_location_add(block.parent_id, block.id, new_block_id, target_block, 'listAfter')
112
- last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(block.parent_id)
113
- last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(block.id)
114
-
115
- operations = [
116
- duplicate_hash,
117
- set_parent_alive_hash,
118
- block_location_hash,
119
- last_edited_time_parent_hash,
120
- last_edited_time_child_hash
121
- ]
122
-
123
- request_body = build_payload(operations, request_ids)
124
- response = HTTParty.post(
125
- request_url,
126
- body: request_body.to_json,
127
- cookies: cookies,
128
- headers: headers
129
- )
130
- unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
131
- Please try again, and if issues persist open an issue in GitHub."; end
132
-
133
- class_to_return = NotionAPI.const_get(Classes.select { |cls| NotionAPI.const_get(cls).notion_type == type }.join.to_s)
134
- class_to_return.new(new_block_id, @title, block.parent_id)
135
- end
136
-
137
- def move(target_block, position = 'after')
138
- # ! move the block to a new location.
139
- # ! target_block -> the targetted block to move to. : ``str``
140
- # ! position -> where the block should be listed, in positions relative to the target_block [before, after, top-child, last-child]
141
- positions_hash = {
142
- 'after' => 'listAfter',
143
- 'before' => 'listBefore'
144
- }
145
-
146
- unless positions_hash.keys.include?(position); raise ArgumentError, "Invalid position. You said: #{position}, valid options are: #{positions_hash.keys.join(', ')}"; end
147
-
148
- position_command = positions_hash[position]
149
- cookies = Core.options['cookies']
150
- headers = Core.options['headers']
151
- request_url = URLS[:UPDATE_BLOCK]
152
-
153
- request_id = extract_id(SecureRandom.hex(16))
154
- transaction_id = extract_id(SecureRandom.hex(16))
155
- space_id = extract_id(SecureRandom.hex(16))
156
-
157
- request_ids = {
158
- request_id: request_id,
159
- transaction_id: transaction_id,
160
- space_id: space_id
161
- }
162
-
163
- check_parents = (@parent_id == target_block.parent_id)
164
- set_block_dead_hash = Utils::BlockComponents.set_block_to_dead(@id) # kill the block this method is invoked on...
165
- block_location_remove_hash = Utils::BlockComponents.block_location_remove(@parent_id, @id) # remove the block this method is invoked on...
166
- parent_location_hash = Utils::BlockComponents.parent_location_add(check_parents ? @parent_id : target_block.parent_id, @id) # set parent location to alive
167
- block_location_add_hash = Utils::BlockComponents.block_location_add(check_parents ? @parent_id : target_block.parent_id, @id, target_block.id, position_command)
168
- last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
169
-
170
- if check_parents
171
- last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
172
- operations = [
173
- set_block_dead_hash,
174
- block_location_remove_hash,
175
- parent_location_hash,
176
- block_location_add_hash,
177
- last_edited_time_parent_hash,
178
- last_edited_time_child_hash
179
- ]
180
- else
181
- last_edited_time_new_parent_hash = Utils::BlockComponents.last_edited_time(target_block.parent_id)
182
- last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
183
- @parent_id = target_block.parent_id
184
- operations = [
185
- set_block_dead_hash,
186
- block_location_remove_hash,
187
- parent_location_hash,
188
- block_location_add_hash,
189
- last_edited_time_parent_hash,
190
- last_edited_time_new_parent_hash,
191
- last_edited_time_child_hash
192
- ]
193
- end
194
- request_body = build_payload(operations, request_ids)
195
- response = HTTParty.post(
196
- request_url,
197
- body: request_body.to_json,
198
- cookies: cookies,
199
- headers: headers
200
- )
201
- unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
202
- Please try again, and if issues persist open an issue in GitHub."; end
203
-
204
- self
205
- end
206
-
207
- def create(block_type, block_title, target = nil, position = 'after')
208
- # ! create a new block
209
- # ! block_type -> the type of block to create : ``cls``
210
- # ! block_title -> the title of the new block : ``str``
211
- # ! target -> the block_id that the new block should be placed after. ``str``
212
- # ! position -> 'after' or 'before'
213
- positions_hash = {
214
- 'after' => 'listAfter',
215
- 'before' => 'listBefore'
216
- }
217
- unless positions_hash.keys.include?(position); raise "Invalid position. You said: #{position}, valid options are: #{positions_hash.keys.join(', ')}"; end
218
-
219
- position_command = positions_hash[position]
220
-
221
- cookies = Core.options['cookies']
222
- headers = Core.options['headers']
223
-
224
- new_block_id = extract_id(SecureRandom.hex(16))
225
- request_id = extract_id(SecureRandom.hex(16))
226
- transaction_id = extract_id(SecureRandom.hex(16))
227
- space_id = extract_id(SecureRandom.hex(16))
228
-
229
- request_ids = {
230
- request_id: request_id,
231
- transaction_id: transaction_id,
232
- space_id: space_id
233
- }
234
-
235
- create_hash = Utils::BlockComponents.create(new_block_id, block_type.notion_type)
236
- set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(@id, new_block_id)
237
- block_location_hash = Utils::BlockComponents.block_location_add(@id, @id, new_block_id, target, position_command)
238
- last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@id)
239
- last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
240
- title_hash = Utils::BlockComponents.title(new_block_id, block_title)
241
-
242
- operations = [
243
- create_hash,
244
- set_parent_alive_hash,
245
- block_location_hash,
246
- last_edited_time_parent_hash,
247
- last_edited_time_child_hash,
248
- title_hash
249
- ]
250
-
251
- request_url = URLS[:UPDATE_BLOCK]
252
- request_body = build_payload(operations, request_ids)
253
- response = HTTParty.post(
254
- request_url,
255
- body: request_body.to_json,
256
- cookies: cookies,
257
- headers: headers
258
- )
259
- unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
260
- Please try again, and if issues persist open an issue in GitHub."; end
261
-
262
- block_type.new(new_block_id, block_title, @id)
263
- end
264
-
265
- private
266
-
267
- def get(url_or_id)
268
- # ! retrieve a Notion Block and return its instantiated class object.
269
- # ! url_or_id -> the block ID or URL : ``str``
270
- clean_id = extract_id(url_or_id)
271
-
272
- request_body = {
273
- pageId: clean_id,
274
- chunkNumber: 0,
275
- limit: 100,
276
- verticalColumns: false
277
- }
278
- jsonified_record_response = get_all_block_info(clean_id, request_body)
279
- i = 0
280
- while jsonified_record_response.empty? || jsonified_record_response['block'].empty?
281
- return {} if i >= 10
282
-
283
- jsonified_record_response = get_all_block_info(clean_id, request_body)
284
- i += 1
285
- end
286
- block_type = extract_type(clean_id, jsonified_record_response)
287
- block_parent_id = extract_parent_id(clean_id, jsonified_record_response)
288
-
289
- if block_type.nil?
290
- {}
291
- else
292
- block_class = NotionAPI.const_get(BLOCK_TYPES[block_type].to_s)
293
- if block_class == NotionAPI::CollectionView
294
- block_collection_id = extract_collection_id(clean_id, jsonified_record_response)
295
- block_view_id = extract_view_ids(clean_id, jsonified_record_response)
296
- collection_title = extract_collection_title(clean_id, block_collection_id, jsonified_record_response)
297
- block_class.new(clean_id, collection_title, block_parent_id, block_collection_id, block_view_id.join)
298
- else
299
- block_title = extract_title(clean_id, jsonified_record_response)
300
- block_class.new(clean_id, block_title, block_parent_id)
301
- end
302
- end
303
- end
304
-
305
- def update_title(new_title, request_id, transaction_id, space_id)
306
- # ! Helper method for sending POST request to change title of block.
307
- # ! new_title -> new title for the block : ``str``
308
- # ! request_id -> the unique ID for the request key. Generated using SecureRandom : ``str``
309
- # ! transaction_id -> the unique ID for the transaction key. Generated using SecureRandom: ``str``
310
- # ! transaction_id -> the unique ID for the space key. Generated using SecureRandom: ``str``
311
- # setup cookies, headers, and grab/create static vars for request
312
- cookies = Core.options['cookies']
313
- headers = Core.options['headers']
314
- request_url = URLS[:UPDATE_BLOCK]
315
-
316
- # set unique IDs for request
317
- request_ids = {
318
- request_id: request_id,
319
- transaction_id: transaction_id,
320
- space_id: space_id
321
- }
322
-
323
- # build and set operations to send to Notion
324
- title_hash = Utils::BlockComponents.title(@id, new_title)
325
- last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
326
- last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
327
- operations = [
328
- title_hash,
329
- last_edited_time_parent_hash,
330
- last_edited_time_child_hash
331
- ]
332
-
333
- request_body = build_payload(operations, request_ids) # defined in utils.rb
334
-
335
- response = HTTParty.post(
336
- request_url,
337
- body: request_body.to_json,
338
- cookies: cookies,
339
- headers: headers
340
- )
341
- response.body
342
- end
343
- end
344
-
345
- # divider block: ---------
346
- class DividerBlock < BlockTemplate
347
- @notion_type = 'divider'
348
- @type = 'divider'
349
-
350
- def type
351
- NotionAPI::DividerBlock.notion_type
352
- end
353
-
354
- class << self
355
- attr_reader :notion_type, :type
356
- end
357
- end
358
-
359
- # To-Do block: best for checklists and tracking to-dos.
360
- class TodoBlock < BlockTemplate
361
- @notion_type = 'to_do'
362
- @type = 'to_do'
363
-
364
- def type
365
- NotionAPI::TodoBlock.notion_type
366
- end
367
-
368
- class << self
369
- attr_reader :notion_type, :type
370
- end
371
-
372
- def checked=(checked_value)
373
- # ! change the checked property of the Todo Block.
374
- # ! checked_value -> boolean value used to determine whether the block should be checked [yes] or not [no] : ``str``
375
- # set static variables for request
376
- cookies = Core.options['cookies']
377
- headers = Core.options['headers']
378
- request_url = URLS[:UPDATE_BLOCK]
379
-
380
- # set unique values for request
381
- request_id = extract_id(SecureRandom.hex(16))
382
- transaction_id = extract_id(SecureRandom.hex(16))
383
- space_id = extract_id(SecureRandom.hex(16))
384
- request_ids = {
385
- request_id: request_id,
386
- transaction_id: transaction_id,
387
- space_id: space_id
388
- }
389
-
390
- if %w[yes no].include?(checked_value.downcase)
391
- checked_hash = Utils::BlockComponents.checked_todo(@id, checked_value.downcase)
392
- last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
393
- last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
394
-
395
- operations = [
396
- checked_hash,
397
- last_edited_time_parent_hash,
398
- last_edited_time_child_hash
399
- ]
400
- request_body = build_payload(operations, request_ids)
401
- response = HTTParty.post(
402
- request_url,
403
- body: request_body.to_json,
404
- cookies: cookies,
405
- headers: headers
406
- )
407
- unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
408
- Please try again, and if issues persist open an issue in GitHub."; end
409
-
410
- true
411
- else
412
- false
413
- end
414
- end
415
- end
416
-
417
- # Code block: used to store code, should be assigned a coding language.
418
- class CodeBlock < BlockTemplate
419
- @notion_type = 'code'
420
- @type = 'code'
421
-
422
- def type
423
- NotionAPI::CodeBlock.notion_type
424
- end
425
-
426
- class << self
427
- attr_reader :notion_type, :type
428
- end
429
- end
430
-
431
- # Header block: H1
432
- class HeaderBlock < BlockTemplate
433
- @notion_type = 'header'
434
- @type = 'header'
435
-
436
- def type
437
- NotionAPI::HeaderBlock.notion_type
438
- end
439
-
440
- class << self
441
- attr_reader :notion_type, :type
442
- end
443
- end
444
-
445
- # SubHeader Block: H2
446
- class SubHeaderBlock < BlockTemplate
447
- @notion_type = 'sub_header'
448
- @type = 'sub_header'
449
-
450
- def type
451
- NotionAPI::SubHeaderBlock.notion_type
452
- end
453
-
454
- class << self
455
- attr_reader :notion_type, :type
456
- end
457
- end
458
-
459
- # Sub-Sub Header Block: H3
460
- class SubSubHeaderBlock < BlockTemplate
461
- @notion_type = 'sub_sub_header'
462
- @type = 'sub_sub_header'
463
-
464
- def type
465
- NotionAPI::SubSubHeaderBlock.notion_type
466
- end
467
-
468
- class << self
469
- attr_reader :notion_type, :type
470
- end
471
- end
472
-
473
- # Page Block, entrypoint for the application
474
- class PageBlock < BlockTemplate
475
- @notion_type = 'page'
476
- @type = 'page'
477
-
478
- def type
479
- NotionAPI::PageBlock.notion_type
480
- end
481
-
482
- class << self
483
- attr_reader :notion_type, :type
484
- end
485
-
486
- def get_block(url_or_id)
487
- # ! retrieve a Notion Block and return its instantiated class object.
488
- # ! url_or_id -> the block ID or URL : ``str``
489
- get(url_or_id)
490
- end
491
-
492
- def get_collection(url_or_id)
493
- # ! retrieve a Notion Collection and return its instantiated class object.
494
- # ! url_or_id -> the block ID or URL : ``str``
495
- clean_id = extract_id(url_or_id)
496
-
497
- request_body = {
498
- pageId: clean_id,
499
- chunkNumber: 0,
500
- limit: 100,
501
- verticalColumns: false
502
- }
503
- jsonified_record_response = get_all_block_info(clean_id, request_body)
504
- i = 0
505
- while jsonified_record_response.empty? || jsonified_record_response['block'].empty?
506
- return {} if i >= 10
507
-
508
- jsonified_record_response = get_all_block_info(clean_id, request_body)
509
- i += 1
510
- end
511
- block_parent_id = extract_parent_id(clean_id, jsonified_record_response)
512
- block_collection_id = extract_collection_id(clean_id, jsonified_record_response)
513
- block_view_id = extract_view_ids(clean_id, jsonified_record_response).join
514
- block_title = extract_collection_title(clean_id, block_collection_id, jsonified_record_response)
515
-
516
- CollectionView.new(clean_id, block_title, block_parent_id, block_collection_id, block_view_id)
517
- end
518
-
519
- def create_collection(_collection_type, collection_title, data)
520
- # ! create a Notion Collection View and return its instantiated class object.
521
- # ! _collection_type -> the type of collection to create : ``str``
522
- # ! collection_title -> the title of the collection view : ``str``
523
- # ! data -> JSON data to add to the table : ``str``
524
-
525
- unless %w[table].include?(_collection_type) ; raise ArgumentError, "That collection type is not yet supported. Try: \"table\"."; end
526
- cookies = Core.options['cookies']
527
- headers = Core.options['headers']
528
-
529
- new_block_id = extract_id(SecureRandom.hex(16))
530
- parent_id = extract_id(SecureRandom.hex(16))
531
- collection_id = extract_id(SecureRandom.hex(16))
532
- view_id = extract_id(SecureRandom.hex(16))
533
-
534
- children = []
535
- alive_blocks = []
536
- data.each do |_row|
537
- child = extract_id(SecureRandom.hex(16))
538
- children.push(child)
539
- alive_blocks.push(Utils::CollectionViewComponents.set_collection_blocks_alive(child, collection_id))
540
- end
541
-
542
- request_id = extract_id(SecureRandom.hex(16))
543
- transaction_id = extract_id(SecureRandom.hex(16))
544
- space_id = extract_id(SecureRandom.hex(16))
545
-
546
- request_ids = {
547
- request_id: request_id,
548
- transaction_id: transaction_id,
549
- space_id: space_id
550
- }
551
-
552
- create_collection_view = Utils::CollectionViewComponents.create_collection_view(new_block_id, collection_id, view_id)
553
- configure_view = Utils::CollectionViewComponents.set_view_config(new_block_id, view_id, children)
554
- configure_columns = Utils::CollectionViewComponents.set_collection_columns(collection_id, new_block_id, data)
555
- set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(@id, new_block_id)
556
- add_block_hash = Utils::BlockComponents.block_location_add(@id, @id, new_block_id, nil, 'listAfter')
557
- new_block_edited_time = Utils::BlockComponents.last_edited_time(new_block_id)
558
- collection_title_hash = Utils::CollectionViewComponents.set_collection_title(collection_title, collection_id)
559
-
560
- operations = [
561
- create_collection_view,
562
- configure_view,
563
- configure_columns,
564
- set_parent_alive_hash,
565
- add_block_hash,
566
- new_block_edited_time,
567
- collection_title_hash
568
- ]
569
- operations << alive_blocks
570
- all_ops = operations.flatten
571
- data.each_with_index do |row, i|
572
- child = children[i]
573
- row.keys.each_with_index do |col_name, j|
574
- child_component = Utils::CollectionViewComponents.insert_data(child, j.zero? ? 'title' : col_name, row[col_name])
575
- all_ops.push(child_component)
576
- end
577
- end
578
-
579
- request_url = URLS[:UPDATE_BLOCK]
580
- request_body = build_payload(all_ops, request_ids)
581
- response = HTTParty.post(
582
- request_url,
583
- body: request_body.to_json,
584
- cookies: cookies,
585
- headers: headers
586
- )
587
-
588
- unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
589
- Please try again, and if issues persist open an issue in GitHub."; end
590
-
591
- CollectionView.new(new_block_id, collection_title, parent_id, collection_id, view_id)
592
- end
593
- end
594
-
595
- # Toggle block: best for storing children blocks
596
- class ToggleBlock < BlockTemplate
597
- @notion_type = 'toggle'
598
- @type = 'toggle'
599
-
600
- def type
601
- NotionAPI::ToggleBlock.notion_type
602
- end
603
-
604
- class << self
605
- attr_reader :notion_type, :type
606
- end
607
- end
608
-
609
- # Bullet list block: best for an unordered list
610
- class BulletedBlock < BlockTemplate
611
- @notion_type = 'bulleted_list'
612
- @type = 'bulleted_list'
613
-
614
- def type
615
- NotionAPI::BulletedBlock.notion_type
616
- end
617
-
618
- class << self
619
- attr_reader :notion_type, :type
620
- end
621
- end
622
-
623
- # Numbered list Block: best for an ordered list
624
- class NumberedBlock < BlockTemplate
625
- @notion_type = 'numbered_list'
626
- @type = 'numbered_list'
627
-
628
- def type
629
- NotionAPI::NumberedBlock.notion_type
630
- end
631
-
632
- class << self
633
- attr_reader :notion_type, :type
634
- end
635
- end
636
-
637
- # best for memorable information
638
- class QuoteBlock < BlockTemplate
639
- @notion_type = 'quote'
640
- @type = 'quote'
641
-
642
- def type
643
- NotionAPI::QuoteBlock.notion_type
644
- end
645
-
646
- class << self
647
- attr_reader :notion_type, :type
648
- end
649
- end
650
-
651
- # same as quote... works similarly to page block
652
- class CalloutBlock < BlockTemplate
653
- @notion_type = 'callout'
654
- @type = 'callout'
655
-
656
- def type
657
- NotionAPI::CalloutBlock.notion_type
658
- end
659
-
660
- class << self
661
- attr_reader :notion_type, :type
662
- end
663
- end
664
-
665
- # simiilar to code block but for mathematical functions.
666
- class LatexBlock < BlockTemplate
667
- @notion_type = 'equation'
668
- @type = 'equation'
669
-
670
- def type
671
- NotionAPI::LatexBlock.notion_type
672
- end
673
-
674
- class << self
675
- attr_reader :notion_type, :type
676
- end
677
- end
678
-
679
- # good for just about anything (-:
680
- class TextBlock < BlockTemplate
681
- @notion_type = 'text'
682
- @type = 'text'
683
-
684
- def type
685
- NotionAPI::TextBlock.notion_type
686
- end
687
-
688
- class << self
689
- attr_reader :notion_type, :type
690
- end
691
- end
692
-
693
- # good for visual information
694
- class ImageBlock < BlockTemplate
695
- @notion_type = 'image'
696
- @type = 'image'
697
-
698
- def type
699
- NotionAPI::ImageBlock.notion_type
700
- end
701
-
702
- class << self
703
- attr_reader :notion_type, :type
704
- end
705
- end
706
-
707
- # maps out the headers - sub-headers - sub-sub-headers on the page
708
- class TableOfContentsBlock < BlockTemplate
709
- @notion_type = 'table_of_contents'
710
- @type = 'table_of_contents'
711
-
712
- def type
713
- NotionAPI::TableOfContentsBlock.notion_type
714
- end
715
-
716
- class << self
717
- attr_reader :notion_type, :type
718
- end
719
- end
720
-
721
- # no use case for this yet.
722
- class ColumnListBlock < BlockTemplate
723
- @notion_type = 'column_list'
724
- @type = 'column_list'
725
-
726
- def type
727
- NotionAPI::ColumnListBlock.notion_type
728
- end
729
-
730
- class << self
731
- attr_reader :notion_type, :type
732
- end
733
- end
734
-
735
- # no use case for this yet.
736
- class ColumnBlock < BlockTemplate
737
- @notion_type = 'column'
738
- @type = 'column'
739
-
740
- def type
741
- NotionAPI::ColumnBlock.notion_type
742
- end
743
-
744
- class << self
745
- attr_reader :notion_type, :type
746
- end
747
- end
748
- end
749
-
750
- module NotionAPI
751
- # collection views such as tables and timelines.
752
- class CollectionView < Core
753
- attr_reader :id, :title, :parent_id, :collection_id, :view_id
754
-
755
- @notion_type = 'collection_view'
756
- @type = 'collection_view'
757
-
758
- def type
759
- NotionAPI::CollectionView.notion_type
760
- end
761
-
762
- class << self
763
- attr_reader :notion_type, :type
764
- end
765
-
766
- def initialize(id, title, parent_id, collection_id, view_id)
767
- @id = id
768
- @title = title
769
- @parent_id = parent_id
770
- @collection_id = collection_id
771
- @view_id = view_id
772
- end
773
-
774
- def add_row(data)
775
- # ! add new row to Collection View table.
776
- # ! data -> data to add to table : ``hash``
777
-
778
- cookies = Core.options['cookies']
779
- headers = Core.options['headers']
780
-
781
- request_id = extract_id(SecureRandom.hex(16))
782
- transaction_id = extract_id(SecureRandom.hex(16))
783
- space_id = extract_id(SecureRandom.hex(16))
784
- new_block_id = extract_id(SecureRandom.hex(16))
785
- schema = extract_collection_schema(@collection_id, @view_id)
786
- keys = schema.keys
787
- col_map = {}
788
- keys.map { |key| col_map[schema[key]['name']] = key }
789
-
790
- request_ids = {
791
- request_id: request_id,
792
- transaction_id: transaction_id,
793
- space_id: space_id
794
- }
795
-
796
- instantiate_row = Utils::CollectionViewComponents.add_new_row(new_block_id)
797
- set_block_alive = Utils::CollectionViewComponents.set_collection_blocks_alive(new_block_id, @collection_id)
798
- new_block_edited_time = Utils::BlockComponents.last_edited_time(new_block_id)
799
- parent_edited_time = Utils::BlockComponents.last_edited_time(@parent_id)
800
-
801
- operations = [
802
- instantiate_row,
803
- set_block_alive,
804
- new_block_edited_time,
805
- parent_edited_time
806
- ]
807
-
808
- data.keys.each_with_index do |col_name, j|
809
- child_component = Utils::CollectionViewComponents.insert_data(new_block_id, j.zero? ? 'title' : col_map[col_name], data[col_name])
810
- operations.push(child_component)
811
- end
812
-
813
- request_url = URLS[:UPDATE_BLOCK]
814
- request_body = build_payload(operations, request_ids)
815
- response = HTTParty.post(
816
- request_url,
817
- body: request_body.to_json,
818
- cookies: cookies,
819
- headers: headers
820
- )
821
-
822
- unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
823
- Please try again, and if issues persist open an issue in GitHub."; end
824
-
825
- NotionAPI::CollectionViewRow.new(new_block_id, @parent_id, @collection_id, @view_id)
826
- end
827
-
828
- def add_property(name, type)
829
- # ! add a property (column) to the table.
830
- # ! name -> name of the property : ``str``
831
- # ! type -> type of the property : ``str``
832
- cookies = Core.options['cookies']
833
- headers = Core.options['headers']
834
-
835
- request_id = extract_id(SecureRandom.hex(16))
836
- transaction_id = extract_id(SecureRandom.hex(16))
837
- space_id = extract_id(SecureRandom.hex(16))
838
-
839
- request_ids = {
840
- request_id: request_id,
841
- transaction_id: transaction_id,
842
- space_id: space_id
843
- }
844
-
845
- # create updated schema
846
- schema = extract_collection_schema(@collection_id, @view_id)
847
- schema[name] = {
848
- name: name,
849
- type: type
850
- }
851
- new_schema = {
852
- schema: schema
853
- }
854
-
855
- add_collection_property = Utils::CollectionViewComponents.add_collection_property(@collection_id, new_schema)
856
-
857
- operations = [
858
- add_collection_property
859
- ]
860
-
861
- request_url = URLS[:UPDATE_BLOCK]
862
- request_body = build_payload(operations, request_ids)
863
- response = HTTParty.post(
864
- request_url,
865
- body: request_body.to_json,
866
- cookies: cookies,
867
- headers: headers
868
- )
869
- unless response.code == 200; raise "There was an issue completing your request. Here is the response from Notion: #{response.body}, and here is the payload that was sent: #{operations}.
870
- Please try again, and if issues persist open an issue in GitHub."; end
871
-
872
- true
873
- end
874
-
875
- def row(row_id)
876
- # ! retrieve a row from a CollectionView Table.
877
- # ! row_id -> the ID for the row to retrieve: ``str``
878
- clean_id = extract_id(row_id)
879
-
880
- request_body = {
881
- pageId: clean_id,
882
- chunkNumber: 0,
883
- limit: 100,
884
- verticalColumns: false
885
- }
886
- jsonified_record_response = get_all_block_info(clean_id, request_body)
887
- schema = extract_collection_schema(@collection_id, @view_id)
888
- keys = schema.keys
889
- column_names = keys.map { |key| schema[key]['name'] }
890
- i = 0
891
- while jsonified_record_response.empty? || jsonified_record_response['block'].empty?
892
- return {} if i >= 10
893
-
894
- jsonified_record_response = get_all_block_info(clean_id, request_body)
895
- i += 1
896
- end
897
- row_jsonified_response = jsonified_record_response['block'][clean_id]['value']['properties']
898
- row_data = {}
899
- keys.each_with_index { |key, idx| row_data[column_names[idx]] = row_jsonified_response[key] ? row_jsonified_response[key].flatten : [] }
900
- row_data
901
- end
902
-
903
- def row_ids
904
- # ! retrieve all Collection View table rows.
905
- clean_id = extract_id(@id)
906
-
907
- request_body = {
908
- pageId: clean_id,
909
- chunkNumber: 0,
910
- limit: 100,
911
- verticalColumns: false
912
- }
913
-
914
- jsonified_record_response = get_all_block_info(clean_id, request_body)
915
- i = 0
916
- while jsonified_record_response.empty? || jsonified_record_response['block'].empty?
917
- return {} if i >= 10
918
-
919
- jsonified_record_response = get_all_block_info(clean_id, request_body)
920
- i += 1
921
- end
922
-
923
- jsonified_record_response['collection_view'][@view_id]['value']['page_sort']
924
- end
925
-
926
- def rows
927
- # ! returns all rows as instantiated class instances.
928
- row_id_array = row_ids
929
- parent_id = @parent_id
930
- collection_id = @collection_id
931
- view_id = @view_id
932
-
933
- row_id_array.map { |row_id| NotionAPI::CollectionViewRow.new(row_id, parent_id, collection_id, view_id) }
934
- end
935
-
936
- private
937
-
938
- def extract_collection_schema(collection_id, view_id)
939
- # ! retrieve the collection scehma. Useful for 'building' the backbone for a table.
940
- # ! collection_id -> the collection ID : ``str``
941
- # ! view_id -> the view ID : ``str``
942
- cookies = Core.options['cookies']
943
- headers = Core.options['headers']
944
-
945
- query_collection_hash = Utils::CollectionViewComponents.query_collection(collection_id, view_id, '')
946
-
947
- request_url = URLS[:GET_COLLECTION]
948
- response = HTTParty.post(
949
- request_url,
950
- body: query_collection_hash.to_json,
951
- cookies: cookies,
952
- headers: headers
953
- )
954
- response['recordMap']['collection'][collection_id]['value']['schema']
955
- end
956
- end
957
- # Class for each row in a Collection View Table.
958
- class CollectionViewRow < Core
959
- @notion_type = 'table_row'
960
- @type = 'table_row'
961
-
962
- def type
963
- NotionAPI::CollectionViewRow.notion_type
964
- end
965
-
966
- class << self
967
- attr_reader :notion_type, :type, :parent_id
968
- end
969
-
970
- attr_reader :parent_id, :id
971
- def initialize(id, parent_id, collection_id, view_id)
972
- @id = id
973
- @parent_id = parent_id
974
- @collection_id = collection_id
975
- @view_id = view_id
976
- end
977
- end
978
- end
979
-
980
- # gather a list of all the classes defined here...
981
21
  Classes = NotionAPI.constants.select { |c| NotionAPI.const_get(c).is_a? Class and c.to_s != 'BlockTemplate' and c.to_s != 'Core' and c.to_s !='Client' }
982
22
  notion_types = []
983
23
  Classes.each { |cls| notion_types.push(NotionAPI.const_get(cls).notion_type) }