Class: Utils::BlockComponents
- Inherits:
-
Object
- Object
- Utils::BlockComponents
- Defined in:
- lib/notion_api/utils.rb
Class Method Summary collapse
- .add_emoji_icon(block_id, icon) ⇒ Object
- .block_location_add(block_parent_id, block_id, new_block_id = nil, target, command) ⇒ Object
- .block_location_remove(block_parent_id, block_id) ⇒ Object
-
.build_payload(operations, request_ids) ⇒ Object
! Each function defined here builds one component that is included in each request sent to Notions backend.
- .checked_todo(block_id, standardized_check_val) ⇒ Object
- .convert_type(id, block_class_to_convert_to) ⇒ Object
- .create(block_id, block_type) ⇒ Object
- .display_source(new_block_id, block_url) ⇒ Object
- .duplicate(block_type, block_title, block_id, new_block_id, user_notion_id, contents, properties, formatting) ⇒ Object
- .last_edited_time(id) ⇒ Object
- .parent_location_add(block_parent_id, block_id) ⇒ Object
- .row_location_add(last_row_id, block_id, view_id) ⇒ Object
- .set_block_to_dead(block_id) ⇒ Object
- .set_parent_to_alive(block_parent_id, new_block_id) ⇒ Object
- .source(new_block_id, url) ⇒ Object
- .title(id, title) ⇒ Object
- .update_codeblock_language(block_id, coding_language) ⇒ Object
Class Method Details
.add_emoji_icon(block_id, icon) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/notion_api/utils.rb', line 316 def self.add_emoji_icon(block_id, icon) # ! add an emoji icon for either a page or callout block # ! block_id -> the ID of the block : ``str`` # ! icon -> the icon for the block. This is currently randomly chosen. : ``str`` { id: block_id, table: "block", path: ["format", "page_icon"], command: "set", "args": icon, } end |
.block_location_add(block_parent_id, block_id, new_block_id = nil, target, command) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/notion_api/utils.rb', line 211 def self.block_location_add(block_parent_id, block_id, new_block_id = nil, target, command) # ! payload for duplicating a block. Most properties should be # ! inherited from the block class the method is invoked on. # ! block_parent_id -> id of parent block : ``str`` # ! block_id -> id of block: ``str`` # ! new_block_id -> id of the new block: ``str`` # ! target -> the ID of the target block : ``str`` # ! command -> the position of the block, before or after, in relation to the target : ``str`` table = "block" path = ["content"] args = if command == "listAfter" { after: target || block_id, id: new_block_id || block_id, } else { before: target || block_id, id: new_block_id || block_id, } end { table: table, id: block_parent_id, # ID of the parent for the new block. It should be the block that the method is invoked on. path: path, command: command, args: args, } end |
.block_location_remove(block_parent_id, block_id) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/notion_api/utils.rb', line 262 def self.block_location_remove(block_parent_id, block_id) # ! removes a notion block # ! block_parent_id -> the parent ID of the block to remove : ``str`` # ! block_id -> the ID of the block to remove : ``str`` table = "block" path = ["content"] command = "listRemove" { table: table, id: block_parent_id, # ID of the parent for the new block. It should be the block that the method is invoked on. path: path, command: command, args: { id: block_id, }, } end |
.build_payload(operations, request_ids) ⇒ Object
! Each function defined here builds one component that is included in each request sent to Notions backend. ! Each request sent will contain multiple components.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/notion_api/utils.rb', line 15 def self.build_payload(operations, request_ids) # ! properly formats the payload for Notions backend. # ! operations -> an array of hashes that define the operations to perform : ``Array[Hash]`` # ! request_ids -> the unique IDs for the request : ``str`` request_id = request_ids[:request_id] transaction_id = request_ids[:transaction_id] space_id = request_ids[:space_id] payload = { requestId: request_id, transactions: [ { id: transaction_id, shardId: 955_090, spaceId: space_id, operations: operations, }, ], } payload end |
.checked_todo(block_id, standardized_check_val) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/notion_api/utils.rb', line 280 def self.checked_todo(block_id, standardized_check_val) # ! payload for setting a "checked" value for TodoBlock. # ! block_id -> the ID of the block to remove : ``str`` # ! standardized_check_val -> tyes/no value, determines the checked property of the block : ``str`` table = "block" path = ["properties"] command = "update" { id: block_id, table: table, path: path, command: command, args: { checked: [[standardized_check_val]], }, } end |
.convert_type(id, block_class_to_convert_to) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/notion_api/utils.rb', line 91 def self.convert_type(id, block_class_to_convert_to) # ! payload for converting a block to a different type. # ! id -> id of the block to convert : ``str`` # ! block_class_to_convert_to -> type to convert to block to: ``NotionAPI::<Block_Type>`` table = "block" path = [] command = "update" { id: id, table: table, path: path, command: command, args: { type: block_class_to_convert_to.notion_type, }, } end |
.create(block_id, block_type) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/notion_api/utils.rb', line 35 def self.create(block_id, block_type) # ! payload for creating a block. # ! block_id -> id of the new block : ``str`` # ! block_type -> type of block to create : ``cls`` table = "block" path = [] command = "update" = DateTime.now.strftime("%Q") { id: block_id, table: table, path: path, command: command, args: { id: block_id, type: block_type, properties: {}, created_time: , last_edited_time: , }, } end |
.display_source(new_block_id, block_url) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/notion_api/utils.rb', line 349 def self.display_source(new_block_id, block_url) # ! set the display source for the ImageBlock # ! new_block_id -> the ID of the new ImageBlock: ``str`` # ! block_url -> the URL of the ImageBlock: ``str`` { id: new_block_id, table: "block", path: [ "format", ], command: "update", args: { display_source: block_url, }, } end |
.duplicate(block_type, block_title, block_id, new_block_id, user_notion_id, contents, properties, formatting) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/notion_api/utils.rb', line 151 def self.duplicate(block_type, block_title, block_id, new_block_id, user_notion_id, contents, properties, formatting) # ! payload for duplicating a block. Most properties should be # ! inherited from the block class the method is invoked on. # ! block_type -> type of block that is being duplicated : ``cls`` # ! block_title -> title of block : ``str`` # ! block_id -> id of block: ``str`` # ! new_block_id -> id of new block : ``str`` # ! user_notion_id -> ID of notion user : ``str`` # ! contents -> The children of the block = DateTime.now.strftime("%Q") table = "block" path = [] command = "update" { id: new_block_id, table: table, path: path, command: command, args: { id: new_block_id, version: 10, type: block_type, properties: properties, format: formatting, content: contents, # root-level blocks created_time: , last_edited_time: , created_by_table: "notion_user", created_by_id: user_notion_id, last_edited_by_table: "notion_user", last_edited_by_id: user_notion_id, copied_from: block_id, }, } end |
.last_edited_time(id) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/notion_api/utils.rb', line 74 def self.last_edited_time(id) # ! payload for updating the last edited time # ! id -> either the block ID or parent ID : ``str`` = DateTime.now.strftime("%Q") table = "block" path = ["last_edited_time"] command = "set" { table: table, id: id, path: path, command: command, args: , } end |
.parent_location_add(block_parent_id, block_id) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/notion_api/utils.rb', line 188 def self.parent_location_add(block_parent_id, block_id) # ! payload for adding a parent # ! block_parent_id -> the parent id of the block : ``str`` # ! block_id -> the id of the block : ``str`` table = "block" path = [] command = "update" parent_table = "block" alive = true { id: block_id, table: table, path: path, command: command, args: { parent_id: block_parent_id, parent_table: parent_table, alive: alive, }, } end |
.row_location_add(last_row_id, block_id, view_id) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/notion_api/utils.rb', line 243 def self.row_location_add(last_row_id, block_id, view_id) # ! add a new row to a Collection View table # ! last_row_id -> ID of the last row in the CV : ``str`` # ! block_id -> ID of the blow : ``str`` # ! view_id -> ID of the View : ``str`` { "table": "collection_view", "id": view_id, "path": [ "page_sort", ], "command": "listAfter", "args": { "after": last_row_id, "id": block_id, }, } end |
.set_block_to_dead(block_id) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/notion_api/utils.rb', line 132 def self.set_block_to_dead(block_id) # ! payload for setting a block to dead (alive == true) # ! block_id -> the block ID to 'kill' : ``str`` table = "block" path = [] command = "update" alive = false { id: block_id, table: table, path: path, command: command, args: { alive: alive, }, } end |
.set_parent_to_alive(block_parent_id, new_block_id) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/notion_api/utils.rb', line 110 def self.set_parent_to_alive(block_parent_id, new_block_id) # ! payload for setting a blocks parent ID to 'alive' # ! block_parent_id -> the blocks parent ID : ``str`` # ! new_block_id -> the new block ID, who is a child of the parent : ``str`` table = "block" path = [] command = "update" parent_table = "block" alive = true { id: new_block_id, table: table, path: path, command: command, args: { parent_id: block_parent_id, parent_table: parent_table, alive: alive, }, } end |
.source(new_block_id, url) ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/notion_api/utils.rb', line 328 def self.source(new_block_id, url) # ! set the source for the ImageBlock # ! new_block_id -> the ID of the new ImageBlock: ``str`` # ! url -> the URL for the image table = "block" path = ["properties"] command = "update" { id: new_block_id, table: table, path: path, command: command, args: { source: [[ url, ]], }, } end |
.title(id, title) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/notion_api/utils.rb', line 58 def self.title(id, title) # ! payload for updating the title of a block # ! id -> the ID to update the title of : ``str`` table = "block" path = %w[properties title] command = "set" { id: id, table: table, path: path, command: command, args: [[title]], } end |
.update_codeblock_language(block_id, coding_language) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/notion_api/utils.rb', line 298 def self.update_codeblock_language(block_id, coding_language) # ! update the language for a codeblock # ! block_id -> id of the code block # ! coding_language -> language to change the block to. table = "block" path = ["properties"] command = "update" { id: block_id, table: table, path: path, command: command, args: { language: [[coding_language]], }, } end |