notion 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,16 @@
1
1
  module NotionAPI
2
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
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
15
14
  end
16
- end
15
+ end
16
+ end
@@ -1,15 +1,15 @@
1
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
2
+ # divider block: ---------
3
+ class DividerBlock < BlockTemplate
4
+ @notion_type = "divider"
5
+ @type = "divider"
6
+
7
+ def type
8
+ NotionAPI::DividerBlock.notion_type
14
9
  end
15
- end
10
+
11
+ class << self
12
+ attr_reader :notion_type, :type
13
+ end
14
+ end
15
+ end
@@ -1,16 +1,16 @@
1
1
  module NotionAPI
2
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
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
15
14
  end
16
- end
15
+ end
16
+ end
@@ -1,16 +1,63 @@
1
1
  module NotionAPI
2
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
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
+
16
+ private
17
+
18
+ def self.create(block_id, new_block_id, block_title, target, position_command, request_ids, options)
19
+ if !(options[:image])
20
+ raise ArgumentError, "Must specify image as an option. For example: .create(\"block type\", \"block title\", options: {image: \"https://image-domain.com\"})"
21
+ else
22
+ cookies = Core.options["cookies"]
23
+ headers = Core.options["headers"]
24
+ if options[:image].match(/^http:\/\/|^https:\/\//)
25
+ create_hash = Utils::BlockComponents.create(new_block_id, self.notion_type)
26
+ set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(block_id, new_block_id)
27
+ block_location_hash = Utils::BlockComponents.block_location_add(block_id, block_id, new_block_id, target, position_command)
28
+ last_edited_time_parent_hash = Utils::BlockComponents.last_edited_time(block_id)
29
+ last_edited_time_child_hash = Utils::BlockComponents.last_edited_time(block_id)
30
+ title_hash = Utils::BlockComponents.title(new_block_id, block_title)
31
+ source_url_hash = Utils::BlockComponents.source(new_block_id, options[:image])
32
+ display_source_url_hash = Utils::BlockComponents.display_source(new_block_id, options[:image])
33
+
34
+ operations = [
35
+ create_hash,
36
+ set_parent_alive_hash,
37
+ block_location_hash,
38
+ last_edited_time_parent_hash,
39
+ last_edited_time_child_hash,
40
+ title_hash,
41
+ source_url_hash,
42
+ display_source_url_hash,
43
+ ]
44
+
45
+ request_url = URLS[:UPDATE_BLOCK]
46
+ request_body = Utils::BlockComponents.build_payload(operations, request_ids)
47
+ response = HTTParty.post(
48
+ request_url,
49
+ body: request_body.to_json,
50
+ cookies: cookies,
51
+ headers: headers,
52
+ )
53
+ 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}.
54
+ Please try again, and if issues persist open an issue in GitHub."; end
55
+
56
+ self.new(new_block_id, block_title, block_id)
57
+ else
58
+ raise ArgumentError, "Currently, images can only be created through a public image URL."
59
+ end
14
60
  end
15
61
  end
16
- end
62
+ end
63
+ end
@@ -1,16 +1,16 @@
1
1
  module NotionAPI
2
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
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
15
14
  end
16
- end
15
+ end
16
+ end
@@ -1,15 +1,15 @@
1
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
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
14
9
  end
15
- end
10
+
11
+ class << self
12
+ attr_reader :notion_type, :type
13
+ end
14
+ end
15
+ end
@@ -1,130 +1,123 @@
1
-
2
1
  module NotionAPI
3
2
 
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)
3
+ # Page Block, entrypoint for the application
4
+ class PageBlock < BlockTemplate
5
+ @notion_type = "page"
6
+ @type = "page"
7
+
8
+ def type
9
+ NotionAPI::PageBlock.notion_type
10
+ end
11
+
12
+ class << self
13
+ attr_reader :notion_type, :type
14
+ end
15
+
16
+ def get_block(url_or_id)
17
+ # ! retrieve a Notion Block and return its instantiated class object.
18
+ # ! url_or_id -> the block ID or URL : ``str``
19
+ get(url_or_id)
20
+ end
21
+
22
+ def get_collection(url_or_id)
23
+ # ! retrieve a Notion Collection and return its instantiated class object.
24
+ # ! url_or_id -> the block ID or URL : ``str``
25
+ clean_id = extract_id(url_or_id)
26
+
27
+ request_body = {
28
+ pageId: clean_id,
29
+ chunkNumber: 0,
30
+ limit: 100,
31
+ verticalColumns: false,
32
+ }
33
+ jsonified_record_response = get_all_block_info(request_body)
34
+
35
+ block_parent_id = extract_parent_id(clean_id, jsonified_record_response)
36
+ block_collection_id = extract_collection_id(clean_id, jsonified_record_response)
37
+ block_view_id = extract_view_ids(clean_id, jsonified_record_response).join
38
+ block_title = extract_collection_title(clean_id, block_collection_id, jsonified_record_response)
39
+
40
+ CollectionView.new(clean_id, block_title, block_parent_id, block_collection_id, block_view_id)
41
+ end
42
+
43
+ def create_collection(collection_type, collection_title, data)
44
+ # ! create a Notion Collection View and return its instantiated class object.
45
+ # ! _collection_type -> the type of collection to create : ``str``
46
+ # ! collection_title -> the title of the collection view : ``str``
47
+ # ! data -> JSON data to add to the table : ``str``
48
+
49
+ valid_types = %w[table board list timeline calendar gallery]
50
+ unless valid_types.include?(collection_type); raise ArgumentError, "That collection type is not yet supported. Try: #{valid_types.join}."; end
51
+ cookies = Core.options["cookies"]
52
+ headers = Core.options["headers"]
53
+
54
+ new_block_id = extract_id(SecureRandom.hex(16))
55
+ parent_id = extract_id(SecureRandom.hex(16))
56
+ collection_id = extract_id(SecureRandom.hex(16))
57
+ view_id = extract_id(SecureRandom.hex(16))
58
+
59
+ children = []
60
+ alive_blocks = []
61
+ data.each do |_row|
62
+ child = extract_id(SecureRandom.hex(16))
63
+ children.push(child)
64
+ alive_blocks.push(Utils::CollectionViewComponents.set_collection_blocks_alive(child, collection_id))
48
65
  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
66
+
67
+ request_id = extract_id(SecureRandom.hex(16))
68
+ transaction_id = extract_id(SecureRandom.hex(16))
69
+ space_id = extract_id(SecureRandom.hex(16))
70
+
71
+ request_ids = {
72
+ request_id: request_id,
73
+ transaction_id: transaction_id,
74
+ space_id: space_id,
75
+ }
76
+
77
+ create_collection_view = Utils::CollectionViewComponents.create_collection_view(new_block_id, collection_id, view_id)
78
+ configure_view = Utils::CollectionViewComponents.set_view_config(collection_type, new_block_id, view_id, children)
79
+
80
+ # returns the JSON and some useful column mappings...
81
+ column_data = Utils::CollectionViewComponents.set_collection_columns(collection_id, new_block_id, data)
82
+ configure_columns_hash = column_data[0]
83
+ column_mappings = column_data[1]
84
+ set_parent_alive_hash = Utils::BlockComponents.set_parent_to_alive(@id, new_block_id)
85
+ add_block_hash = Utils::BlockComponents.block_location_add(@id, @id, new_block_id, nil, "listAfter")
86
+ new_block_edited_time = Utils::BlockComponents.last_edited_time(new_block_id)
87
+ collection_title_hash = Utils::CollectionViewComponents.set_collection_title(collection_title, collection_id)
88
+
89
+ operations = [
90
+ create_collection_view,
91
+ configure_view,
92
+ configure_columns_hash,
93
+ set_parent_alive_hash,
94
+ add_block_hash,
95
+ new_block_edited_time,
96
+ collection_title_hash,
97
+ ]
98
+ operations << alive_blocks
99
+ all_ops = operations.flatten
100
+ data.each_with_index do |row, i|
101
+ child = children[i]
102
+ row.keys.each_with_index do |col_name, j|
103
+ child_component = Utils::CollectionViewComponents.insert_data(child, j.zero? ? "title" : col_name, row[col_name], column_mappings[j])
104
+ all_ops.push(child_component)
113
105
  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
106
  end
107
+
108
+ request_url = URLS[:UPDATE_BLOCK]
109
+ request_body = build_payload(all_ops, request_ids)
110
+ response = HTTParty.post(
111
+ request_url,
112
+ body: request_body.to_json,
113
+ cookies: cookies,
114
+ headers: headers,
115
+ )
116
+
117
+ 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}.
118
+ Please try again, and if issues persist open an issue in GitHub."; end
119
+
120
+ CollectionView.new(new_block_id, collection_title, parent_id, collection_id, view_id)
129
121
  end
130
- end
122
+ end
123
+ end