class Utils::BlockComponents

Public Class Methods

add_emoji_icon(block_id, icon) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

! Each function defined here builds one component that is included in each request sent to Notions backend. ! Each request sent will contain multiple components.

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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"
  timestamp = DateTime.now.strftime("%Q")
  {
    id: block_id,
    table: table,
    path: path,
    command: command,
    args: {
      id: block_id,
      type: block_type,
      properties: {},
      created_time: timestamp,
      last_edited_time: timestamp,
    },
  }
end
display_source(new_block_id, block_url) click to toggle source
# 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) click to toggle source
# 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
  timestamp = 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: timestamp,
      last_edited_time: timestamp,
      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) click to toggle source
# 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``
  timestamp = DateTime.now.strftime("%Q")
  table = "block"
  path = ["last_edited_time"]
  command = "set"

  {
    table: table,
    id: id,
    path: path,
    command: command,
    args: timestamp,
  }
end
parent_location_add(block_parent_id, block_id) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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