はじめに
hkob の雑記録の第377回目(通算774日目)は、NotionRubyMapping に List data source templates を実装してみます。
テストデータの作成
まず、何も指定しない templates のスクリプト (list_data_source_templates_no_limit.sh) を作成します。
curl 'https://api.notion.com/v1/data_sources/4f93db514e1d4015b07f876e34c3b0b1/templates' \\ -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \\ -H 'Notion-Version: 2025-09-03'
make すると、以下の JSON が返却されました。昨日も解説したように Pagination 処理があるにも関わらず、List object になっていません。しかし、NotionRubyMapping では Pagination 処理は List object が担当しているので、List object に変換してしまいます。
{ "templates": [ { "id": "88f4a106-b84b-4d35-9c2d-256bcd55901f", "name": "template", "is_default": true }, { "id": "2e5d8e4e-98ab-808f-8c3a-f0819e3b3567", "name": "another template", "is_default": false } ], "has_more": false, "next_cursor": null }
Pagination のテストも実施するため、page_size を 1 にしたスクリプト(list_data_source_templates_limit_1.sh)も用意します。
curl 'https://api.notion.com/v1/data_sources/4f93db514e1d4015b07f876e34c3b0b1/templates?page_size=1' \\ -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \\ -H 'Notion-Version: 2025-09-03'
同様にこの続きを取得するスクリプト (list_data_source_templates_next_1.sh)も用意します。
curl '<https://api.notion.com/v1/data_sources/4f93db514e1d4015b07f876e34c3b0b1/templates?start_cursor=2e5d8e4e-98ab-808f-8c3a-f0819e3b3567>' \\ -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \\ -H 'Notion-Version: 2025-09-03'
DataSource にテストを追加
templates の取得は事前に取得した DataSource object に対して、templates というメソッドを呼んだ時に実行されることにします。このため、以下のようなテストを追加しました。
describe "templates" do let(:data_source) { DataSource.find data_source_id } context "when no limit" do let(:templates) { data_source.templates.to_a } it { expect(templates.count).to eq 2 } it { expect(templates.map(&:name)).to eq(["tempalte", "another template"]) } it { expect(templates.map(&:id)).to eq(%w[88f4a106b84b4d359c2d256bcd55901f 2e5d8e4e98ab808f8c3af0819e3b3567]) } end context "when limit 1" do let(:templates) { data_source.templates(page_size: 1).to_a } it { expect(templates.count).to eq 2 } it { expect(templates.map(&:name)).to eq(["tempalte", "another template"]) } it { expect(templates.map(&:id)).to eq(%w[88f4a106b84b4d359c2d256bcd55901f 2e5d8e4e98ab808f8c3af0819e3b3567]) } end end
stubs の設定
実装に入る前に、上で設定した API 呼び出しのための WebMock の stubs を用意してしまおうと思います。まず、 getnerate_stubs で list_data_source_templates というメソッドを呼び出すことにします。
def generate_stubs WebMock.enable! retrieve_page retrieve_database retrieve_data_source retrieve_block query_data_source update_page create_page create_database create_data_source update_database update_data_source retrieve_block_children append_block_children_page append_block_children_block destroy_block update_block retrieve_property retrieve_comments append_comment retrieve_user retrieve_users search append_after create_file_upload complete retrieve list_data_source_templates end
追加したメソッドは以下のようになります。
def list_data_source_templates generate_stubs_sub :get, __method__, :list_data_source_templates_path, { no_limit: [DATA_SOURCE_ID, 200], limit_1: [DATA_SOURCE_ID, 200, {"page_size" => 1}], next_1: [DATA_SOURCE_ID, 200, {"start_cursor" => "2e5d8e4e-98ab-808f-8c3a-f0819e3b3567"}], } end
ここで、list_data_source_templates_path が定義されていないので、大量にエラーになりました。まず、このテストを追加します。
describe "list_data_source_templates_path" do it { expect(nc.list_data_source_templates_path("ABC")).to eq "v1/data_sources/ABC/templates" } end
このメソッドを追加することで、メソッド未定義のエラーは出なくなりました。
def list_data_source_templates_path(data_source_id) "v1/data_sources/#{data_source_id}/templates" end
おわりに
今日は外出してしまったため、ほとんど進めることができませんでした。明日、新しく Template object のテストと実装を追加した上で、取得 API の実装と Pagination を実装していきます。