Update a comment by rich text の実装 : hkob の雑記録 (475)

はじめに

hkob の雑記録の第475回目(連続48日目)は、Update a comment by rich text を実装します。少し間が空いてしまってすみません。

スクリプトは作成済

スクリプトは雑記録 469 にて下調べをしており、この時に作成しており、JSON も取得しております。今回はこれを下にテストを記載します。

テストの記述

Rich text の更新は text_objects に格納されている RichTextArray の更新で行います。ただし、text_objects を直接書き換えると will_update フラグを更新できないので、comment から delegate で書き換えます。

    describe "update_comment" do
      let(:target) { CommentObject.find TestConnection::EDIT_TEXT_COMMENT_ID }

      context "when update by rich_text" do
        before { target.rich_text_objects = "update comment by rich text" }

        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: {
            "rich_text" => [
              {
                "type" => "text",
                "text" => {
                  "content": "update comment by rich text",
                  "link": nil,
                },
                "plain_text" => "update comment by rich text",
                "href" => nil,
              },
            ],
          }
        end

        context "when save" do
          before { target.save }

          it { expect(target.full_text).to eq "update comment by rich text" }
        end
      end
    end

実装の追加

これに合わせて実装を追加します。まず、id をインスタンス変数として保持したいので、initialize を修正しました。

    def initialize(id: nil, text_objects: nil, json: {})
      nc = NotionCache.instance
      if text_objects
        @id = nc.hex_id id
        @text_objects = RichTextArray.new "rich_text", text_objects: text_objects
        @json = {}
      elsif json
        @json = json
        @id = nc.hex_id json["id"]
        @text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
      else
        raise StandardError, "Either text_objects or json is required CommentObject"
      end
      @will_update = false
    end
    attr_reader :will_update, :text_objects, :json, :id

次に、rich_text_object= を実装します。delegate した上で will_update インスタンス変数を true にします。

    def rich_text_objects=(text_objects)
      @text_objects.rich_text_objects = text_objects
      @will_update = true
    end

save は dry_run と API 呼び出しの両方を記載します。

    def save(dry_run: false)
      return unless @will_update

      if dry_run
        Base.dry_run_script :patch, NotionCache.instance.comment_path(@json["id"]), {
          "rich_text" => @text_objects.property_values_json,
        }
      else
        json = NotionCache.instance.update_comment_request id, rich_text: @text_objects.property_values_json
        @rich_text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
        @will_update = false
      end
    end

NotionInstance の update_comment_request も追加します。次の markdown の実装の場所を空けておきます。

    def update_comment_request(comment_id, rich_text: nil)
      if rich_text
        request :patch, comment_path(comment_id), {"rich_text" => rich_text}
      else
        request :patch, comment_path(comment_id)
      end
    end

WebMock の修正

ここまで動作すると以下のように stub 作成するように言われます。

       Real HTTP connections are disabled. Please stub: PATCH https://api.notion.com/v1/comments/37921658-0840-45ae-924a-d9ce1d60375b with body '{"rich_text":[{"type":"text","text":{"content":"update comment by rich text","link":null},"plain_text":"update comment by rich text","href":null}]}' with headers {省略}

spec_helper に update_comment の stub を追加します。

    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,
                        },
                      ],
                    }],
      }
    end

テストの確認

WebMock を設定した結果、以下のようにテストが通過しました。

  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"

Finished in 0.00968 seconds (files took 0.41314 seconds to load)
4 examples, 0 failures

おわりに

とりあえず、rich_text でのコメント更新を実装しました。

https://hkob.notion.site/hkob-16dd8e4e98ab807cbe3cf3cc94cdfe0f?pvs=4hkob.notion.site