LoginSignup
13
13

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-10-24

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

概要

Ruboty の ruboty-gen で Handlers と Actions のひな形を作成します。

資料

ruboty-gen を利用しない場合の自作 Handler の作成については下記を参照。
Ruboty | 自作 Handler gem の作成

試用

仕様

下記記事と同じ仕様にします
Ruboty | 自作 Handler gem の作成

手順

  • ruboty-gen で自動生成を行います
$ ruboty-gen gem qiita_scouter
      create  ruboty-qiita_scouter/Gemfile
      create  ruboty-qiita_scouter/Rakefile
      create  ruboty-qiita_scouter/LICENSE.txt
      create  ruboty-qiita_scouter/README.md
      create  ruboty-qiita_scouter/.gitignore
      create  ruboty-qiita_scouter/ruboty-qiita_scouter.gemspec
      create  ruboty-qiita_scouter/lib/ruboty/qiita_scouter.rb
      create  ruboty-qiita_scouter/lib/ruboty/qiita_scouter/version.rb
Initializing git repo in /cygdrive/d/project/study/qiita/20141024_ruboty_ruboty-gen/sample/ruboty-qiita_scoutert
      create  ruboty-qiita_scouter/lib/ruboty/handlers/qiita_scouter.rb
      create  ruboty-qiita_scouter/lib/ruboty/qiita_scouter/actions/qiita_scouter.rb

  • 出力構成の確認
$ cd qiita_scouter
$ tree
.
┣ Gemfile
┣ lib
┃ ┗ ruboty
┃     ┣ handlers
┃     ┃ ┗ qiita_scouter.rb
┃     ┣ qiita_scouter
┃     ┃ ┣ actions
┃     ┃ ┃ ┗ qiita_scouter.rb
┃     ┃ ┗ version.rb
┃     ┗ qiita_scouter.rb
┣ LICENSE.txt
┣ Rakefile
┣ README.md
┗ ruboty-qiita_scouter.gemspec
  • ruboty-gen の出力ファイルの確認( lib/ruboty/qiita_scouter.rb )
$ cat lib/ruboty/qiita_scouter.rb
require "ruboty/qiita_scouter/version"
require "ruboty/handlers/qiita_scouter"
require "ruboty/qiita_scouter/actions/qiita_scouter"

module Ruboty
  module QiitaScouter
    # Your code goes here...
  end
end
  • ruboty-gen の出力ファイルの確認( lib/ruboty/handlers/qiita_scouter.rb )
$ cat lib/ruboty/handlers/qiita_scouter.rb
module Ruboty
  module Handlers
    class QiitaScouter < Base
      on /qiita scouter/, name: 'todo', description: 'TODO: write your description'

      def todo(message)
        Ruboty::QiitaScouter::Actions::QiitaScouter.new(message).call
      end
    end
  end
end
  • ruboty-gen の出力ファイルの確認( lib/ruboty/qiita_scouter/actions/qiita_scouter.rb )
$ cat lib/ruboty/qiita_scouter/actions/qiita_scouter.rb
module Ruboty
  module QiitaScouter
    module Actions
      class QiitaScouter < Ruboty::Actions::Base
        def call
          message.reply("TODO: write a message.")
        end
      end
    end
  end
end

※以降の手順は、 Ruboty | 自作 Handler gem の作成 と同様。

備考

2014/10/24現在の実装だと、 Action の生成内容が
Ruboty::QiitaScouter::Actions::QiitaScouter
になっていますが、 Actions というモジュール名から察するに
おそらく ruboty の作者さんの意図としては

Ruboty::QiitaScouter::Actions::ConcreteAction1
Ruboty::QiitaScouter::Actions::ConcreteAction2

のようにしたいのでは、と思います。
( ruboty の作者さんが作成した ruboty-github の実装をみると、そのような構成になっている。)

下記のような利用法、出力結果を想定し、
プルリクエストを作成しました。
https://github.com/blockgiven/ruboty-gen/pull/1
=>追記。マージしていただきました。ご対応ありがとうございます。

今回のプルリクエストは以下のメリットがある。

  • Ruboty:Handler::Base.on の name を自動で埋めることができるように
  • todo になっていたメソッド名も自動で埋まる
  • 1 Handler 複数 Actions 構成への対応
# 第1引数が handler 名。第2引数以降が 1-n 件のアクション名。
$ ruboty-gen gem qiita_scouter analyze random_analyze
$ tree
.
┣ Gemfile
┣ lib
┃ ┗ ruboty
┃     ┣ handlers
┃     ┃ ┗ qiita_scouter.rb
┃     ┣ qiita_scouter
┃     ┃ ┣ actions
┃     ┃ ┃ ┣ analyze.rb
┃     ┃ ┃ ┗ random_analyze.rb
┃     ┃ ┗ version.rb
┃     ┗ qiita_scouter.rb
┣ LICENSE.txt
┣ Rakefile
┣ README.md
┗ ruboty-qiita_scouter.gemspec
$ cat lib/ruboty/qiita_scouter.rb
require "ruboty/qiita_scouter/version"
require "ruboty/handlers/qiita_scouter"

module Ruboty
  module QiitaScouter
    # Your code goes here...
  end
end
  • ruboty-gen の出力ファイルの確認( lib/ruboty/handlers/qiita_scouter.rb )
$ cat lib/ruboty/handlers/qiita_scouter.rb
require "ruboty/qiita_scouter/actions/analyze"
require "ruboty/qiita_scouter/actions/random_analyze"

module Ruboty
  module Handlers
    class QiitaScouter < Base
      on /qiita scouter analyze/, name: 'analyze', description: 'TODO: write your description'
      on /qiita scouter random analyze/, name: 'random_analyze', description: 'TODO: write your description'

      def analyze(message)
        Ruboty::QiitaScouter::Actions::Analyze.new(message).call
      end

      def random_analyze(message)
        Ruboty::QiitaScouter::Actions::RandomAnalyze.new(message).call
      end
    end
  end
end
  • ruboty-gen の出力ファイルの確認( lib/ruboty/qiita_scouter/actions/qiita_scouter.rb )
$ cat lib/ruboty/qiita_scouter/actions/analyze.rb
module Ruboty
  module QiitaScouter
    module Actions
      class Analyze < Ruboty::Actions::Base
        def call
          message.reply("TODO: write a message.")
        end
      end
    end
  end
end

$ cat lib/ruboty/qiita_scouter/actions/random_analyze.rb
module Ruboty
  module QiitaScouter
    module Actions
      class RandomAnalyze < Ruboty::Actions::Base
        def call
          message.reply("TODO: write a message.")
        end
      end
    end
  end
end

参照

Ruboty GitHub
Ruboty RubyGems
ruboty-gen GitHub
ruboty-gen RubyGems
Ruboty | 自作 Handler gem の作成

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