NotionRubyMapping のアップデート(1) : hkob の雑記録 (156)

はじめに

これまで Alfred の解説を続けてきました。Alfred については一通り説明が終わったので、次の話題に移りたいと思います。hkob の雑記録の第156回目は、停滞していた NotionRubyMapping の大幅アップデートの計画について書いてみたいと思います。

File upload API

先日、File upload API がリリースされました。これまで外部の URL をリンクすることはできましたが、アップロードすることはできませんでした。NotionRubyMapping でも対応したいと思っていました。

今回の API では 20MB までのファイルアップロードは通常に実行できるのですが、それ以上のファイルは分割してマルチパート送信する必要があるそうです。NotionRubyMapping で対応するには、利用している Faraday でマルチパート送信をしなければいけません。調べたところ、以下の記事にあたりました。

qiita.com

この記事によると Faraday 2.0 以降では、faraday-multipart gem をインストールして使うとのことでした。現在の NotionRubyMapping では、faraday-middleware を使っていたので、1系列に縛られていました。せっかくなので、faraday-middleware を外し、Faraday 2.0 系列に移行してみようと思っています。

Faraday をさらに調べてみると 2.8.1 までは Ruby 2.6 以上となっていますが、2.9.0 以降(現在は 2.13.1 が最新)では、Ruby 3.0 以上となっています。個人で利用しているのは常に最新版の Ruby を使っているのですが、macOS に標準で入っているものがいまだに 2.6.10 となっています。今回、かなり大きく手を入れることにするので、システムの Ruby 2.6 については現在の 0.8.9 を最終バージョンとしたいと思います。

notion_ruby_mapping.gemspec の修正

とりあえず、notion_ruby_mapping.gemspec を修正してみます。変更したのは Faraday の最低条件である required_ruby_version>= 3.0 にした点と、 spec.add_dependency "faraday-middleware" を外した点です。

# frozen_string_literal: true

require_relative "lib/notion_ruby_mapping/version"

Gem::Specification.new do |spec|
  spec.name = "notion_ruby_mapping"
  spec.version = NotionRubyMapping::VERSION
  spec.authors = ["Hiroyuki KOBAYASHI"]
  spec.email = ["hkob@metro-cit.ac.jp"]

  spec.summary = "Notion Ruby mapping tool"
  spec.description = "Mapping tool from Notion Database/Page/Block to Ruby Objects."
  spec.homepage = "https://github.com/hkob/notion_ruby_mapping.git"
  spec.license = "MIT"
  spec.required_ruby_version = ">= 3.0"

  # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"

  # spec.metadata["homepage_uri"] = spec.homepage
  # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
  # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."

  # Specify which files should be added to the gem when it is released.
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
    `git ls-files -z`.split("\x0").reject do |f|
      (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
    end
  end
  spec.bindir = "exe"
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  # Uncomment to register a new dependency of your gem
  # spec.add_dependency "example-gem", "~> 1.0"

  spec.add_dependency "faraday"

  spec.add_development_dependency "guard"
  spec.add_development_dependency "guard-rspec"
  spec.add_development_dependency "rake"
  spec.add_development_dependency "rspec"
  spec.add_development_dependency "rubocop"
  spec.add_development_dependency "rubocop-rspec"
  spec.add_development_dependency "webmock"

  # For more information and examples about making a new gem, check out our
  # guide at: https://bundler.io/guides/creating_gem.html
end

bundle update したら以下のような Gem になりました。別のマシンで bundle してしまったので、すでに全て update 済です。faraday-middleware を外したので、faraday が最新版の 2.13.1 に上がっています。これまでのコードでは動かなくなっているので、明日から順次テスト及び実装を修正していきます。

> bundle
Using rake 13.3.0
Using public_suffix 6.0.2
Using addressable 2.8.7
Using ast 2.4.3
Using bigdecimal 3.2.1
Using bundler 2.4.13
Using coderay 1.1.3
Using rexml 3.4.1
Using crack 1.0.0
Using diff-lcs 1.6.2
Using uri 1.0.3
Using net-http 0.6.0
Using faraday-net_http 3.4.0
Using json 2.12.2
Using logger 1.7.0
Using faraday 2.13.1
Using ffi 1.17.2 (arm64-darwin)
Using formatador 1.1.0
Using rb-fsevent 0.11.2
Using rb-inotify 0.11.1
Using listen 3.9.0
Using lumberjack 1.2.10
Using nenv 0.3.0
Using shellany 0.0.1
Using notiffany 0.1.3
Using ostruct 0.6.1
Using method_source 1.1.0
Using pry 0.15.2
Using thor 1.3.2
Using guard 2.19.1
Using guard-compat 1.2.1
Using rspec-support 3.13.4
Using rspec-core 3.13.4
Using rspec-expectations 3.13.5
Using rspec-mocks 3.13.5
Using rspec 3.13.1
Using guard-rspec 4.7.3
Using hashdiff 1.2.0
Using language_server-protocol 3.17.0.5
Using lint_roller 1.1.0
Using notion_ruby_mapping 0.8.9 from source at `.` and installing its executables
Using parallel 1.27.0
Using racc 1.8.1
Using parser 3.3.8.0
Using prism 1.4.0
Using rainbow 3.1.1
Using regexp_parser 2.10.0
Using rubocop-ast 1.44.1
Using ruby-progressbar 1.13.0
Using unicode-emoji 4.0.4
Using unicode-display_width 3.1.4
Using rubocop 1.75.8
Using rubocop-rspec 3.6.0
Using webmock 3.25.1
Bundle complete! 8 Gemfile dependencies, 54 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

おわりに

とりあえず、gemspec だけ修正して gem が更新されたことを確認しました。Faraday はだいぶ使い方が変わっているので、テストはほぼ全滅です。これから修正していきます。

hkob.notion.site