Skip to content

Commit ff3be17

Browse files
Merge pull request #32 from danmurphy1217/add_link_block
Add LinkBlock Support
2 parents f7ba2ef + 9308d58 commit ff3be17

File tree

7 files changed

+118
-37
lines changed

7 files changed

+118
-37
lines changed

lib/notion_api/blocks.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require_relative "notion_types/text_block"
1818
require_relative "notion_types/todo_block"
1919
require_relative "notion_types/toggle_block"
20+
require_relative "notion_types/link_block"
2021

2122
Classes = NotionAPI.constants.select { |c| NotionAPI.const_get(c).is_a? Class and c.to_s != 'BlockTemplate' and c.to_s != 'Core' and c.to_s !='Client' }
2223
notion_types = []

lib/notion_api/core.rb

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ def children_ids(url_or_id = @id)
7272
jsonified_record_response["block"][clean_id]["value"]["content"] || []
7373
end
7474

75+
def extract_id(url_or_id)
76+
# ! parse and clean the URL or ID object provided.
77+
# ! url_or_id -> the block ID or URL : ``str``
78+
http_or_https = url_or_id.match(/^(http|https)/) # true if http or https in url_or_id...
79+
collection_view_match = url_or_id.match(/(\?v=)/)
80+
81+
if (url_or_id.length == 36) && ((url_or_id.split("-").length == 5) && !http_or_https)
82+
# passes if url_or_id is perfectly formatted already...
83+
url_or_id
84+
elsif (http_or_https && (url_or_id.split("-").last.length == 32)) || (!http_or_https && (url_or_id.length == 32)) || (collection_view_match)
85+
# passes if either:
86+
# 1. a URL is passed as url_or_id and the ID at the end is 32 characters long or
87+
# 2. a URL is not passed and the ID length is 32 [aka unformatted]
88+
pattern = [8, 13, 18, 23]
89+
if collection_view_match
90+
id_without_view = url_or_id.split("?")[0]
91+
clean_id = id_without_view.split("/").last
92+
pattern.each { |index| clean_id.insert(index, "-") }
93+
clean_id
94+
else
95+
id = url_or_id.split("-").last
96+
pattern.each { |index| id.insert(index, "-") }
97+
id
98+
end
99+
else
100+
raise ArgumentError, "Expected a Notion page URL or a page ID. Please consult the documentation for further information."
101+
end
102+
end
103+
75104
private
76105

77106
def get_notion_id(body)
@@ -205,34 +234,34 @@ def extract_view_ids(clean_id, jsonified_record_response)
205234
jsonified_record_response["block"][clean_id]["value"]["view_ids"] || []
206235
end
207236

208-
def extract_id(url_or_id)
209-
# ! parse and clean the URL or ID object provided.
210-
# ! url_or_id -> the block ID or URL : ``str``
211-
http_or_https = url_or_id.match(/^(http|https)/) # true if http or https in url_or_id...
212-
collection_view_match = url_or_id.match(/(\?v=)/)
213-
214-
if (url_or_id.length == 36) && ((url_or_id.split("-").length == 5) && !http_or_https)
215-
# passes if url_or_id is perfectly formatted already...
216-
url_or_id
217-
elsif (http_or_https && (url_or_id.split("-").last.length == 32)) || (!http_or_https && (url_or_id.length == 32)) || (collection_view_match)
218-
# passes if either:
219-
# 1. a URL is passed as url_or_id and the ID at the end is 32 characters long or
220-
# 2. a URL is not passed and the ID length is 32 [aka unformatted]
221-
pattern = [8, 13, 18, 23]
222-
if collection_view_match
223-
id_without_view = url_or_id.split("?")[0]
224-
clean_id = id_without_view.split("/").last
225-
pattern.each { |index| clean_id.insert(index, "-") }
226-
clean_id
227-
else
228-
id = url_or_id.split("-").last
229-
pattern.each { |index| id.insert(index, "-") }
230-
id
231-
end
232-
else
233-
raise ArgumentError, "Expected a Notion page URL or a page ID. Please consult the documentation for further information."
234-
end
235-
end
237+
# def extract_id(url_or_id)
238+
# # ! parse and clean the URL or ID object provided.
239+
# # ! url_or_id -> the block ID or URL : ``str``
240+
# http_or_https = url_or_id.match(/^(http|https)/) # true if http or https in url_or_id...
241+
# collection_view_match = url_or_id.match(/(\?v=)/)
242+
243+
# if (url_or_id.length == 36) && ((url_or_id.split("-").length == 5) && !http_or_https)
244+
# # passes if url_or_id is perfectly formatted already...
245+
# url_or_id
246+
# elsif (http_or_https && (url_or_id.split("-").last.length == 32)) || (!http_or_https && (url_or_id.length == 32)) || (collection_view_match)
247+
# # passes if either:
248+
# # 1. a URL is passed as url_or_id and the ID at the end is 32 characters long or
249+
# # 2. a URL is not passed and the ID length is 32 [aka unformatted]
250+
# pattern = [8, 13, 18, 23]
251+
# if collection_view_match
252+
# id_without_view = url_or_id.split("?")[0]
253+
# clean_id = id_without_view.split("/").last
254+
# pattern.each { |index| clean_id.insert(index, "-") }
255+
# clean_id
256+
# else
257+
# id = url_or_id.split("-").last
258+
# pattern.each { |index| id.insert(index, "-") }
259+
# id
260+
# end
261+
# else
262+
# raise ArgumentError, "Expected a Notion page URL or a page ID. Please consult the documentation for further information."
263+
# end
264+
# end
236265

237266
def extract_collection_schema(collection_id, view_id, response = {})
238267
# ! retrieve the collection scehma. Useful for 'building' the backbone for a table.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module NotionAPI
2+
3+
# simiilar to code block but for mathematical functions.
4+
class LinkBlock < BlockTemplate
5+
@notion_type = "link_to_page"
6+
@type = "link_to_page"
7+
8+
def type
9+
NotionAPI::LinkBlock.notion_type
10+
end
11+
12+
class << self
13+
attr_reader :notion_type, :type
14+
end
15+
16+
def self.create(block_id, new_block_id, block_title, target, position_command, request_ids, options)
17+
block_title = super.extract_id(block_title)
18+
19+
cookies = Core.options["cookies"]
20+
headers = Core.options["headers"]
21+
22+
create_block_hash = Utils::BlockComponents.create(new_block_id, self.notion_type)
23+
block_location_hash = Utils::BlockComponents.block_location_add(block_id, block_id, block_title, new_block_id, position_command)
24+
last_edited_time_hash = Utils::BlockComponents.last_edited_time(block_id)
25+
remove_item_hash = Utils::BlockComponents.block_location_remove( super.parent_id, new_block_id)
26+
27+
operations = [
28+
create_block_hash,
29+
block_location_hash,
30+
last_edited_time_hash,
31+
remove_item_hash
32+
]
33+
34+
request_url = URLS[:UPDATE_BLOCK]
35+
request_body = Utils::BlockComponents.build_payload(operations, request_ids)
36+
37+
response = HTTParty.post(
38+
request_url,
39+
body: request_body.to_json,
40+
cookies: cookies,
41+
headers: headers,
42+
)
43+
44+
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}.
45+
Please try again, and if issues persist open an issue in GitHub."; end
46+
47+
self.new(new_block_id, block_title, block_id)
48+
end
49+
end
50+
end
51+

lib/notion_api/utils.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Utils
1111
class BlockComponents
1212
# ! Each function defined here builds one component that is included in each request sent to Notions backend.
1313
# ! Each request sent will contain multiple components.
14-
# TODO figure this out
14+
1515
def self.build_payload(operations, request_ids)
1616
# ! properly formats the payload for Notions backend.
1717
# ! operations -> an array of hashes that define the operations to perform : ``Array[Hash]``
@@ -351,14 +351,14 @@ def self.display_source(new_block_id, block_url)
351351
# ! new_block_id -> the ID of the new ImageBlock: ``str``
352352
# ! block_url -> the URL of the ImageBlock: ``str``
353353
{
354-
"id": new_block_id,
355-
"table": "block",
356-
"path": [
354+
id: new_block_id,
355+
table: "block",
356+
path: [
357357
"format",
358358
],
359-
"command": "update",
360-
"args": {
361-
"display_source": block_url,
359+
command: "update",
360+
args: {
361+
display_source: block_url,
362362
},
363363
}
364364
end

lib/notion_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module NotionAPI
2-
VERSION = '1.1.3'
2+
VERSION = '1.1.4'
33
end

spec/blocks_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
describe "#convert" do
1818
it "should convert the block to a different type and return the new block." do
1919
@block = $Block_spec_page.get_block($Block_spec_convert_id)
20-
filtered_classes = Classes.select { |cls| ![:CollectionView, :CollectionViewRow, :CollectionViewPage, :ImageBlock].include?(cls)}
20+
filtered_classes = Classes.select { |cls| ![:CollectionView, :CollectionViewRow, :CollectionViewPage, :ImageBlock, :LinkBlock].include?(cls)}
2121

2222
number_of_classes = filtered_classes.length
2323
class_for_conversion = filtered_classes[rand(0...number_of_classes)]

spec/markdown_spec.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)