Skip to content

Commit

Permalink
Merge pull request #1 from hkob/development
Browse files Browse the repository at this point in the history
add 'after' option to append_block_chidren, etc
  • Loading branch information
hkob authored Jul 13, 2023
2 parents 7ad74f1 + f95f094 commit d87cb38
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ NotionRubyMapping.configuration { |c| c.notion_token = ENV["NOTION_API_TOKEN"] }

## 3. ChangeLog

- 2023/7/13 [v0.8.2] add 'after' option to append_block_chidren, 'append_after' method to block, and 'public_url' method to page
- 2023/7/10 [v0.8.1] Automatically change type to external when file object is updated
- 2023/6/4 [v0.8.0] add unique_id properties, filter_properties
- 2023/4/14 [v0.7.7] add token= method for Notion API book typo
Expand Down
4 changes: 3 additions & 1 deletion lib/notion_ruby_mapping/blocks/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ def get(key)
end

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

only_one = blocks.length == 1
Expand All @@ -132,6 +133,7 @@ def append_block_children(*blocks, dry_run: false)
block.block_json
end,
}
json["after"] = after if after
if dry_run
path = @nc.append_block_children_page_path(id)
self.class.dry_run_script :patch, path, json
Expand Down
7 changes: 7 additions & 0 deletions lib/notion_ruby_mapping/blocks/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def self.find(id, dry_run: false)
end
end

# @param [Boolean] dry_run true if dry_run
# @return [NotionRubyMapping::Block, String]
# @param [Array<Block>] blocks
def append_after(*blocks, dry_run: false)
parent.append_block_children *blocks, after: id, dry_run: dry_run
end

# @param [Boolean] not_update false when update
# @return [Hash{String (frozen)->Hash}]
def block_json(not_update: true)
Expand Down
11 changes: 11 additions & 0 deletions lib/notion_ruby_mapping/blocks/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,23 @@ def create_child_database(title, *assigns, dry_run: false)
db.save dry_run: dry_run
end

# @return [String] 公開URL
def public_url
@json["public_url"]
end

# @return [String] title
# @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#2ff7209055f346fbbda454cdbb40b1c8
def title
tp = properties.select { |p| p.is_a?(TitleProperty) || (p.is_a?(Property) && p.property_id == "title") }
tp.map(&:full_text).join ""
end

# @return [String] URL
def url
@json["url"]
end

protected

# @param [Boolean] dry_run true if dry_run
Expand Down Expand Up @@ -93,5 +103,6 @@ def update(dry_run: false)
update_json @nc.update_page_request(@id, property_values_json)
end
end

end
end
2 changes: 1 addition & 1 deletion lib/notion_ruby_mapping/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module NotionRubyMapping
VERSION = "0.8.1"
VERSION = "0.8.2"
NOTION_VERSION = "2022-06-28"
end
95 changes: 95 additions & 0 deletions spec/fixtures/append_block_children_append_after.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"object": "list",
"results": [
{
"object": "block",
"id": "67c0e9bd-bbe4-456c-8731-27763d7fa580",
"parent": {
"type": "block_id",
"block_id": "03f6460c-2673-4af4-84b9-5de15082d84e"
},
"created_time": "2023-07-12T12:20:00.000Z",
"last_edited_time": "2023-07-12T12:20:00.000Z",
"created_by": {
"object": "user",
"id": "40673a87-d8ed-41e0-aa55-7f0e8ace24cd"
},
"last_edited_by": {
"object": "user",
"id": "40673a87-d8ed-41e0-aa55-7f0e8ace24cd"
},
"has_children": false,
"archived": false,
"type": "numbered_list_item",
"numbered_list_item": {
"rich_text": [
{
"type": "text",
"text": {
"content": "Middle block",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Middle block",
"href": null
}
],
"color": "default"
}
},
{
"object": "block",
"id": "28b314c5-d2b5-4df8-8760-95b67a673d69",
"parent": {
"type": "block_id",
"block_id": "03f6460c-2673-4af4-84b9-5de15082d84e"
},
"created_time": "2023-07-12T09:44:00.000Z",
"last_edited_time": "2023-07-12T09:44:00.000Z",
"created_by": {
"object": "user",
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
},
"last_edited_by": {
"object": "user",
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
},
"has_children": false,
"archived": false,
"type": "numbered_list_item",
"numbered_list_item": {
"rich_text": [
{
"type": "text",
"text": {
"content": "Last block",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Last block",
"href": null
}
],
"color": "default"
}
}
],
"next_cursor": null,
"has_more": false,
"type": "block",
"block": {}
}
6 changes: 6 additions & 0 deletions spec/fixtures/append_block_children_append_after.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
curl -X PATCH 'https://api.notion.com/v1/blocks/03f6460c26734af484b95de15082d84e/children' \
-H 'Notion-Version: 2022-06-28' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
-H 'Content-Type: application/json' \
--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"}'
47 changes: 47 additions & 0 deletions spec/fixtures/retrieve_block_append_after_parent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"object": "block",
"id": "03f6460c-2673-4af4-84b9-5de15082d84e",
"parent": {
"type": "page_id",
"page_id": "c01166c6-13ae-45cb-b968-18b4ef2f5a77"
},
"created_time": "2023-07-11T23:24:00.000Z",
"last_edited_time": "2023-07-12T12:20:00.000Z",
"created_by": {
"object": "user",
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
},
"last_edited_by": {
"object": "user",
"id": "40673a87-d8ed-41e0-aa55-7f0e8ace24cd"
},
"has_children": true,
"archived": false,
"type": "callout",
"callout": {
"rich_text": [
{
"type": "text",
"text": {
"content": "Append after test callout",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Append after test callout",
"href": null
}
],
"icon": {
"type": "emoji",
"emoji": "💡"
},
"color": "gray_background"
}
}
3 changes: 3 additions & 0 deletions spec/fixtures/retrieve_block_append_after_parent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
curl 'https://api.notion.com/v1/blocks/03f6460c26734af484b95de15082d84e' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
-H 'Notion-Version: 2022-06-28'
43 changes: 43 additions & 0 deletions spec/fixtures/retrieve_block_append_after_previous.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"object": "block",
"id": "263f125b-179e-4e4f-996a-1eff812d9d3d",
"parent": {
"type": "block_id",
"block_id": "03f6460c-2673-4af4-84b9-5de15082d84e"
},
"created_time": "2023-07-12T09:42:00.000Z",
"last_edited_time": "2023-07-12T09:44:00.000Z",
"created_by": {
"object": "user",
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
},
"last_edited_by": {
"object": "user",
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
},
"has_children": false,
"archived": false,
"type": "numbered_list_item",
"numbered_list_item": {
"rich_text": [
{
"type": "text",
"text": {
"content": "First block",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "First block",
"href": null
}
],
"color": "default"
}
}
3 changes: 3 additions & 0 deletions spec/fixtures/retrieve_block_append_after_previous.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
curl 'https://api.notion.com/v1/blocks/263f125b179e4e4f996a1eff812d9d3d' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
-H 'Notion-Version: 2022-06-28'
11 changes: 7 additions & 4 deletions spec/fixtures/retrieve_page_top.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"object": "page",
"id": "c01166c6-13ae-45cb-b968-18b4ef2f5a77",
"created_time": "2022-02-07T21:12:00.000Z",
"last_edited_time": "2022-09-01T23:59:00.000Z",
"last_edited_time": "2023-07-12T12:18:00.000Z",
"created_by": {
"object": "user",
"id": "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"
Expand All @@ -13,8 +13,10 @@
},
"cover": null,
"icon": {
"type": "emoji",
"emoji": "😀"
"type": "external",
"external": {
"url": "https://cdn.profile-image.st-hatena.com/users/hkob/profile.png"
}
},
"parent": {
"type": "block_id",
Expand Down Expand Up @@ -46,5 +48,6 @@
]
}
},
"url": "https://www.notion.so/notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77"
"url": "https://www.notion.so/notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77",
"public_url": "https://hkob.notion.site/notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77"
}
37 changes: 36 additions & 1 deletion spec/notion_ruby_mapping/blocks/block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module NotionRubyMapping

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

describe "append block children with after" do
parent_id = TestConnection::APPEND_AFTER_PARENT_ID
previous_id = TestConnection::APPEND_AFTER_PREVIOUS_ID
append_block = NumberedListItemBlock.new "Middle block"
let(:parent_block) { Block.find parent_id }
let(:above_block) { Block.find previous_id }

shared_examples "append_block_children_append_after" do
context "dry_run" do
it_behaves_like :dry_run, :patch, :append_block_children_page_path, id: parent_id,
json: {
"children" => [append_block.block_json],
"after" => previous_id,
}
end

context "create" do
it { expect(block.id).to eq TestConnection::APPEND_AFTER_ADDED_ID }
end
end

context "after option" do
let(:dry_run) { parent_block.append_block_children append_block, after: previous_id, dry_run: true }
let(:block) { parent_block.append_block_children append_block, after: previous_id }
it_behaves_like "append_block_children_append_after"
end

context "append_after method" do
let(:dry_run) { above_block.append_after append_block, dry_run: true }
let(:block) { above_block.append_after append_block }
it_behaves_like "append_block_children_append_after"
end
end

describe "destroy" do
let(:id) { TestConnection::DESTROY_BLOCK_ID }
let(:target) { Block.new(id: id) }
Expand Down
8 changes: 6 additions & 2 deletions spec/notion_ruby_mapping/blocks/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ module NotionRubyMapping
expect(subject.call.id).to eq nc.hex_id(TestConnection::TOP_PAGE_ID)
end

it "receive title" do
expect(subject.call.title).to eq "notion_ruby_mapping_test_data"
it "receive title, url, public_url" do
page = subject.call
page_id = "notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77"
expect(page.title).to eq "notion_ruby_mapping_test_data"
expect(page.url).to eq "https://www.notion.so/#{page_id}"
expect(page.public_url).to eq "https://hkob.notion.site/#{page_id}"
end

describe "dry_run" do
Expand Down
Loading

0 comments on commit d87cb38

Please sign in to comment.