class Utils::CollectionViewComponents

Public Class Methods

add_collection_property(collection_id, args) click to toggle source
# File lib/notion_api/utils.rb, line 628
def self.add_collection_property(collection_id, args)
  # ! payload for adding a column to the table.
  # ! collection_id -> the collection ID : ``str``
  # ! args -> the definition of the column : ``str``

  {
    id: collection_id,
    table: "collection",
    path: [],
    command: "update",
    args: args,
  }
end
add_new_option(column, value, collection_id) click to toggle source
# File lib/notion_api/utils.rb, line 562
def self.add_new_option(column, value, collection_id)
  table = "collection"
  path = ["schema", column, "options"]
  command = "keyedObjectListAfter"
  colors = ["default", "gray", "brown", "orange", "yellow", "green", "blue", "purple", "pink", "red"]
  random_color = colors[rand(0...colors.length)]

  args = {
    "value": {
      "id": SecureRandom.hex(16),
      "value": value,
      "color": random_color,
    },
  }

  {
    table: table,
    id: collection_id,
    command: command,
    path: path,
    args: args,
  }
end
add_new_row(new_block_id) click to toggle source
# File lib/notion_api/utils.rb, line 586
def self.add_new_row(new_block_id)
  # ! payload for adding a new row to the table.
  # ! new_block_id -> the ID of the new row : ``str``
  table = "block"
  path = []
  command = "set"
  type = "page"

  {
    id: new_block_id,
    table: table,
    path: path,
    command: command,
    args: {
      type: type,
      id: new_block_id,
      version: 1,
    },
  }
end
create_collection_view(new_block_id, collection_id, view_ids) click to toggle source
# File lib/notion_api/utils.rb, line 368
def self.create_collection_view(new_block_id, collection_id, view_ids)
  # ! payload for creating a collection view
  # ! new_block_id -> id of the new block
  # ! collection_id -> ID of the collection.
  # ! view_ids -> id of the view
  table = "block"
  command = "update"
  path = []
  type = "collection_view"
  properties = {}
  timestamp = DateTime.now.strftime("%Q")

  {
    id: new_block_id,
    table: table,
    path: path,
    command: command,
    args: {
      id: new_block_id,
      type: type,
      collection_id: collection_id,
      view_ids: [
        view_ids,
      ],
      properties: properties,
      created_time: timestamp,
      last_edited_time: timestamp,
    },
  }
end
insert_data(block_id, column, value, mapping) click to toggle source
# File lib/notion_api/utils.rb, line 518
def self.insert_data(block_id, column, value, mapping)
  # ! payload for inserting data into the table.
  # ! block_id -> the ID of the block : ``str``
  # ! column -> the name of the column to insert data into.
  # ! value -> the value to insert into the column.
  # ! mapping -> the column data type.
  simple_mappings = ["title", "text", "phone_number", "email", "url", "number", "checkbox", "select", "multi_select"]
  datetime_mappings = ["date"]
  media_mappings = ["file"]
  person_mappings = ["person"]
  page_mappings = ["relation"]

  table = "block"
  path = [
    "properties",
    column,
  ]
  command = "set"

  if simple_mappings.include?(mapping)
    args = [[value]]
  elsif media_mappings.include?(mapping)
    args = [[value, [["a", value]]]]
  elsif datetime_mappings.include?(mapping)
    args = [["‣", [["d", { "type": "date", "start_date": value }]]]]
  elsif person_mappings.include?(mapping)
    args = [["‣",
             [["u", value]]]]
  elsif page_mappings.include?(mapping)
    args = [["‣",
             [["p", value]]]]
  else
    raise SchemaTypeError, "Invalid property type: #{mapping}"
  end

  {
    table: table,
    id: block_id,
    command: command,
    path: path,
    args: args,
  }
end
query_collection(collection_id, view_id, search_query = "") click to toggle source
# File lib/notion_api/utils.rb, line 607
def self.query_collection(collection_id, view_id, search_query = "")
  # ! payload for querying the table for data.
  # ! collection_id -> the collection ID : ``str``
  # ! view_id -> the view ID : ``str``
  # ! search_query -> the query for searching the table : ``str``
  query = {}
  loader = {
    type: "table",
    limit: 100,
    searchQuery: search_query,
    loadContentCover: true,
  }

  {
    collectionId: collection_id,
    collectionViewId: view_id,
    query: query,
    loader: loader,
  }
end
set_collection_blocks_alive(new_block_id, collection_id) click to toggle source
# File lib/notion_api/utils.rb, line 399
def self.set_collection_blocks_alive(new_block_id, collection_id)
  # ! payload for setting the collection blocks to alive.
  # ! new_block_id -> id of the new block
  # ! collection_id -> ID of the collection.
  table = "block"
  path = []
  command = "update"
  parent_table = "collection"
  alive = true
  type = "page"
  properties = {}
  timestamp = DateTime.now.strftime("%Q")

  {
    id: new_block_id,
    table: table,
    path: path,
    command: command,
    args: {
      id: new_block_id,
      type: type,
      parent_id: collection_id,
      parent_table: parent_table,
      alive: alive,
      properties: properties,
      created_time: timestamp,
      last_edited_time: timestamp,
    },
  }
end
set_collection_columns(collection_id, new_block_id, data) click to toggle source
# File lib/notion_api/utils.rb, line 461
def self.set_collection_columns(collection_id, new_block_id, data)
  # ! payload for setting the columns of the table.
  # ! collection_id -> ID of the collection.
  # ! new_block_id -> id of the new block
  # ! data -> json data to insert into table.
  col_names = data[0].keys
  data_mappings = { Integer => "number", String => "text", Array => "text", Float => "number", Date => "date" }
  exceptions = [ArgumentError, TypeError]
  data_types = col_names.map do |name|
    # TODO: this is a little hacky... should probably think about a better way or add a requirement for user input to match a certain criteria.
    begin
      DateTime.parse(data[0][name]) ? data_mappings[Date] : nil
    rescue *exceptions
      data_mappings[data[0][name].class]
    end
  end

  schema_conf = {}
  col_names.each_with_index do |_name, i|
    if i.zero?
      schema_conf[:title] = { name: col_names[i], type: "title" }
    else
      schema_conf[col_names[i]] = { name: col_names[i], type: data_types[i] }
    end
  end
  return {
           id: collection_id,
           table: "collection",
           path: [],
           command: "update",
           args: {
             id: collection_id,
             schema: schema_conf,
             parent_id: new_block_id,
             parent_table: "block",
             alive: true,
           },
         }, data_types
end
set_collection_title(collection_title, collection_id) click to toggle source
# File lib/notion_api/utils.rb, line 501
def self.set_collection_title(collection_title, collection_id)
  # ! payload for setting the title of the collection.
  # ! collection_title -> title of the collection.
  # ! collection_id -> ID of the collection.
  table = "collection"
  path = ["name"]
  command = "set"

  {
    id: collection_id,
    table: table,
    path: path,
    command: command,
    args: [[collection_title]],
  }
end
set_view_config(collection_type, new_block_id, view_id, children_ids) click to toggle source
# File lib/notion_api/utils.rb, line 430
def self.set_view_config(collection_type, new_block_id, view_id, children_ids)
  # ! payload for setting the configurations of the view.
  # ! new_block_id -> id of the new block
  # ! view_id -> id of the view
  # ! children_ids -> IDs for the children of the collection.
  table = "collection_view"
  path = []
  command = "update"
  version = 0
  name = "Default View"
  parent_table = "block"
  alive = true

  {
    id: view_id,
    table: table,
    path: path,
    command: command,
    args: {
      id: view_id,
      version: version,
      type: collection_type,
      name: name,
      page_sort: children_ids,
      parent_id: new_block_id,
      parent_table: parent_table,
      alive: alive,
    },
  }
end
update_property_value(page_id, column_name, new_value, column_type) click to toggle source
# File lib/notion_api/utils.rb, line 642
def self.update_property_value(page_id, column_name, new_value, column_type)
  # ! update the specified column_name to new_value
  # ! page_id -> the ID of the page: ``str``
  # ! column_name -> the name of the column ["property"] to update: ``str``
  # ! new_value -> the new value to assign to that column ["property"]: ``str``
  # ! column_type -> the type of the property: ``str``
  table = "block"
  path = [
    "properties",
    column_name,
  ]
  command = "set"

  if column_type == "relation"
    args = [["‣", [[
      "p", new_value,
    ]]]]
  else
    args = [[
      new_value,
    ]]
  end

  {
    id: page_id,
    table: table,
    path: path,
    command: command,
    args: args,
  }
end