1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruboty で環境変数の必須チェックを行う

Posted at

Ruboty は環境変数の必須チェックを行う仕組みが用意されています

Env::Validatable

Ruboty は内部で Env::Validatable というモジュールを用意しており、
これが読み込まれているクラスでは

env(key, description, options = {})

を設定することで環境変数の必須チェックを設定することができます。
Ruboty::Handers::Base は Env::Validatable を include しているので

require "ruboty/hoge/actions/hoge"
require "ruboty/hoge/actions/hige"

module Ruboty
  module Handlers
    # An Ruboty Handler + Actions to hoge-hige
    class Hoge < Base
      on /hoge\z/, name: 'hoge', description: 'output hige'
      on /hige\z/, name: 'hige', description: 'output hige'
      env :DEFAULT_HOGE_TEXT1, "DEFAULT_HOGE_TEXT1 desc"
      env :DEFAULT_HOGE_TEXT2, "DEFAULT_HOGE_TEXT2 desc"

      def hoge(message)
        Ruboty::Hoge::Actions::Hoge.new(message).call
      end

      def hige(message)
        Ruboty::Hoge::Actions::Hige.new(message).call
      end

    end
  end
end

のようにしておけば、 DEFAULT_HOGE_TEXT1, DEFAULT_HOGE_TEXT2 に対して必須チェックが行われます。
未設定の場合は ruboty 実行時に以下の様なメッセージが表示されます。

Error: ENV["DEFAULT_HOGE_TEXT1"] is required but missing
    DEFAULT_HOGE_TEXT1                      - DEFAULT_HOGE_TEXT1 desc
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?