Class: NotionAPI::PageBlock

Inherits:
BlockTemplate show all
Defined in:
lib/notion_api/notion_types/page_block.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.



14
15
16
# File 'lib/notion_api/notion_types/page_block.rb', line 14

def notion_type
  @notion_type
end

.typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/notion_api/notion_types/page_block.rb', line 14

def type
  @type
end

Instance Method Details

#create_collection(collection_type, collection_title, data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/notion_api/notion_types/page_block.rb', line 50

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
  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(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: 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



17
18
19
20
21
# File 'lib/notion_api/notion_types/page_block.rb', line 17

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/notion_api/notion_types/page_block.rb', line 23

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



9
10
11
# File 'lib/notion_api/notion_types/page_block.rb', line 9

def type
  NotionAPI::PageBlock.notion_type
end