retrieve a board view の実装 : hkob の雑記録 (489)

はじめに

hkob の雑記録の第489回目(連続62日目)は、BoardView の読み込みを実装します。

WebMock の追加

今回は payload が必要ない簡単なものだったので、先に stub を追加してしまいました。 BOARD_VIEW_ID に対する view の取得だけですので簡単です。

    def retrieve_view
      generate_stubs_sub :get, __method__, :view_path, {
        table_view: [TABLE_VIEW_ID, 200],
        board_view: [BOARD_VIEW_ID, 200],
      }
    end

Retrieve a board view のテスト

次に、board_view_spec.rb にテストを実装します。フィルタや並び順を設定していなかったので、nil のままになっています。あとでこの辺りは適当に設定してテストをやり直すかもしれません。また、group_by は create a board view の時に必要なものだけうまく実装したいので、今回はクラス化せずに後日実装していくことにします。このためテストからは除外しています。あとで追加することにします。

# frozen_string_literal: true

require_relative "../../spec_helper"

module NotionRubyMapping
  RSpec.describe BoardView do
    tc = TestConnection.instance
    let!(:nc) { tc.nc }

    describe "find" do
      context "when real access" do
        let(:target) { View.find TestConnection::BOARD_VIEW_ID }

        it { expect(target).to be_a BoardView }
        it { expect(target.id).to eq nc.hex_id(TestConnection::BOARD_VIEW_ID) }
        it { expect(target.type).to eq "board" }

        context "when filter and sorts" do
          let(:query) { target.query }

          it { expect(query.class).to eq Query }

          it { expect(query.filter).to be_nil }

          it { expect(query.sort).to be_nil }
        end
      end

      context "when dry run" do
        let(:dry_run) { View.find TestConnection::BOARD_VIEW_ID, dry_run: true }

        it_behaves_like "dry run", :get, :view_path, id: TestConnection::BOARD_VIEW_ID
      end
    end
  end
end

BoardView の実装

とりあえず、group_by などの実装をスキップするので board_view.rb については、クラス名と type の設定だけになっています。

# frozen_string_literal: true

module NotionRubyMapping
  # Notion view
  class BoardView < View
    # @param [String, nil] id
    # @param [String, nil] json
    def initialize(id: nil, json: nil)
      super id: id, json: json
    end

    # @return [String]
    def type
      "board"
    end
  end
end

View の修正

このままだと View type board is not implemented yet. と出てきたので、 "board" を追加しました。

    # @param [Hash] json
    # @return [NotionRubyMapping::View]
    def self.create_from_json(json)
      base_class = {
        "table" => TableView,
        "board" => BoardView,
      }[json["type"]]
      raise NotImplementedError, "View type #{json["type"]} is not implemented yet." unless base_class

      base_class.new json: json
    end

テスト確認

今回は retrieve だけの確認なので、テスト自体はあっという間に通過してしまいました。他の view も json までの読み込みまではサッと作ってしまった方がいいかもしれません。

NotionRubyMapping::BoardView
  find
    when real access
      is expected to be a kind of NotionRubyMapping::BoardView
      is expected to eq "32fd8e4e98ab80e2b3f8000c6da330b4"
      is expected to eq "board"
      when filter and sorts
        is expected to eq NotionRubyMapping::Query
        is expected to be nil
        is expected to be nil
    when dry run
      behaves like dry run
        is expected to eq "#!/bin/sh\ncurl  'https://api.notion.com/v1/views/32fd8e4e98ab80e2b3f8000c6da330b4' \\\n  -H 'Notion-Version: 2026-03-11' \\\n  -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''"

Finished in 0.01828 seconds (files took 1.22 seconds to load)
7 examples, 0 failures

おわりに

今回は BoardView の読み込みだけを実装してみました。他の view も同様に共通部分までは実装してしまおうと思います。

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