Class: NotionAPI::PageBlock

Inherits:
BlockTemplate show all
Defined in:
lib/notion_api/blocks.rb

Overview

Page Block, entrypoint for the application

Constant Summary

Constants included from Utils

Utils::URLS

Class Attribute Summary collapse

Attributes inherited from BlockTemplate

#id, #parent_id, #title

Attributes inherited from Core

#clean_id, #cookies, #headers

Instance Method Summary collapse

Methods inherited from BlockTemplate

#convert, #create, #duplicate, #initialize, #move

Methods included from Utils

#build_payload

Methods inherited from Core

#children, #children_ids, #get_page, #initialize

Constructor Details

This class inherits a constructor from NotionAPI::BlockTemplate

Class Attribute Details

.notion_typeObject (readonly)

Returns the value of attribute notion_type.



483
484
485
# File 'lib/notion_api/blocks.rb', line 483

def notion_type
  @notion_type
end

.typeObject (readonly)

Returns the value of attribute type.



483
484
485
# File 'lib/notion_api/blocks.rb', line 483

def type
  @type
end

Instance Method Details

#create_collection(_collection_type, collection_title, data) ⇒ Object



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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/notion_api/blocks.rb', line 519

def create_collection(_collection_type, collection_title, data)
  # ! create a Notion Collection View and return its instantiated class object.
  # ! _collection_type -> the type of collection to create : ``str``
  # ! collection_title -> the title of the collection view : ``str``
  # ! data -> JSON data to add to the table : ``str``

  unless %w[table].include?(_collection_type) ; raise ArgumentError, "That collection type is not yet supported. Try: \"table\"."; end
  cookies = Core.options['cookies']
  headers = Core.options['headers']

  new_block_id = extract_id(SecureRandom.hex(16))
  parent_id = extract_id(SecureRandom.hex(16))
  collection_id = extract_id(SecureRandom.hex(16))
  view_id = extract_id(SecureRandom.hex(16))

  children = []
  alive_blocks = []
  data.each do |_row|
    child = extract_id(SecureRandom.hex(16))
    children.push(child)
    alive_blocks.push(Utils::CollectionViewComponents.set_collection_blocks_alive(child, collection_id))
  end

  request_id = extract_id(SecureRandom.hex(16))
  transaction_id = extract_id(SecureRandom.hex(16))
  space_id = extract_id(SecureRandom.hex(16))

  request_ids = {
    request_id: request_id,
    transaction_id: transaction_id,
    space_id: space_id
  }

  create_collection_view = Utils::CollectionViewComponents.create_collection_view(new_block_id, collection_id, view_id)
  configure_view = Utils::CollectionViewComponents.set_view_config(new_block_id, view_id, children)
  configure_columns = Utils::CollectionViewComponents.set_collection_columns(collection_id, new_block_id, data)
  set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(@id, new_block_id)
  add_block_hash = Utils::BlockComponents.block_location_add(@id, @id, new_block_id, nil, 'listAfter')
  new_block_edited_time = Utils::BlockComponents.last_edited_time(new_block_id)
  collection_title_hash = Utils::CollectionViewComponents.set_collection_title(collection_title, collection_id)

  operations = [
    create_collection_view,
    configure_view,
    configure_columns,
    set_parent_alive_hash,
    add_block_hash,
    new_block_edited_time,
    collection_title_hash
  ]
  operations << alive_blocks
  all_ops = operations.flatten
  data.each_with_index do |row, i|
    child = children[i]
    row.keys.each_with_index do |col_name, j|
      child_component = Utils::CollectionViewComponents.insert_data(child, j.zero? ? 'title' : col_name, row[col_name])
      all_ops.push(child_component)
    end
  end

  request_url = URLS[:UPDATE_BLOCK]
  request_body = build_payload(all_ops, request_ids)
  response = HTTParty.post(
    request_url,
    body: request_body.to_json,
    cookies: cookies,
    headers: headers
  )

  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}.
     Please try again, and if issues persist open an issue in GitHub."; end

  CollectionView.new(new_block_id, collection_title, parent_id, collection_id, view_id)
end

#get_block(url_or_id) ⇒ Object



486
487
488
489
490
# File 'lib/notion_api/blocks.rb', line 486

def get_block(url_or_id)
  # ! retrieve a Notion Block and return its instantiated class object.
  # ! url_or_id -> the block ID or URL : ``str``
  get(url_or_id)
end

#get_collection(url_or_id) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/notion_api/blocks.rb', line 492

def get_collection(url_or_id)
  # ! retrieve a Notion Collection and return its instantiated class object.
  # ! url_or_id -> the block ID or URL : ``str``
  clean_id = extract_id(url_or_id)

  request_body = {
    pageId: clean_id,
    chunkNumber: 0,
    limit: 100,
    verticalColumns: false
  }
  jsonified_record_response = get_all_block_info(clean_id, request_body)
  i = 0
  while jsonified_record_response.empty? || jsonified_record_response['block'].empty?
    return {} if i >= 10

    jsonified_record_response = get_all_block_info(clean_id, request_body)
    i += 1
  end
  block_parent_id = extract_parent_id(clean_id, jsonified_record_response)
  block_collection_id = extract_collection_id(clean_id, jsonified_record_response)
  block_view_id = extract_view_ids(clean_id, jsonified_record_response).join
  block_title = extract_collection_title(clean_id, block_collection_id, jsonified_record_response)

  CollectionView.new(clean_id, block_title, block_parent_id, block_collection_id, block_view_id)
end

#typeObject



478
479
480
# File 'lib/notion_api/blocks.rb', line 478

def type
  NotionAPI::PageBlock.notion_type
end