Method: Utils::CollectionViewComponents.insert_data

Defined in:
lib/notion_api/utils.rb

.insert_data(block_id, column, value, mapping) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# 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