Skip to content

Commit d87cb38

Browse files
authored
Merge pull request #1 from hkob/development
add 'after' option to append_block_chidren, etc
2 parents 7ad74f1 + f95f094 commit d87cb38

15 files changed

+306
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ NotionRubyMapping.configuration { |c| c.notion_token = ENV["NOTION_API_TOKEN"] }
127127

128128
## 3. ChangeLog
129129

130+
- 2023/7/13 [v0.8.2] add 'after' option to append_block_chidren, 'append_after' method to block, and 'public_url' method to page
130131
- 2023/7/10 [v0.8.1] Automatically change type to external when file object is updated
131132
- 2023/6/4 [v0.8.0] add unique_id properties, filter_properties
132133
- 2023/4/14 [v0.7.7] add token= method for Notion API book typo

lib/notion_ruby_mapping/blocks/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ def get(key)
118118
end
119119

120120
# @param [Array<Block>] blocks
121+
# @param [String, nil] after block id of previous block
121122
# @param [Boolean] dry_run true if dry_run
122123
# @return [NotionRubyMapping::Block, String]
123124
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#44bbf83d852c419485c5efe9fe1558fb
124125
# @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#2c47f7fedae543cf8566389ba1677132
125-
def append_block_children(*blocks, dry_run: false)
126+
def append_block_children(*blocks, after: nil, dry_run: false)
126127
raise StandardError, "This block can have no children." unless page? || (block? && can_have_children)
127128

128129
only_one = blocks.length == 1
@@ -132,6 +133,7 @@ def append_block_children(*blocks, dry_run: false)
132133
block.block_json
133134
end,
134135
}
136+
json["after"] = after if after
135137
if dry_run
136138
path = @nc.append_block_children_page_path(id)
137139
self.class.dry_run_script :patch, path, json

lib/notion_ruby_mapping/blocks/block.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ def self.find(id, dry_run: false)
7676
end
7777
end
7878

79+
# @param [Boolean] dry_run true if dry_run
80+
# @return [NotionRubyMapping::Block, String]
81+
# @param [Array<Block>] blocks
82+
def append_after(*blocks, dry_run: false)
83+
parent.append_block_children *blocks, after: id, dry_run: dry_run
84+
end
85+
7986
# @param [Boolean] not_update false when update
8087
# @return [Hash{String (frozen)->Hash}]
8188
def block_json(not_update: true)

lib/notion_ruby_mapping/blocks/page.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,23 @@ def create_child_database(title, *assigns, dry_run: false)
5959
db.save dry_run: dry_run
6060
end
6161

62+
# @return [String] 公開URL
63+
def public_url
64+
@json["public_url"]
65+
end
66+
6267
# @return [String] title
6368
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#2ff7209055f346fbbda454cdbb40b1c8
6469
def title
6570
tp = properties.select { |p| p.is_a?(TitleProperty) || (p.is_a?(Property) && p.property_id == "title") }
6671
tp.map(&:full_text).join ""
6772
end
6873

74+
# @return [String] URL
75+
def url
76+
@json["url"]
77+
end
78+
6979
protected
7080

7181
# @param [Boolean] dry_run true if dry_run
@@ -93,5 +103,6 @@ def update(dry_run: false)
93103
update_json @nc.update_page_request(@id, property_values_json)
94104
end
95105
end
106+
96107
end
97108
end

lib/notion_ruby_mapping/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

33
module NotionRubyMapping
4-
VERSION = "0.8.1"
4+
VERSION = "0.8.2"
55
NOTION_VERSION = "2022-06-28"
66
end
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"object": "list",
3+
"results": [
4+
{
5+
"object": "block",
6+
"id": "67c0e9bd-bbe4-456c-8731-27763d7fa580",
7+
"parent": {
8+
"type": "block_id",
9+
"block_id": "03f6460c-2673-4af4-84b9-5de15082d84e"
10+
},
11+
"created_time": "2023-07-12T12:20:00.000Z",
12+
"last_edited_time": "2023-07-12T12:20:00.000Z",
13+
"created_by": {
14+
"object": "user",
15+
"id": "40673a87-d8ed-41e0-aa55-7f0e8ace24cd"
16+
},
17+
"last_edited_by": {
18+
"object": "user",
19+
"id": "40673a87-d8ed-41e0-aa55-7f0e8ace24cd"
20+
},
21+
"has_children": false,
22+
"archived": false,
23+
"type": "numbered_list_item",
24+
"numbered_list_item": {
25+
"rich_text": [
26+
{
27+
"type": "text",
28+
"text": {
29+
"content": "Middle block",
30+
"link": null
31+
},
32+
"annotations": {
33+
"bold": false,
34+
"italic": false,
35+
"strikethrough": false,
36+
"underline": false,
37+
"code": false,
38+
"color": "default"
39+
},
40+
"plain_text": "Middle block",
41+
"href": null
42+
}
43+
],
44+
"color": "default"
45+
}
46+
},
47+
{
48+
"object": "block",
49+
"id": "28b314c5-d2b5-4df8-8760-95b67a673d69",
50+
"parent": {
51+
"type": "block_id",
52+
"block_id": "03f6460c-2673-4af4-84b9-5de15082d84e"
53+
},
54+
"created_time": "2023-07-12T09:44:00.000Z",
55+
"last_edited_time": "2023-07-12T09:44:00.000Z",
56+
"created_by": {
57+
"object": "user",
58+
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
59+
},
60+
"last_edited_by": {
61+
"object": "user",
62+
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
63+
},
64+
"has_children": false,
65+
"archived": false,
66+
"type": "numbered_list_item",
67+
"numbered_list_item": {
68+
"rich_text": [
69+
{
70+
"type": "text",
71+
"text": {
72+
"content": "Last block",
73+
"link": null
74+
},
75+
"annotations": {
76+
"bold": false,
77+
"italic": false,
78+
"strikethrough": false,
79+
"underline": false,
80+
"code": false,
81+
"color": "default"
82+
},
83+
"plain_text": "Last block",
84+
"href": null
85+
}
86+
],
87+
"color": "default"
88+
}
89+
}
90+
],
91+
"next_cursor": null,
92+
"has_more": false,
93+
"type": "block",
94+
"block": {}
95+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
curl -X PATCH 'https://api.notion.com/v1/blocks/03f6460c26734af484b95de15082d84e/children' \
3+
-H 'Notion-Version: 2022-06-28' \
4+
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
5+
-H 'Content-Type: application/json' \
6+
--data '{"children":[{"type":"numbered_list_item","object":"block","numbered_list_item":{"rich_text":[{"type":"text","text":{"content":"Middle block","link":null},"plain_text":"Middle block","href":null}],"color":"default"}}], "after": "263f125b179e4e4f996a1eff812d9d3d"}'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"object": "block",
3+
"id": "03f6460c-2673-4af4-84b9-5de15082d84e",
4+
"parent": {
5+
"type": "page_id",
6+
"page_id": "c01166c6-13ae-45cb-b968-18b4ef2f5a77"
7+
},
8+
"created_time": "2023-07-11T23:24:00.000Z",
9+
"last_edited_time": "2023-07-12T12:20:00.000Z",
10+
"created_by": {
11+
"object": "user",
12+
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
13+
},
14+
"last_edited_by": {
15+
"object": "user",
16+
"id": "40673a87-d8ed-41e0-aa55-7f0e8ace24cd"
17+
},
18+
"has_children": true,
19+
"archived": false,
20+
"type": "callout",
21+
"callout": {
22+
"rich_text": [
23+
{
24+
"type": "text",
25+
"text": {
26+
"content": "Append after test callout",
27+
"link": null
28+
},
29+
"annotations": {
30+
"bold": false,
31+
"italic": false,
32+
"strikethrough": false,
33+
"underline": false,
34+
"code": false,
35+
"color": "default"
36+
},
37+
"plain_text": "Append after test callout",
38+
"href": null
39+
}
40+
],
41+
"icon": {
42+
"type": "emoji",
43+
"emoji": "💡"
44+
},
45+
"color": "gray_background"
46+
}
47+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
curl 'https://api.notion.com/v1/blocks/03f6460c26734af484b95de15082d84e' \
2+
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
3+
-H 'Notion-Version: 2022-06-28'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"object": "block",
3+
"id": "263f125b-179e-4e4f-996a-1eff812d9d3d",
4+
"parent": {
5+
"type": "block_id",
6+
"block_id": "03f6460c-2673-4af4-84b9-5de15082d84e"
7+
},
8+
"created_time": "2023-07-12T09:42:00.000Z",
9+
"last_edited_time": "2023-07-12T09:44:00.000Z",
10+
"created_by": {
11+
"object": "user",
12+
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
13+
},
14+
"last_edited_by": {
15+
"object": "user",
16+
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
17+
},
18+
"has_children": false,
19+
"archived": false,
20+
"type": "numbered_list_item",
21+
"numbered_list_item": {
22+
"rich_text": [
23+
{
24+
"type": "text",
25+
"text": {
26+
"content": "First block",
27+
"link": null
28+
},
29+
"annotations": {
30+
"bold": false,
31+
"italic": false,
32+
"strikethrough": false,
33+
"underline": false,
34+
"code": false,
35+
"color": "default"
36+
},
37+
"plain_text": "First block",
38+
"href": null
39+
}
40+
],
41+
"color": "default"
42+
}
43+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
curl 'https://api.notion.com/v1/blocks/263f125b179e4e4f996a1eff812d9d3d' \
2+
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
3+
-H 'Notion-Version: 2022-06-28'

spec/fixtures/retrieve_page_top.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"object": "page",
33
"id": "c01166c6-13ae-45cb-b968-18b4ef2f5a77",
44
"created_time": "2022-02-07T21:12:00.000Z",
5-
"last_edited_time": "2022-09-01T23:59:00.000Z",
5+
"last_edited_time": "2023-07-12T12:18:00.000Z",
66
"created_by": {
77
"object": "user",
88
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
@@ -13,8 +13,10 @@
1313
},
1414
"cover": null,
1515
"icon": {
16-
"type": "emoji",
17-
"emoji": "😀"
16+
"type": "external",
17+
"external": {
18+
"url": "https://cdn.profile-image.st-hatena.com/users/hkob/profile.png"
19+
}
1820
},
1921
"parent": {
2022
"type": "block_id",
@@ -46,5 +48,6 @@
4648
]
4749
}
4850
},
49-
"url": "https://www.notion.so/notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77"
51+
"url": "https://www.notion.so/notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77",
52+
"public_url": "https://hkob.notion.site/notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77"
5053
}

spec/notion_ruby_mapping/blocks/block_spec.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module NotionRubyMapping
1616

1717
can_have_children = %i[bulleted_list_item paragraph inline_contents numbered_list_item synced_block template
1818
toggle toggle_heading_1 toggle_heading_2 toggle_heading_3 quote table to_do
19-
synced_block_original callout column_list column].include? key
19+
synced_block_original callout column_list column append_after_parent
20+
append_after_previous].include? key
2021
it "can #{key} have children? = #{can_have_children}" do
2122
expect(target.can_have_children).to eq can_have_children
2223
end
@@ -44,6 +45,40 @@ module NotionRubyMapping
4445
end
4546
end
4647

48+
describe "append block children with after" do
49+
parent_id = TestConnection::APPEND_AFTER_PARENT_ID
50+
previous_id = TestConnection::APPEND_AFTER_PREVIOUS_ID
51+
append_block = NumberedListItemBlock.new "Middle block"
52+
let(:parent_block) { Block.find parent_id }
53+
let(:above_block) { Block.find previous_id }
54+
55+
shared_examples "append_block_children_append_after" do
56+
context "dry_run" do
57+
it_behaves_like :dry_run, :patch, :append_block_children_page_path, id: parent_id,
58+
json: {
59+
"children" => [append_block.block_json],
60+
"after" => previous_id,
61+
}
62+
end
63+
64+
context "create" do
65+
it { expect(block.id).to eq TestConnection::APPEND_AFTER_ADDED_ID }
66+
end
67+
end
68+
69+
context "after option" do
70+
let(:dry_run) { parent_block.append_block_children append_block, after: previous_id, dry_run: true }
71+
let(:block) { parent_block.append_block_children append_block, after: previous_id }
72+
it_behaves_like "append_block_children_append_after"
73+
end
74+
75+
context "append_after method" do
76+
let(:dry_run) { above_block.append_after append_block, dry_run: true }
77+
let(:block) { above_block.append_after append_block }
78+
it_behaves_like "append_block_children_append_after"
79+
end
80+
end
81+
4782
describe "destroy" do
4883
let(:id) { TestConnection::DESTROY_BLOCK_ID }
4984
let(:target) { Block.new(id: id) }

spec/notion_ruby_mapping/blocks/page_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ module NotionRubyMapping
1616
expect(subject.call.id).to eq nc.hex_id(TestConnection::TOP_PAGE_ID)
1717
end
1818

19-
it "receive title" do
20-
expect(subject.call.title).to eq "notion_ruby_mapping_test_data"
19+
it "receive title, url, public_url" do
20+
page = subject.call
21+
page_id = "notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77"
22+
expect(page.title).to eq "notion_ruby_mapping_test_data"
23+
expect(page.url).to eq "https://www.notion.so/#{page_id}"
24+
expect(page.public_url).to eq "https://hkob.notion.site/#{page_id}"
2125
end
2226

2327
describe "dry_run" do

0 commit comments

Comments
 (0)