Class: NotionAPI::PageBlock
- Inherits:
-
BlockTemplate
- Object
- Core
- BlockTemplate
- NotionAPI::PageBlock
- Defined in:
- lib/notion_api/blocks.rb
Overview
Page Block, entrypoint for the application
Constant Summary
Constants included from Utils
Class Attribute Summary collapse
-
.notion_type ⇒ Object
readonly
Returns the value of attribute notion_type.
-
.type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from BlockTemplate
Attributes inherited from Core
Instance Method Summary collapse
- #create_collection(collection_type, collection_title, data) ⇒ Object
- #get_block(url_or_id) ⇒ Object
- #get_collection(url_or_id) ⇒ Object
- #type ⇒ Object
Methods inherited from BlockTemplate
#convert, #create, #duplicate, #initialize, #move
Methods included from Utils
Methods inherited from Core
#children, #children_ids, #get_page, #initialize
Constructor Details
This class inherits a constructor from NotionAPI::BlockTemplate
Class Attribute Details
.notion_type ⇒ Object (readonly)
Returns the value of attribute notion_type.
481 482 483 |
# File 'lib/notion_api/blocks.rb', line 481 def notion_type @notion_type end |
.type ⇒ Object (readonly)
Returns the value of attribute type.
481 482 483 |
# File 'lib/notion_api/blocks.rb', line 481 def type @type end |
Instance Method Details
#create_collection(collection_type, collection_title, data) ⇒ Object
517 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 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 593 594 595 |
# File 'lib/notion_api/blocks.rb', line 517 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`` valid_types = %w[table board list timeline calendar gallery] unless valid_types.include?(collection_type) ; raise ArgumentError, "That collection type is not yet supported. Try: #{valid_types.join}."; end = Core.['cookies'] headers = Core.['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(collection_type, new_block_id, view_id, children) # returns the JSON and some useful column mappings... column_data = Utils::CollectionViewComponents.set_collection_columns(collection_id, new_block_id, data) configure_columns_hash = column_data[0] column_mappings = column_data[1] 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_hash, 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], column_mappings[j]) 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: , 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
484 485 486 487 488 |
# File 'lib/notion_api/blocks.rb', line 484 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
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/notion_api/blocks.rb', line 490 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 |
#type ⇒ Object
476 477 478 |
# File 'lib/notion_api/blocks.rb', line 476 def type NotionAPI::PageBlock.notion_type end |