notion 1.0.1 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +116 -4
- data/lib/notion_api/blocks.rb +19 -982
- data/lib/notion_api/core.rb +85 -6
- data/lib/notion_api/notion_types/bulleted_block.rb +17 -0
- data/lib/notion_api/notion_types/callout_block.rb +16 -0
- data/lib/notion_api/notion_types/code_block.rb +16 -0
- data/lib/notion_api/notion_types/collection_view_blocks.rb +316 -0
- data/lib/notion_api/notion_types/column_list_block.rb +16 -0
- data/lib/notion_api/notion_types/divider_block.rb +15 -0
- data/lib/notion_api/notion_types/header_block.rb +16 -0
- data/lib/notion_api/notion_types/image_block.rb +16 -0
- data/lib/notion_api/notion_types/latex_block.rb +16 -0
- data/lib/notion_api/notion_types/numbered_block.rb +15 -0
- data/lib/notion_api/notion_types/page_block.rb +130 -0
- data/lib/notion_api/notion_types/quote_block.rb +16 -0
- data/lib/notion_api/notion_types/sub_header_block.rb +16 -0
- data/lib/notion_api/notion_types/sub_sub_header.rb +16 -0
- data/lib/notion_api/notion_types/table_of_contents_block.rb +15 -0
- data/lib/notion_api/notion_types/template.rb +360 -0
- data/lib/notion_api/notion_types/text_block.rb +16 -0
- data/lib/notion_api/notion_types/todo_block.rb +60 -0
- data/lib/notion_api/notion_types/toggle_block.rb +16 -0
- data/lib/notion_api/utils.rb +35 -4
- data/lib/notion_api/version.rb +1 -1
- metadata +21 -2
@@ -0,0 +1,16 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
|
3
|
+
# no use case for this yet.
|
4
|
+
class ColumnListBlock < BlockTemplate
|
5
|
+
@notion_type = 'column_list'
|
6
|
+
@type = 'column_list'
|
7
|
+
|
8
|
+
def type
|
9
|
+
NotionAPI::ColumnListBlock.notion_type
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :notion_type, :type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
# divider block: ---------
|
3
|
+
class DividerBlock < BlockTemplate
|
4
|
+
@notion_type = 'divider'
|
5
|
+
@type = 'divider'
|
6
|
+
|
7
|
+
def type
|
8
|
+
NotionAPI::DividerBlock.notion_type
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_reader :notion_type, :type
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
|
3
|
+
# Header block: H1
|
4
|
+
class HeaderBlock < BlockTemplate
|
5
|
+
@notion_type = 'header'
|
6
|
+
@type = 'header'
|
7
|
+
|
8
|
+
def type
|
9
|
+
NotionAPI::HeaderBlock.notion_type
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :notion_type, :type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
|
3
|
+
# good for visual information
|
4
|
+
class ImageBlock < BlockTemplate
|
5
|
+
@notion_type = 'image'
|
6
|
+
@type = 'image'
|
7
|
+
|
8
|
+
def type
|
9
|
+
NotionAPI::ImageBlock.notion_type
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :notion_type, :type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
|
3
|
+
# simiilar to code block but for mathematical functions.
|
4
|
+
class LatexBlock < BlockTemplate
|
5
|
+
@notion_type = 'equation'
|
6
|
+
@type = 'equation'
|
7
|
+
|
8
|
+
def type
|
9
|
+
NotionAPI::LatexBlock.notion_type
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :notion_type, :type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
# Numbered list Block: best for an ordered list
|
3
|
+
class NumberedBlock < BlockTemplate
|
4
|
+
@notion_type = 'numbered_list'
|
5
|
+
@type = 'numbered_list'
|
6
|
+
|
7
|
+
def type
|
8
|
+
NotionAPI::NumberedBlock.notion_type
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_reader :notion_type, :type
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
|
2
|
+
module NotionAPI
|
3
|
+
|
4
|
+
# Page Block, entrypoint for the application
|
5
|
+
class PageBlock < BlockTemplate
|
6
|
+
@notion_type = 'page'
|
7
|
+
@type = 'page'
|
8
|
+
|
9
|
+
def type
|
10
|
+
NotionAPI::PageBlock.notion_type
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_reader :notion_type, :type
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_block(url_or_id)
|
18
|
+
# ! retrieve a Notion Block and return its instantiated class object.
|
19
|
+
# ! url_or_id -> the block ID or URL : ``str``
|
20
|
+
get(url_or_id)
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_collection(url_or_id)
|
24
|
+
# ! retrieve a Notion Collection and return its instantiated class object.
|
25
|
+
# ! url_or_id -> the block ID or URL : ``str``
|
26
|
+
clean_id = extract_id(url_or_id)
|
27
|
+
|
28
|
+
request_body = {
|
29
|
+
pageId: clean_id,
|
30
|
+
chunkNumber: 0,
|
31
|
+
limit: 100,
|
32
|
+
verticalColumns: false
|
33
|
+
}
|
34
|
+
jsonified_record_response = get_all_block_info(clean_id, request_body)
|
35
|
+
i = 0
|
36
|
+
while jsonified_record_response.empty? || jsonified_record_response['block'].empty?
|
37
|
+
return {} if i >= 10
|
38
|
+
|
39
|
+
jsonified_record_response = get_all_block_info(clean_id, request_body)
|
40
|
+
i += 1
|
41
|
+
end
|
42
|
+
block_parent_id = extract_parent_id(clean_id, jsonified_record_response)
|
43
|
+
block_collection_id = extract_collection_id(clean_id, jsonified_record_response)
|
44
|
+
block_view_id = extract_view_ids(clean_id, jsonified_record_response).join
|
45
|
+
block_title = extract_collection_title(clean_id, block_collection_id, jsonified_record_response)
|
46
|
+
|
47
|
+
CollectionView.new(clean_id, block_title, block_parent_id, block_collection_id, block_view_id)
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_collection(collection_type, collection_title, data)
|
51
|
+
# ! create a Notion Collection View and return its instantiated class object.
|
52
|
+
# ! _collection_type -> the type of collection to create : ``str``
|
53
|
+
# ! collection_title -> the title of the collection view : ``str``
|
54
|
+
# ! data -> JSON data to add to the table : ``str``
|
55
|
+
|
56
|
+
valid_types = %w[table board list timeline calendar gallery]
|
57
|
+
unless valid_types.include?(collection_type) ; raise ArgumentError, "That collection type is not yet supported. Try: #{valid_types.join}."; end
|
58
|
+
cookies = Core.options['cookies']
|
59
|
+
headers = Core.options['headers']
|
60
|
+
|
61
|
+
new_block_id = extract_id(SecureRandom.hex(16))
|
62
|
+
parent_id = extract_id(SecureRandom.hex(16))
|
63
|
+
collection_id = extract_id(SecureRandom.hex(16))
|
64
|
+
view_id = extract_id(SecureRandom.hex(16))
|
65
|
+
|
66
|
+
children = []
|
67
|
+
alive_blocks = []
|
68
|
+
data.each do |_row|
|
69
|
+
child = extract_id(SecureRandom.hex(16))
|
70
|
+
children.push(child)
|
71
|
+
alive_blocks.push(Utils::CollectionViewComponents.set_collection_blocks_alive(child, collection_id))
|
72
|
+
end
|
73
|
+
|
74
|
+
request_id = extract_id(SecureRandom.hex(16))
|
75
|
+
transaction_id = extract_id(SecureRandom.hex(16))
|
76
|
+
space_id = extract_id(SecureRandom.hex(16))
|
77
|
+
|
78
|
+
request_ids = {
|
79
|
+
request_id: request_id,
|
80
|
+
transaction_id: transaction_id,
|
81
|
+
space_id: space_id
|
82
|
+
}
|
83
|
+
|
84
|
+
create_collection_view = Utils::CollectionViewComponents.create_collection_view(new_block_id, collection_id, view_id)
|
85
|
+
configure_view = Utils::CollectionViewComponents.set_view_config(collection_type, new_block_id, view_id, children)
|
86
|
+
|
87
|
+
# returns the JSON and some useful column mappings...
|
88
|
+
column_data = Utils::CollectionViewComponents.set_collection_columns(collection_id, new_block_id, data)
|
89
|
+
configure_columns_hash = column_data[0]
|
90
|
+
column_mappings = column_data[1]
|
91
|
+
set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(@id, new_block_id)
|
92
|
+
add_block_hash = Utils::BlockComponents.block_location_add(@id, @id, new_block_id, nil, 'listAfter')
|
93
|
+
new_block_edited_time = Utils::BlockComponents.last_edited_time(new_block_id)
|
94
|
+
collection_title_hash = Utils::CollectionViewComponents.set_collection_title(collection_title, collection_id)
|
95
|
+
|
96
|
+
operations = [
|
97
|
+
create_collection_view,
|
98
|
+
configure_view,
|
99
|
+
configure_columns_hash,
|
100
|
+
set_parent_alive_hash,
|
101
|
+
add_block_hash,
|
102
|
+
new_block_edited_time,
|
103
|
+
collection_title_hash
|
104
|
+
]
|
105
|
+
operations << alive_blocks
|
106
|
+
all_ops = operations.flatten
|
107
|
+
data.each_with_index do |row, i|
|
108
|
+
child = children[i]
|
109
|
+
row.keys.each_with_index do |col_name, j|
|
110
|
+
child_component = Utils::CollectionViewComponents.insert_data(child, j.zero? ? 'title' : col_name, row[col_name], column_mappings[j])
|
111
|
+
all_ops.push(child_component)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
request_url = URLS[:UPDATE_BLOCK]
|
116
|
+
request_body = build_payload(all_ops, request_ids)
|
117
|
+
response = HTTParty.post(
|
118
|
+
request_url,
|
119
|
+
body: request_body.to_json,
|
120
|
+
cookies: cookies,
|
121
|
+
headers: headers
|
122
|
+
)
|
123
|
+
|
124
|
+
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}.
|
125
|
+
Please try again, and if issues persist open an issue in GitHub."; end
|
126
|
+
|
127
|
+
CollectionView.new(new_block_id, collection_title, parent_id, collection_id, view_id)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
|
3
|
+
# best for memorable information
|
4
|
+
class QuoteBlock < BlockTemplate
|
5
|
+
@notion_type = 'quote'
|
6
|
+
@type = 'quote'
|
7
|
+
|
8
|
+
def type
|
9
|
+
NotionAPI::QuoteBlock.notion_type
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :notion_type, :type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
|
3
|
+
# SubHeader Block: H2
|
4
|
+
class SubHeaderBlock < BlockTemplate
|
5
|
+
@notion_type = 'sub_header'
|
6
|
+
@type = 'sub_header'
|
7
|
+
|
8
|
+
def type
|
9
|
+
NotionAPI::SubHeaderBlock.notion_type
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :notion_type, :type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
|
3
|
+
# Sub-Sub Header Block: H3
|
4
|
+
class SubSubHeaderBlock < BlockTemplate
|
5
|
+
@notion_type = 'sub_sub_header'
|
6
|
+
@type = 'sub_sub_header'
|
7
|
+
|
8
|
+
def type
|
9
|
+
NotionAPI::SubSubHeaderBlock.notion_type
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :notion_type, :type
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module NotionAPI
|
2
|
+
# maps out the headers - sub-headers - sub-sub-headers on the page
|
3
|
+
class TableOfContentsBlock < BlockTemplate
|
4
|
+
@notion_type = 'table_of_contents'
|
5
|
+
@type = 'table_of_contents'
|
6
|
+
|
7
|
+
def type
|
8
|
+
NotionAPI::TableOfContentsBlock.notion_type
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_reader :notion_type, :type
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,360 @@
|
|
1
|
+
require_relative '../core'
|
2
|
+
require 'httparty'
|
3
|
+
|
4
|
+
module NotionAPI
|
5
|
+
# Base Template for all blocks. Inherits core methods from the Block class defined in block.rb
|
6
|
+
class BlockTemplate < Core
|
7
|
+
include Utils
|
8
|
+
|
9
|
+
attr_reader :id, :title, :parent_id
|
10
|
+
|
11
|
+
def initialize(id, title, parent_id)
|
12
|
+
@id = id
|
13
|
+
@title = title
|
14
|
+
@parent_id = parent_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def title=(new_title)
|
18
|
+
# ! Change the title of a block.
|
19
|
+
# ! new_title -> new title for the block : ``str``
|
20
|
+
request_id = extract_id(SecureRandom.hex(16))
|
21
|
+
transaction_id = extract_id(SecureRandom.hex(16))
|
22
|
+
space_id = extract_id(SecureRandom.hex(16))
|
23
|
+
update_title(new_title.to_s, request_id, transaction_id, space_id)
|
24
|
+
@title = new_title
|
25
|
+
end
|
26
|
+
|
27
|
+
def convert(block_class_to_convert_to)
|
28
|
+
# ! convert a block from its current type to another.
|
29
|
+
# ! block_class_to_convert_to -> the type of block to convert to : ``cls``
|
30
|
+
if type == block_class_to_convert_to.notion_type
|
31
|
+
# if converting to same type, skip and return self
|
32
|
+
self
|
33
|
+
else
|
34
|
+
# setup cookies, headers, and grab/create static vars for request
|
35
|
+
cookies = Core.options['cookies']
|
36
|
+
headers = Core.options['headers']
|
37
|
+
request_url = URLS[:UPDATE_BLOCK]
|
38
|
+
|
39
|
+
# set random IDs for request
|
40
|
+
request_id = extract_id(SecureRandom.hex(16))
|
41
|
+
transaction_id = extract_id(SecureRandom.hex(16))
|
42
|
+
space_id = extract_id(SecureRandom.hex(16))
|
43
|
+
request_ids = {
|
44
|
+
request_id: request_id,
|
45
|
+
transaction_id: transaction_id,
|
46
|
+
space_id: space_id
|
47
|
+
}
|
48
|
+
|
49
|
+
# build hash's that contain the operations to send to Notions backend
|
50
|
+
convert_type_hash = Utils::BlockComponents.convert_type(@id, block_class_to_convert_to)
|
51
|
+
last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
|
52
|
+
last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
|
53
|
+
|
54
|
+
operations = [
|
55
|
+
convert_type_hash,
|
56
|
+
last_edited_time_parent_hash,
|
57
|
+
last_edited_time_child_hash
|
58
|
+
]
|
59
|
+
|
60
|
+
request_body = build_payload(operations, request_ids)
|
61
|
+
response = HTTParty.post(
|
62
|
+
request_url,
|
63
|
+
body: request_body.to_json,
|
64
|
+
cookies: cookies,
|
65
|
+
headers: headers
|
66
|
+
)
|
67
|
+
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}.
|
68
|
+
Please try again, and if issues persist open an issue in GitHub."; end
|
69
|
+
|
70
|
+
block_class_to_convert_to.new(@id, @title, @parent_id)
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def duplicate(target_block = nil)
|
76
|
+
# ! duplicate the block that this method is invoked upon.
|
77
|
+
# ! target_block -> the block to place the duplicated block after. Can be any valid Block ID! : ``str``
|
78
|
+
cookies = Core.options['cookies']
|
79
|
+
headers = Core.options['headers']
|
80
|
+
request_url = URLS[:UPDATE_BLOCK]
|
81
|
+
|
82
|
+
new_block_id = extract_id(SecureRandom.hex(16))
|
83
|
+
request_id = extract_id(SecureRandom.hex(16))
|
84
|
+
transaction_id = extract_id(SecureRandom.hex(16))
|
85
|
+
space_id = extract_id(SecureRandom.hex(16))
|
86
|
+
|
87
|
+
root_children = children_ids(@id)
|
88
|
+
sub_children = []
|
89
|
+
root_children.each { |root_id| sub_children.push(children_ids(root_id)) }
|
90
|
+
|
91
|
+
request_ids = {
|
92
|
+
request_id: request_id,
|
93
|
+
transaction_id: transaction_id,
|
94
|
+
space_id: space_id
|
95
|
+
}
|
96
|
+
body = {
|
97
|
+
pageId: @id,
|
98
|
+
chunkNumber: 0,
|
99
|
+
limit: 100,
|
100
|
+
verticalColumns: false
|
101
|
+
}
|
102
|
+
|
103
|
+
user_notion_id = get_notion_id(body)
|
104
|
+
|
105
|
+
block = target_block ? get(target_block) : self # allows dev to place block anywhere!
|
106
|
+
|
107
|
+
props_and_formatting = get_block_props_and_format(@id, @title)
|
108
|
+
props = props_and_formatting[:properties]
|
109
|
+
formats = props_and_formatting[:format]
|
110
|
+
duplicate_hash = Utils::BlockComponents.duplicate(type, @title, block.id, new_block_id, user_notion_id, root_children, props, formats)
|
111
|
+
set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(block.parent_id, new_block_id)
|
112
|
+
block_location_hash = Utils::BlockComponents.block_location_add(block.parent_id, block.id, new_block_id, target_block, 'listAfter')
|
113
|
+
last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(block.parent_id)
|
114
|
+
last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(block.id)
|
115
|
+
|
116
|
+
operations = [
|
117
|
+
duplicate_hash,
|
118
|
+
set_parent_alive_hash,
|
119
|
+
block_location_hash,
|
120
|
+
last_edited_time_parent_hash,
|
121
|
+
last_edited_time_child_hash
|
122
|
+
]
|
123
|
+
|
124
|
+
request_body = build_payload(operations, request_ids)
|
125
|
+
response = HTTParty.post(
|
126
|
+
request_url,
|
127
|
+
body: request_body.to_json,
|
128
|
+
cookies: cookies,
|
129
|
+
headers: headers
|
130
|
+
)
|
131
|
+
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}.
|
132
|
+
Please try again, and if issues persist open an issue in GitHub."; end
|
133
|
+
|
134
|
+
class_to_return = NotionAPI.const_get(Classes.select { |cls| NotionAPI.const_get(cls).notion_type == type }.join.to_s)
|
135
|
+
class_to_return.new(new_block_id, @title, block.parent_id)
|
136
|
+
end
|
137
|
+
|
138
|
+
def move(target_block, position = 'after')
|
139
|
+
# ! move the block to a new location.
|
140
|
+
# ! target_block -> the targetted block to move to. : ``str``
|
141
|
+
# ! position -> where the block should be listed, in positions relative to the target_block [before, after, top-child, last-child]
|
142
|
+
positions_hash = {
|
143
|
+
'after' => 'listAfter',
|
144
|
+
'before' => 'listBefore'
|
145
|
+
}
|
146
|
+
|
147
|
+
unless positions_hash.keys.include?(position); raise ArgumentError, "Invalid position. You said: #{position}, valid options are: #{positions_hash.keys.join(', ')}"; end
|
148
|
+
|
149
|
+
position_command = positions_hash[position]
|
150
|
+
cookies = Core.options['cookies']
|
151
|
+
headers = Core.options['headers']
|
152
|
+
request_url = URLS[:UPDATE_BLOCK]
|
153
|
+
|
154
|
+
request_id = extract_id(SecureRandom.hex(16))
|
155
|
+
transaction_id = extract_id(SecureRandom.hex(16))
|
156
|
+
space_id = extract_id(SecureRandom.hex(16))
|
157
|
+
|
158
|
+
request_ids = {
|
159
|
+
request_id: request_id,
|
160
|
+
transaction_id: transaction_id,
|
161
|
+
space_id: space_id
|
162
|
+
}
|
163
|
+
|
164
|
+
check_parents = (@parent_id == target_block.parent_id)
|
165
|
+
set_block_dead_hash = Utils::BlockComponents.set_block_to_dead(@id) # kill the block this method is invoked on...
|
166
|
+
block_location_remove_hash = Utils::BlockComponents.block_location_remove(@parent_id, @id) # remove the block this method is invoked on...
|
167
|
+
parent_location_hash = Utils::BlockComponents.parent_location_add(check_parents ? @parent_id : target_block.parent_id, @id) # set parent location to alive
|
168
|
+
block_location_add_hash = Utils::BlockComponents.block_location_add(check_parents ? @parent_id : target_block.parent_id, @id, target_block.id, position_command)
|
169
|
+
last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@parent_id)
|
170
|
+
|
171
|
+
if check_parents
|
172
|
+
last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
|
173
|
+
operations = [
|
174
|
+
set_block_dead_hash,
|
175
|
+
block_location_remove_hash,
|
176
|
+
parent_location_hash,
|
177
|
+
block_location_add_hash,
|
178
|
+
last_edited_time_parent_hash,
|
179
|
+
last_edited_time_child_hash
|
180
|
+
]
|
181
|
+
else
|
182
|
+
last_edited_time_new_parent_hash = Utils::BlockComponents.last_edited_time(target_block.parent_id)
|
183
|
+
last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
|
184
|
+
@parent_id = target_block.parent_id
|
185
|
+
operations = [
|
186
|
+
set_block_dead_hash,
|
187
|
+
block_location_remove_hash,
|
188
|
+
parent_location_hash,
|
189
|
+
block_location_add_hash,
|
190
|
+
last_edited_time_parent_hash,
|
191
|
+
last_edited_time_new_parent_hash,
|
192
|
+
last_edited_time_child_hash
|
193
|
+
]
|
194
|
+
end
|
195
|
+
request_body = build_payload(operations, request_ids)
|
196
|
+
response = HTTParty.post(
|
197
|
+
request_url,
|
198
|
+
body: request_body.to_json,
|
199
|
+
cookies: cookies,
|
200
|
+
headers: headers
|
201
|
+
)
|
202
|
+
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}.
|
203
|
+
Please try again, and if issues persist open an issue in GitHub."; end
|
204
|
+
|
205
|
+
self
|
206
|
+
end
|
207
|
+
|
208
|
+
def create(block_type, block_title, target = nil, position = 'after')
|
209
|
+
# ! create a new block
|
210
|
+
# ! block_type -> the type of block to create : ``cls``
|
211
|
+
# ! block_title -> the title of the new block : ``str``
|
212
|
+
# ! target -> the block_id that the new block should be placed after. ``str``
|
213
|
+
# ! position -> 'after' or 'before'
|
214
|
+
positions_hash = {
|
215
|
+
'after' => 'listAfter',
|
216
|
+
'before' => 'listBefore'
|
217
|
+
}
|
218
|
+
unless positions_hash.keys.include?(position); raise "Invalid position. You said: #{position}, valid options are: #{positions_hash.keys.join(', ')}"; end
|
219
|
+
|
220
|
+
position_command = positions_hash[position]
|
221
|
+
blocks_with_emojis = [NotionAPI::PageBlock, NotionAPI::CalloutBlock]
|
222
|
+
|
223
|
+
cookies = Core.options['cookies']
|
224
|
+
headers = Core.options['headers']
|
225
|
+
|
226
|
+
new_block_id = extract_id(SecureRandom.hex(16))
|
227
|
+
request_id = extract_id(SecureRandom.hex(16))
|
228
|
+
transaction_id = extract_id(SecureRandom.hex(16))
|
229
|
+
space_id = extract_id(SecureRandom.hex(16))
|
230
|
+
|
231
|
+
request_ids = {
|
232
|
+
request_id: request_id,
|
233
|
+
transaction_id: transaction_id,
|
234
|
+
space_id: space_id
|
235
|
+
}
|
236
|
+
|
237
|
+
create_hash = Utils::BlockComponents.create(new_block_id, block_type.notion_type)
|
238
|
+
set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(@id, new_block_id)
|
239
|
+
block_location_hash = Utils::BlockComponents.block_location_add(@id, @id, new_block_id, target, position_command)
|
240
|
+
last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(@id)
|
241
|
+
last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
|
242
|
+
title_hash = Utils::BlockComponents.title(new_block_id, block_title)
|
243
|
+
|
244
|
+
operations = [
|
245
|
+
create_hash,
|
246
|
+
set_parent_alive_hash,
|
247
|
+
block_location_hash,
|
248
|
+
last_edited_time_parent_hash,
|
249
|
+
last_edited_time_child_hash,
|
250
|
+
title_hash
|
251
|
+
]
|
252
|
+
|
253
|
+
if blocks_with_emojis.include?(block_type)
|
254
|
+
emoji_choices = ["😀", "😃", "😄", "😁", "😆", "😅", "🤣", "😂", "🙂", "🙃", "😉", "😊", "😇", "🥰", "😍", "😀", "😃"]
|
255
|
+
emoji = emoji_choices[rand(0...emoji_choices.length)]
|
256
|
+
emoji_icon_hash = Utils::BlockComponents.add_emoji_icon(new_block_id, emoji)
|
257
|
+
operations.push(emoji_icon_hash)
|
258
|
+
end
|
259
|
+
|
260
|
+
|
261
|
+
request_url = URLS[:UPDATE_BLOCK]
|
262
|
+
request_body = build_payload(operations, request_ids)
|
263
|
+
response = HTTParty.post(
|
264
|
+
request_url,
|
265
|
+
body: request_body.to_json,
|
266
|
+
cookies: cookies,
|
267
|
+
headers: headers
|
268
|
+
)
|
269
|
+
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}.
|
270
|
+
Please try again, and if issues persist open an issue in GitHub."; end
|
271
|
+
|
272
|
+
block_type.new(new_block_id, block_title, @id)
|
273
|
+
end
|
274
|
+
|
275
|
+
private
|
276
|
+
|
277
|
+
def get(url_or_id)
|
278
|
+
# ! retrieve a Notion Block and return its instantiated class object.
|
279
|
+
# ! url_or_id -> the block ID or URL : ``str``
|
280
|
+
clean_id = extract_id(url_or_id)
|
281
|
+
|
282
|
+
request_body = {
|
283
|
+
pageId: clean_id,
|
284
|
+
chunkNumber: 0,
|
285
|
+
limit: 100,
|
286
|
+
verticalColumns: false
|
287
|
+
}
|
288
|
+
jsonified_record_response = get_all_block_info(clean_id, request_body)
|
289
|
+
i = 0
|
290
|
+
while jsonified_record_response.empty? || jsonified_record_response['block'].empty?
|
291
|
+
return {} if i >= 10
|
292
|
+
|
293
|
+
jsonified_record_response = get_all_block_info(clean_id, request_body)
|
294
|
+
i += 1
|
295
|
+
end
|
296
|
+
block_type = extract_type(clean_id, jsonified_record_response)
|
297
|
+
block_parent_id = extract_parent_id(clean_id, jsonified_record_response)
|
298
|
+
|
299
|
+
if block_type.nil?
|
300
|
+
{}
|
301
|
+
else
|
302
|
+
block_class = NotionAPI.const_get(BLOCK_TYPES[block_type].to_s)
|
303
|
+
if block_class == NotionAPI::CollectionView
|
304
|
+
block_collection_id = extract_collection_id(clean_id, jsonified_record_response)
|
305
|
+
block_view_id = extract_view_ids(clean_id, jsonified_record_response)
|
306
|
+
collection_title = extract_collection_title(clean_id, block_collection_id, jsonified_record_response)
|
307
|
+
|
308
|
+
block = block_class.new(clean_id, collection_title, block_parent_id, block_collection_id, block_view_id.join)
|
309
|
+
schema = extract_collection_schema(block_collection_id, block_view_id[0])
|
310
|
+
column_mappings = schema.keys
|
311
|
+
column_names = column_mappings.map { |mapping| schema[mapping]['name']}
|
312
|
+
block.instance_variable_set(:@column_names, column_names)
|
313
|
+
CollectionView.class_eval{attr_reader :column_names}
|
314
|
+
|
315
|
+
block
|
316
|
+
else
|
317
|
+
block_title = extract_title(clean_id, jsonified_record_response)
|
318
|
+
block_class.new(clean_id, block_title, block_parent_id)
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
def update_title(new_title, request_id, transaction_id, space_id)
|
324
|
+
# ! Helper method for sending POST request to change title of block.
|
325
|
+
# ! new_title -> new title for the block : ``str``
|
326
|
+
# ! request_id -> the unique ID for the request key. Generated using SecureRandom : ``str``
|
327
|
+
# ! transaction_id -> the unique ID for the transaction key. Generated using SecureRandom: ``str``
|
328
|
+
# ! transaction_id -> the unique ID for the space key. Generated using SecureRandom: ``str``
|
329
|
+
# setup cookies, headers, and grab/create static vars for request
|
330
|
+
cookies = Core.options['cookies']
|
331
|
+
headers = Core.options['headers']
|
332
|
+
request_url = URLS[:UPDATE_BLOCK]
|
333
|
+
|
334
|
+
# set unique IDs for request
|
335
|
+
request_ids = {
|
336
|
+
request_id: request_id,
|
337
|
+
transaction_id: transaction_id,
|
338
|
+
space_id: space_id
|
339
|
+
}
|
340
|
+
|
341
|
+
# build and set operations to send to Notion
|
342
|
+
title_hash = Utils::BlockComponents.title(@id, new_title)
|
343
|
+
last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(@id)
|
344
|
+
operations = [
|
345
|
+
title_hash,
|
346
|
+
last_edited_time_child_hash
|
347
|
+
]
|
348
|
+
|
349
|
+
request_body = build_payload(operations, request_ids) # defined in utils.rb
|
350
|
+
|
351
|
+
response = HTTParty.post(
|
352
|
+
request_url,
|
353
|
+
body: request_body.to_json,
|
354
|
+
cookies: cookies,
|
355
|
+
headers: headers
|
356
|
+
)
|
357
|
+
response.body
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end
|