はじめに
hkob の雑記録の第470回目(連続43日目)は、Update a comment の前にRetrieve a comment を実装します。
コメントの準備
昨日変更してしまったコメントを再度 API で戻します。これまではページやブロックから discussion_thread をまとめて読むことしかできませんでした。今回は、id を指定してこのコメントを取得する Retrieve a comment を実装していきます。

スクリプト作成
この comment を取得するスクリプトを記述します。
curl 'https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b' \ -H 'Notion-Version: 2026-03-11' \ -H 'Authorization: Bearer '"$NOTION_API_KEY"''
make して json を取得します。
> touch *.json; make
sh retrieve_comment_edit_text.sh > retrieve_comment_edit_text.json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 736 100 736 0 0 1787 0 --:--:-- --:--:-- --:--:-- 1786
sleep 1
取得した JSON はこちらです。parent で親がページなのか、ブロックなのかを確認することができます。また、discussion_id も記録されているので、comment.discussion のようにして、DiscussionThread を取得できるようにしてもよさそうです。
{ "object": "comment", "id": "37921658-0840-45ae-924a-d9ce1d60375b", "parent": { "type": "page_id", "page_id": "c01166c6-13ae-45cb-b968-18b4ef2f5a77" }, "discussion_id": "44753616-4099-4a5f-97c6-53eb758e7a9d", "created_time": "2022-09-01T23:42:00.000Z", "last_edited_time": "2026-04-21T09:26:00.000Z", "created_by": { "object": "user", "id": "019a87c7-d197-44a4-b19a-baa684400f81" }, "rich_text": [ { "type": "text", "text": { "content": "comment for edit text", "link": null }, "annotations": { "bold": false, "italic": false, "strikethrough": false, "underline": false, "code": false, "color": "default" }, "plain_text": "comment for edit text", "href": null } ], "display_name": { "type": "integration", "resolved_name": "notion_ruby_mapping" }, "request_id": "4dd68e99-ae8a-4c71-9246-7c6c19dd126d" }
テストを作成
comment_object_spec.rb に find のテストを追加します。
describe "find" do let(:target) { CommentObject.find TestConnection::EDIT_TEXT_COMMENT_ID } it { expect(target.full_text).to eq "comment for edit text" } end
当然 find はないのでエラーになります。
undefined method 'find' for class NotionRubyMapping::CommentObject
実装を作成
comment_object.rb に find を作成します。
def self.find(comment_id) new json: NotionCache.instance.comment_request(comment_id) end
さらに NotionCache に comment_request を実装します。
def comment_request(comment_id) request :get, comment_path(comment_id) end
comment_path はテストが簡単に書けるので、先に書いておきます。comments_path のテストもなかったので追加しておきます。
describe "comments_path" do it { expect(nc.comments_path).to eq "v1/comments" } end describe "comment_path" do it { expect(nc.comment_path("ABC")).to eq "v1/comments/ABC" } end
実装は簡単です。
def comment_path(comment_id) "v1/comments/#{comment_id}" end
実装は動作し、API アクセスが実行されました。WebMock を書いていないので、 WebMock::NetConnectNotAllowedError: で止まりました。retrieve_comment メソッドを追加し、retrieve_comment_edit_text.json を mock に追加します。
def retrieve_comment generate_stubs_sub :get, __method__, :comment_path, { edit_text: [EDIT_TEXT_COMMENT_ID, 200], } end
テスト確認
これで Retrieve a comment のテスト・実装は完了です。無事にテストも通過しました。
NotionRubyMapping::CommentObject create from json is expected to eq "Comment to a page" find is expected to eq "comment for edit text" Finished in 0.00534 seconds (files took 0.19284 seconds to load) 2 examples, 0 failures
おわりに
Update a comment を実装する前に、単一コメントを取得する Retrieve a comment を実装しました。次回は、この comment を更新していきます。
https://hkob.notion.site/hkob-16dd8e4e98ab807cbe3cf3cc94cdfe0f?pvs=4hkob.notion.site