はじめに
hkob の雑記録の第477回目(連続50日目)は、Update a comment by markdown を実装します。rich_text が実装されているので、変更点は多くありません。
テストデータの作成
スクリプトは Richtext の部分を markdown に変更しただけです。markdown だと非常に簡潔でいいですね。
curl -X PATCH 'https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b' \ -H 'Notion-Version: 2026-03-11' \ -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \ -H 'Content-Type: application/json' \ --data '{ "markdown": "update comment by markdown" }'
make して json を取得します。
make sh update_comment_markdown.sh > update_comment_markdown.json sleep 1
テストを追加
markdown の場合には、markdown= で update するようにしてみます。
context "when update by markdown" do before { target.markdown = "update comment by markdown" } context "when dry_run" do let(:dry_run) { target.save dry_run: true } it_behaves_like "dry run", :patch, :comment_path, use_id: true, json: { "markdown" => "update comment by markdown", } end context "when save" do before { target.save } it { expect(target.full_text).to eq "update comment by markdown" } end end
当然、undefined method となります。
undefined method 'markdown=' for an instance of NotionRubyMapping::CommentObject
実装を追加
markdown= を実装します。とりあえず、 @markdown インスタンス変数に代入し、 @will_update を true にしておきます。
def markdown=(markdown) @markdown = markdown @will_update = true end
このままでは当然空の rich_text を生成してしまうので、save を対応します。
- --data '{"markdown":"update comment by markdown"}' + --data '{"rich_text":[]}'
save は @markdown の定義によって呼び出しを変更します。
def save(dry_run: false) return unless @will_update if dry_run json = @markdown ? {"markdown" => @markdown} : {"rich_text" => @text_objects.property_values_json} @markdown = nil Base.dry_run_script :patch, NotionCache.instance.comment_path(id), json else json = if @markdown NotionCache.instance.update_comment_request id, markdown: @markdown else NotionCache.instance.update_comment_request id, rich_text: @text_objects.property_values_json end @markdown = nil @text_objects = RichTextArray.new "rich_text", json: json["rich_text"] @will_update = false end end
dry_run の方は通過しました。次は NotionCache の update_comment_request に markdown キーを追加します。ここは前回残していた else の部分になります。
def update_comment_request(comment_id, rich_text: nil, markdown: nil) if rich_text request :patch, comment_path(comment_id), {"rich_text" => rich_text} else request :patch, comment_path(comment_id), {"markdown" => markdown} end end
これで実装は完了しました。あとは、 WebMock の NetConnectNotAllowedError となりました。
WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Please stub: PATCH https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b with body '{"markdown":"update comment by markdown"}' with headers {省略}
WebMock の stub を追加
あとは stub を追加するだけです。markdown の payload を追加しました。
def update_comment generate_stubs_sub :patch, __method__, :comment_path, { rich_text: [EDIT_TEXT_COMMENT_ID, 200, { "rich_text" => [ { "type" => "text", "text" => { "content" => "update comment by rich text", "link" => nil, }, "plain_text" => "update comment by rich text", "href" => nil, }, ], }], markdown: [EDIT_TEXT_COMMENT_ID, 200, { "markdown" => "update comment by markdown", }], } end
テスト確認
これでテストが無事に通過したことを確認しました。
NotionRubyMapping::CommentObject create from json is expected to eq "Comment to a page" find is expected to eq "comment for edit text" update_comment when update by rich_text when dry_run behaves like dry run is expected to eq "#!/bin/sh\ncurl -X PATCH 'https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b' \\\n ...ment by rich text\",\"link\":null},\"plain_text\":\"update comment by rich text\",\"href\":null}]}'" when save is expected to eq "update comment by rich text" when update by markdown when dry_run behaves like dry run is expected to eq "#!/bin/sh\ncurl -X PATCH 'https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b' \\\n ...n -H 'Content-Type: application/json' \\\n --data '{\"markdown\":\"update comment by markdown\"}'" when save is expected to eq "update comment by markdown" Finished in 0.00713 seconds (files took 0.22124 seconds to load) 6 examples, 0 failures
おわりに
markdown によるコメントの更新も実装できました。rich_text に比べると簡単でいいですね。
https://hkob.notion.site/hkob-16dd8e4e98ab807cbe3cf3cc94cdfe0f?pvs=4hkob.notion.site