NotionRubyMapping に Move page を実装(2) : hkob の雑記録 (381)

はじめに

hkob の雑記録の第381回目(通算778日目)は、NotionRubyMapping に対する Move page endpoint の実装を記録していきます。昨日のテストに引き続き実装を進めていきます。

WebMock の設定

WebMock の stubs メソッドを追加します。move_page 用のメソッドに記載することにします。

    def generate_stubs
      WebMock.enable!
      (中略)
      list_data_source_templates
      move_page
    end

昨日作成したスクリプトを参考に payload などを準備します。

    def move_page
      generate_stubs_sub :post, __method__, :move_page_path, {
        to_page: [
          MOVE_CHILDREN_PAGE_ID, 200,
          {
            "parent" => {
              "type" => "page_id",
              "page_id" => MOVE_PARENT_PAGE_ID,
            },
          }
        ],
        to_data_source: [
          MOVE_PARENT_PAGE_ID, 200,
          {
            "parent" => {
              "type" => "data_source_id",
              "data_source_id" => MOVE_PARENT_DATA_SOURCE_ID,
            },
          }
        ],
      }
    end

実装開始

まず、stub で使っている move_page_path がないと言われています。API のパスを作るメソッドです。

NoMethodError:
  undefined method 'move_page_path' for an instance of NotionRubyMapping::NotionCache

まず notion_cache_spec.rb にテストを記述します。

    describe "move_page_path" do
      it { expect(nc.move_page_path("ABC")).to eq "v1/pages/ABC/move" }
    end

move_page_path で API の URL を作成します。

    # @param [String] page_id
    # @return [String] move_page_path
    def move_page_path(page_id)
      "v1/pages/#{page_id}/move"
    en

あとは、実際に利用する move_to がないと言われるので、これを実装します。dry_run_script は専用のメソットがあるので、path と payload の json を渡せば完了です。実際の処理は move_page_request で実行します。

    # @param [Page, DataSource] page_or_data_source
    # @param [Boolean] dry_run true if dry_run
    # @return [NotionRubyMapping::Page, String]
    def move_to(page_or_data_source, dry_run: false)
      key = page_or_data_source.is_a?(Page) ? "page_id" : "data_source_id"
      json = {"parent" => {"type" => key, key => page_or_data_source.id}}
      if dry_run
        self.class.dry_run_script :post, @nc.move_page_path(@id), json
      else
        update_json @nc.move_page_request(@id, json)
      end
    end

notion_cache.rb 内の move_page_request は API path を作成し、payload を渡して、 request を発行するだけです。

    # @param [String] page_id
    # @param [Hash] payload
    # @return [Hash] response hash
    def move_page_request(page_id, payload)
      request :post, move_page_path(page_id), payload
    end

ここまで実装したことで、無事にテストが全て通過しました。

  move page
    when it moves to page
      when not dry_run
        is expected to eq "2e8d8e4e98ab80c5b4a1ef787d32f244"
        is expected to eq "2e8d8e4e-98ab-80ae-9fbe-f2d0fc4c4fea"
      when dry_run
        behaves like dry run
          is expected to eq "#!/bin/sh\\ncurl -X POST '<https://api.notion.com/v1/pages/2e8d8e4e98ab80c5b4a1ef787d32f244/move>' \\\\\\n...\\\\\\n  --data '{\\"parent\\":{\\"type\\":\\"page_id\\",\\"page_id\\":\\"2e8d8e4e98ab80ae9fbef2d0fc4c4fea\\"}}'"
    when it moves to data source
      when not dry_run
        is expected to eq "2e8d8e4e98ab80ae9fbef2d0fc4c4fea"
        is expected to eq "2e8d8e4e-98ab-80b1-a70e-000b9440843a"
      when dry_run
        behaves like dry run
          is expected to eq "#!/bin/sh\\ncurl -X POST '<https://api.notion.com/v1/pages/2e8d8e4e98ab80ae9fbef2d0fc4c4fea/move>' \\\\\\n...{\\"parent\\":{\\"type\\":\\"data_source_id\\",\\"data_source_id\\":\\"2e8d8e4e98ab80b1a70e000b9440843a\\"}}'"

おわりに

これで、無事にページの move page の機能が NotionRubyMapping 内に実装できました。NotionRubyMapping 内の実装自体もかなりブロック化してしまっているので、実装自体は15行程度で済んでしまいますね。今回もほとんどの時間はテストの作成時間でした。

hkob.notion.site