rounders 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -2
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +10 -1
  5. data/.travis.yml +9 -0
  6. data/README.ja.md +196 -0
  7. data/README.md +56 -8
  8. data/lib/rounders.rb +18 -10
  9. data/lib/rounders/commands/global_command.rb +13 -5
  10. data/lib/rounders/commands/sub_commands/generate.rb +24 -8
  11. data/lib/rounders/generators/app/app_generator.rb +46 -0
  12. data/lib/rounders/generators/app/templates/.rspec +2 -0
  13. data/lib/rounders/generators/app/templates/Gemfile +10 -0
  14. data/lib/rounders/generators/app/templates/MIT-LICENSE +20 -0
  15. data/lib/rounders/generators/app/templates/README.md.tt +21 -0
  16. data/lib/rounders/generators/app/templates/Rakefile +4 -0
  17. data/lib/rounders/generators/app/templates/app/handlers/.gitkeep +0 -0
  18. data/lib/rounders/generators/app/templates/app/matchers/.gitkeep +0 -0
  19. data/lib/rounders/generators/app/templates/app/receivers/.gitkeep +0 -0
  20. data/{templates/app → lib/rounders/generators/app/templates}/config/initializers/mail.rb +2 -2
  21. data/lib/rounders/generators/app/templates/gitignore +54 -0
  22. data/lib/rounders/generators/app/templates/spec/handlers/.gitkeep +0 -0
  23. data/lib/rounders/generators/app/templates/spec/matchers/.gitkeep +0 -0
  24. data/lib/rounders/generators/app/templates/spec/receivers/.gitkeep +0 -0
  25. data/lib/rounders/generators/app/templates/spec/spec_helper.rb.tt +105 -0
  26. data/lib/rounders/generators/app/templates/travis.yml +9 -0
  27. data/lib/rounders/generators/base.rb +69 -0
  28. data/lib/rounders/generators/handler/handler_generator.rb +16 -0
  29. data/lib/rounders/generators/handler/templates/%underscored_name%_handler.rb.tt +12 -0
  30. data/lib/rounders/generators/matcher/matcher_generator.rb +15 -0
  31. data/{templates/generators/matcher.mustache.rb → lib/rounders/generators/matcher/templates/%underscored_name%_matcher.rb.tt} +1 -1
  32. data/lib/rounders/generators/plugin/plugin_generator.rb +49 -0
  33. data/lib/rounders/generators/plugin/templates/.rspec +2 -0
  34. data/lib/rounders/generators/plugin/templates/Gemfile +4 -0
  35. data/lib/rounders/generators/plugin/templates/MIT-LICENSE +20 -0
  36. data/lib/rounders/generators/plugin/templates/README.md.tt +35 -0
  37. data/lib/rounders/generators/plugin/templates/Rakefile +4 -0
  38. data/lib/rounders/generators/plugin/templates/gitignore +54 -0
  39. data/lib/rounders/generators/plugin/templates/lib/rounders/%underscored_name%.rb.tt +6 -0
  40. data/lib/rounders/generators/plugin/templates/lib/rounders/%underscored_name%/version.rb.tt +5 -0
  41. data/lib/rounders/generators/plugin/templates/rounders-%underscored_name%.gemspec.tt +25 -0
  42. data/lib/rounders/generators/plugin/templates/spec/rounders/%underscored_name%_spec.rb.tt +7 -0
  43. data/lib/rounders/generators/plugin/templates/spec/rounders/.gitkeep +0 -0
  44. data/lib/rounders/generators/plugin/templates/spec/spec_helper.rb.tt +123 -0
  45. data/lib/rounders/generators/plugin/templates/travis.yml +9 -0
  46. data/lib/rounders/generators/receiver/receiver_generator.rb +15 -0
  47. data/lib/rounders/generators/receiver/templates/%underscored_name%_receiver.rb.tt +16 -0
  48. data/lib/rounders/handlers/handler.rb +3 -2
  49. data/lib/rounders/matchers/matcher.rb +4 -3
  50. data/lib/rounders/plugins/pluggable.rb +12 -2
  51. data/lib/rounders/receivers/mail.rb +41 -0
  52. data/lib/rounders/receivers/receiver.rb +29 -0
  53. data/lib/rounders/rounder.rb +1 -1
  54. data/lib/rounders/util.rb +20 -0
  55. data/lib/rounders/version.rb +1 -1
  56. data/rounders.gemspec +6 -5
  57. metadata +65 -33
  58. data/circle.yml +0 -36
  59. data/lib/rounders/generators/app.rb +0 -28
  60. data/lib/rounders/generators/generator.rb +0 -48
  61. data/lib/rounders/generators/handler.rb +0 -12
  62. data/lib/rounders/generators/matcher.rb +0 -9
  63. data/lib/rounders/receiver.rb +0 -67
  64. data/templates/app/Gemfile +0 -2
  65. data/templates/generators/handler.mustache.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1848b1a79ddcaa6da0ea0ea7504a91e8bc2d627c
4
- data.tar.gz: 8c7146f3cf50f54d99e4c77a313e61d24849a4f9
3
+ metadata.gz: 45547d405ca80484bf5a309a7f0784b0c6d0d16d
4
+ data.tar.gz: b1935a235c262dc8517499725dd60c56701b5a1a
5
5
  SHA512:
6
- metadata.gz: b063b78cc4418e6cfb8410ae63b71c6d87e362a0552dfbbc39a6d1136185926eafb7d6956bdbeca84ae1dcbb42d00331b0f84887f0f60305c920eaaff5ed0134
7
- data.tar.gz: 32aef46a039116f55d600022e0c3e6deb875113736d236384ab6a807d785e2f2e90fdebfb95e61713e3cd946e6d2a170c3415ce6639a72875bcb58434f66e128
6
+ metadata.gz: e5ddda05355cc11f219ff0cc6e62a77c8161a8034535f4c9f4277a92a6ca0eaec6e46805ea1c1d05053324958e85e6889a6fa48cfbfe86df1f2342a932fa369c
7
+ data.tar.gz: '049ab0e60c9eb09a101909ec29cde953285824520b04e2a86a34f3c6fd4715226a60cd40953d54a6b0dad48886dd6326b02ce2f3767e2f8635c492e41e8732b7'
data/.gitignore CHANGED
@@ -8,15 +8,15 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  ### https://raw.github.com/github/gitignore/0aeefb48c0ac885a516fb92f7f5bfd7e85ce7a37/Rails.gitignore
11
+ /verndor/bundle
11
12
 
12
13
  *.rbc
13
14
  *.sassc
14
15
  .sass-cache
15
16
  capybara-*.html
16
- .rspec
17
17
  .rvmrc
18
18
  /.bundle
19
- /vendor/bundle
19
+ /vendor/bundle/
20
20
  /log/*
21
21
  /tmp/*
22
22
  /db/*.sqlite3
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml CHANGED
@@ -4,7 +4,7 @@ AllCops:
4
4
  - 'vendor/**/*'
5
5
  - 'spec/fixtures/**/*'
6
6
  - 'tmp/**/*'
7
- - 'templates/*'
7
+ - 'templates/**/*'
8
8
  ##################### Metrics ##################################
9
9
 
10
10
  Metrics/LineLength:
@@ -35,3 +35,12 @@ Style/DotPosition:
35
35
 
36
36
  Style/FrozenStringLiteralComment:
37
37
  Enabled: false
38
+
39
+ Metrics/BlockLength:
40
+ Exclude:
41
+ - 'rounders.gemspec'
42
+ - 'spec/**/*'
43
+
44
+ Metrics/MethodLength:
45
+ CountComments: false # count full line comments?
46
+ Max: 30
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.3
4
+ - 2.4.0
5
+ before_install:
6
+ - gem install bundler -v 1.13.1
7
+ - gem update --system
8
+ env:
9
+ - CI=true
data/README.ja.md ADDED
@@ -0,0 +1,196 @@
1
+ # Rounders [![Build Status](https://travis-ci.org/rike422/rounders.svg?branch=master)](https://travis-ci.org/rike422/rounders) [![Code Climate](https://codeclimate.com/github/rike422/rounders/badges/gpa.svg)](https://codeclimate.com/github/rike422/rounders) [![Coverage Status](https://coveralls.io/repos/github/rike422/rounders/badge.svg?branch=master)](https://coveralls.io/github/rike422/rounders?branch=master)
2
+
3
+ Roundersは拡張性を重視した、メール処理フレームワークです。
4
+ RubotyなどのBotを参考に作られました。
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rounders'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rounders
22
+
23
+ ## Usage
24
+
25
+ ### botの作成
26
+
27
+ botを作成するには、下記のコマンドを実行します。
28
+
29
+ ```
30
+ rounders new [name]
31
+ ```
32
+
33
+ ### メールアカウントの設定
34
+
35
+ 生成されたbotの`config/initiarizers/mail.rb`にメールアカウント情報を設定します。
36
+ ```
37
+ Rounders::Receivers::Mail.configure do |config|
38
+ # please show more option
39
+ #
40
+ config.protocol = :imap
41
+ # メールアカウント情報を設定します。
42
+ config.mail_server_setting = {
43
+ address: 'imap.gmail.com',
44
+ port: 993,
45
+ user_name: ENV['GMAIL_USER_NAME'],
46
+ password: ENV['GMAIL_PASSWORD'],
47
+ enable_ssl: true
48
+ }
49
+ config.options = {
50
+ # flag for whether to delete each receive mail after find Default: false
51
+ # delete_after_find: true
52
+ }
53
+ end
54
+
55
+ ```
56
+
57
+ ### botの実行
58
+
59
+ `bundle exec rounders start`でbotをスタートできます。
60
+
61
+ ## Modules
62
+
63
+ Roundersはそれぞれのモジュールの基底クラスを継承することにより、
64
+ Hookが行われ、システム内に組み込むことができます。
65
+
66
+ 各モジュールのテンプレートを作成するジェネレータを用意しています。
67
+
68
+ #### Handlers
69
+
70
+ Handlersはメールのハンドングを行うモジュールです。
71
+ `.on`の第一引数で渡した条件で下記のMatcherインスタンスを作成し、
72
+ マッチしたメールを、第二引数で渡したメソッドに引き渡します
73
+
74
+ #### Generate Command
75
+
76
+ 下記のように`rounders generate handler`をroudersディレクトリ内で実行することで、
77
+ ./app/handlersの中にテンプレートファイルが生成されます。
78
+
79
+ ```
80
+ rounders generate handler [name] [method1, method2...] `
81
+ ```
82
+
83
+ ##### example
84
+
85
+ ```ruby
86
+ module Rounders
87
+ module Handlers
88
+ class MyHandler < Rounders::Handlers::Handler
89
+ # mail.body is include 'exmpale'
90
+ on({ body: 'example' }, :callback_method1)
91
+ # body include 'exmpale' AND subject match the /programing (?<name>.+)$/
92
+ on({
93
+ body: 'example',
94
+ subject: /programing (?<name>.+)$/},
95
+ :callback_method2)
96
+
97
+ def method1(mail)
98
+ # BodyMatcherの戻り値画が格納されています。
99
+ matches[:body]
100
+ # => #<MatchData "example">
101
+ # 引数のmail、matchesを利用して処理を行えます。
102
+ end
103
+
104
+ def method2(mail)
105
+ matches[:subject]
106
+ # => <MatchData "programing ruby" name:"ruby">
107
+ matches[:subject][:name]
108
+ # => "ruby"
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ ```
115
+
116
+ #### Matchers
117
+
118
+ Matcherはメールのフィルタリングを行うモジュールです。
119
+ `#match`メソッドを実装する必要があり、このメソッドが返した値は、
120
+ Handlersメソッド内で扱えるmatchesに格納されます。
121
+
122
+ #### Generate Command
123
+
124
+ 下記のように`rounders generate matchers`をroudersディレクトリ内で実行することで、
125
+ ./app/matchers/の中にテンプレートファイルが生成されます。
126
+
127
+ ```
128
+ rounders generate matchers [name]`
129
+ ```
130
+
131
+ 下記はHTMLメールに対して、CSSセレクタでフィルタ・抽出を行うためのMatcherの実装例です。
132
+
133
+ #### exmaple
134
+
135
+ /app/matchers/css_selector.rb
136
+
137
+ ```ruby
138
+ module Rounders
139
+ module Matchers
140
+ # クラス名をsnake_caseにした値が、handlerでのkeyになります。
141
+ class CssSelector < Rounders::Matchers::Matcher
142
+ attr_reader :pattern
143
+
144
+ def initialize(pattern)
145
+ @pattern = pattern
146
+ end
147
+
148
+ def match(mail)
149
+ return if mail.html_part.blank?
150
+ html_part = Nokogiri::HTML(mail.html_part.body.to_s)
151
+ node = html_part.css(pattern)
152
+ # 戻り値がmatches[:css_selector]に格納されます。
153
+ node.present? ? node : nil
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ ```
160
+
161
+ /app/handlers/your_hander.rb
162
+ ```ruby
163
+ module Rounders
164
+ module Handlers
165
+ class YourHandler < Rounders::Handlers::Handler
166
+ # 上記のCssSelectorを利用するためにキーを指定して、CSSセレクタを値として渡します。
167
+ on({ css_selector: 'body .header h2' }, method1)
168
+
169
+ def method1(mail)
170
+ # CssSelectorの戻り値が格納されています。
171
+ matches[:css_selector]
172
+ # =>[#<Nokogiri::XML::Element:0x3fc6d77f6ccc name="h2" children=[#<Nokogiri::XML::Text:0x3fc6d77f6ad8 " head text ">]>]
173
+ matches[:css_selector].to_s
174
+ # => '<h2> head text </h2>'
175
+ end
176
+ end
177
+ end
178
+ end
179
+
180
+ ```
181
+
182
+ #### Gems
183
+
184
+ RoundersのモジュールはGemとして配布できます。
185
+ 上記のCssSelectorをGemにしたものが下記のリポジトリになります。
186
+ - [rounders-css_selector_matcher](https://github.com/rike422/rounders-css_selector_matcher)
187
+
188
+ #### reciever
189
+
190
+ coming soon...
191
+
192
+
193
+ ## License
194
+
195
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
196
+
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
- # Rounders [![Circle CI](https://circleci.com/gh/rike422/rounders.svg?style=svg)](https://circleci.com/gh/rike422/rounders) [![Code Climate](https://codeclimate.com/github/rike422/rounders/badges/gpa.svg)](https://codeclimate.com/github/rike422/rounders) [![Coverage Status](https://coveralls.io/repos/github/rike422/rounders/badge.svg?branch=master)](https://coveralls.io/github/rike422/rounders?branch=master)
1
+ # Rounders [![Build Status](https://travis-ci.org/rike422/rounders.svg?branch=master)](https://travis-ci.org/rike422/rounders) [![Code Climate](https://codeclimate.com/github/rike422/rounders/badges/gpa.svg)](https://codeclimate.com/github/rike422/rounders) [![Coverage Status](https://coveralls.io/repos/github/rike422/rounders/badge.svg?branch=master)](https://coveralls.io/github/rike422/rounders?branch=master)
2
2
 
3
- The pluggalbe mail processing framework
3
+ [日本語README](https://github.com/rike422/rounders/blob/master/README.ja.md)
4
+
5
+ Rounders is a mail handling framework that emphasizes pluggability.
6
+ It was made with reference to Bot such as [Ruboty](https://github.com/r7kamura/ruboty),[Lita](https://github.com/litaio/lita).
4
7
 
5
8
  ## Installation
6
9
 
@@ -22,16 +25,53 @@ Or install it yourself as:
22
25
 
23
26
  ### create bot
24
27
 
25
- create a new bot by running the rounders new command after installing rounders.
28
+ To create a bot, execute the following command.
26
29
 
27
30
  ```
28
31
  rounders new [name]
29
32
  ```
30
33
 
31
- ### generator
34
+ ### Mail account settings
35
+
36
+ Set mail account information in `bot/config/initiarizers/ mail.rb` of the generated bot.
37
+
38
+ ```
39
+ Rounders::Receivers::Mail.configure do |config|
40
+ # please show more option
41
+ #
42
+ config.protocol = :imap
43
+ # メールアカウント情報を設定します。
44
+ config.mail_server_setting = {
45
+ address: 'imap.gmail.com',
46
+ port: 993,
47
+ user_name: ENV['GMAIL_USER_NAME'],
48
+ password: ENV['GMAIL_PASSWORD'],
49
+ enable_ssl: true
50
+ }
51
+ config.options = {
52
+ # flag for whether to delete each receive mail after find Default: false
53
+ # delete_after_find: true
54
+ }
55
+ end
56
+
57
+ ```
58
+
59
+ ### start bot
60
+
61
+ You can start the bot with `bundle exec rounders start`.
62
+
63
+ ### Modules
64
+
65
+
66
+ If you want to extend the processing of Rounders. inherit the base class of following modules.
67
+ it is hooked and can be incorporated into the Rounders.
32
68
 
33
69
  #### Handlers
34
70
 
71
+ Handlers is a module that handles mails.
72
+ Create the Matcher instance with the condition passed as the first argument of `.on`,
73
+ If there is a matching email, If there is a matching email, It passe to the method that passed as the second argument
74
+
35
75
  The `rounders generate handler` command create template of handler into ./plugins/handlers/
36
76
 
37
77
  ```
@@ -44,14 +84,15 @@ module Rounders
44
84
  module Handlers
45
85
  class MyHandler < Rounders::Handlers::Handler
46
86
  # mail.body is include 'exmpale'
47
- on({ body: 'example' }, :callback_method1)
87
+ on({ body: 'example' }, :method1)
48
88
  # body include 'exmpale' AND subject match the /programing (?<name>.+)$/
49
89
  on({
50
90
  body: 'example',
51
91
  subject: /programing (?<name>.+)$/},
52
- :callback_method2)
92
+ :method2)
53
93
 
54
94
  def method1(mail)
95
+ # The return value of BodyMatcher is assign to Handler#matches.
55
96
  matches[:body]
56
97
  # => #<MatchData "example">
57
98
  # any process
@@ -71,13 +112,18 @@ end
71
112
 
72
113
  #### Matchers
73
114
 
115
+ Matcher is a module that performs mail filtering.
116
+ The Matcher must implement 'match' method, and the value returned by this method which returns value assign to Handler#matches
117
+
118
+
74
119
  The `rounders generate matchers` command create template of matchers into ./plugins/matchers/
75
120
 
76
121
  ```
77
122
  rounders generate matchers [name]`
78
123
  ```
79
124
 
80
- your writen the matcher plugins that usable some handlers
125
+
126
+ Below is an example of Matcher for filtering / extracting with CSSSelector for HTML mail.
81
127
 
82
128
  #### exmaple
83
129
 
@@ -111,7 +157,7 @@ module Rounders
111
157
  module Handlers
112
158
  class YourHandler < Rounders::Handlers::Handler
113
159
  # css selector match
114
- on({ css_selector: 'body .header h2' }, method1)
160
+ on({ css_selector: 'body .header h2' }, :method1)
115
161
 
116
162
  def method1(mail)
117
163
  matches[:css_selector]
@@ -127,6 +173,8 @@ end
127
173
 
128
174
  #### Gems
129
175
 
176
+ Rounder's module can be distributed as Gem.
177
+
130
178
  - [rounders-css_selector_matcher](https://github.com/rike422/rounders-css_selector_matcher)
131
179
 
132
180
  #### reciever
data/lib/rounders.rb CHANGED
@@ -3,25 +3,30 @@ require 'rounders/version'
3
3
  module Rounders
4
4
  # Your code goes here...
5
5
  CONFIG_DIR_PATH = File.join(Dir.pwd, 'config').freeze
6
- PLUGIN_DIR_PATH = File.join(Dir.pwd, 'plugins').freeze
6
+ APP_PATH = File.join(Dir.pwd, 'app').freeze
7
7
  class << self
8
8
  def handlers
9
- @handlers ||= []
9
+ @_handlers ||= []
10
10
  end
11
11
 
12
12
  def matchers
13
- @matchers ||= {}
13
+ @_matchers ||= {}
14
+ end
15
+
16
+ def receivers
17
+ @_receivers ||= []
14
18
  end
15
19
  end
16
20
  end
17
21
 
18
- require 'active_support/core_ext/object'
19
- require 'active_support/inflector'
20
22
  require 'thor'
23
+ require 'thor/group'
24
+ require 'dry-configurable'
21
25
  require 'forwardable'
22
26
  require 'mail'
23
27
  require 'dotenv'
24
28
 
29
+ require 'rounders/util'
25
30
  require 'rounders/mail'
26
31
  require 'rounders/plugins/plugin_loader'
27
32
  require 'rounders/plugins/pluggable'
@@ -33,11 +38,14 @@ require 'rounders/matchers/from'
33
38
  require 'rounders/matchers/to'
34
39
  require 'rounders/handlers/handler'
35
40
  require 'rounders/commander'
36
- require 'rounders/receiver'
41
+ require 'rounders/receivers/receiver'
42
+ require 'rounders/receivers/mail'
37
43
  require 'rounders/rounder'
38
44
  require 'rounders/brains/base'
39
45
 
40
- require 'rounders/generators/generator'
41
- require 'rounders/generators/app'
42
- require 'rounders/generators/handler'
43
- require 'rounders/generators/matcher'
46
+ require 'rounders/generators/base'
47
+ require 'rounders/generators/app/app_generator'
48
+ require 'rounders/generators/plugin/plugin_generator'
49
+ require 'rounders/generators/handler/handler_generator'
50
+ require 'rounders/generators/matcher/matcher_generator'
51
+ require 'rounders/generators/receiver/receiver_generator'
@@ -4,11 +4,19 @@ module Rounders
4
4
  class_option :help, type: :boolean, aliases: '-h', desc: 'Help message.'
5
5
  package_name 'rounders'
6
6
 
7
- desc 'new [Path]', 'Generate new application'
8
- method_option aliases: '-n'
9
- def new(name)
10
- Rounders::Generators::App.new(name).generate!
11
- end
7
+ register(
8
+ Rounders::Generators::AppGenerator,
9
+ 'new',
10
+ 'new <name> <path>',
11
+ 'generate new application'
12
+ )
13
+
14
+ register(
15
+ Rounders::Generators::PluginGenerator,
16
+ 'plugin',
17
+ 'plugin <name>',
18
+ 'Generate new rounders plugin'
19
+ )
12
20
  end
13
21
  end
14
22
  end