LoginSignup
2
2

More than 5 years have passed since last update.

Ruboty | ruboty_gen の生成する Action に rescue 付きのソースを生成する rescue オプションを追加 #ruboty

Last updated at Posted at 2014-12-23

Ruboty | ruboty_gen の生成する Action に rescue 付きのソースを生成する rescue オプションを追加 #ruboty

概要

ruboty_gen の生成する Action に rescue 付きのソースを生成する rescue オプションを追加します

前提知識

Ruboty | 【怠惰は正義】ruboty-gen で Handlers と Actions のひな形を作成

経緯

Ruboty の Plugin はその性質上、内部で他の API を利用することが多いです。
その際に、例外処理が必要になるため、API を利用した Plugin を作成する場合、
下記のような処理が必要になることが多々あります。

module Ruboty
  module SomePlugin
    module Actions
      class Convert < Ruboty::Actions::Base
        def call
          message.reply(your_action)
        end

        private

        def your_action
          ret = some_api_call
          some_converter(ret)
        rescue => e
          e.message
        end
      end
    end
  end
end

私は 怠惰 なのでたったこれだけではありますが毎回書きたくありません。
そこで、 Ruboty のテンプレートジェネレータである、 ruboty-gen に機能を追加します。

既存の ruboty-gen の出力確認

Ruboty Plugin のプロジェクトテンプレートを生成

$ ruboty-gen gem some_plugin some_action

自動生成された Action のひな形を確認

module Ruboty
  module SomePlugin
    module Actions
      class SomeAction < Ruboty::Actions::Base
        def call
          message.reply("TODO: write a message.")
        end
      end
    end
  end
end

rescue オプション動作イメージ

Ruboty Plugin のプロジェクトテンプレートを生成(rescue オプション付)

$ ruboty-gen gem some_plugin some_action --rescue

自動生成された Action のひな形を確認

module Ruboty
  module SomePlugin
    module Actions
      class SomeAction < Ruboty::Actions::Base
        def call
          message.reply(some_action)
        end
      end

      def some_action
        "TODO: write your logic."
      rescue => e
        e.message
      end
    end
  end
end

プルリクエスト

以上の仕様でプルリクエストを行いました。
既にマージして頂いているため、最新版の ruboty-gen を使用していれば当記事の機能を利用できます。

add_rescue_option pull request

参照

2
2
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
2
2