rounders 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef43acf9007fdf8093f20dca5a2c078811d0db50
4
+ data.tar.gz: ae387006a1c61597efb26c2981ecce6bbf62d522
5
+ SHA512:
6
+ metadata.gz: f6efa757ec07292598b3f1fe4207a7f78676f99fe85ac238f8cb554c6d8c5592e600900591cfaa1b4b78e17eb0abaf4af937681e8b93fe9e8e4582a86a36d893
7
+ data.tar.gz: 182719aa56dca3afe0d822d3c7c5770f1c6fc55463a68abf170905cc06401a0aef28ce776b8d33b8abd842aa1aa7c18e3d5ee19013a7babe9572cf63a4bcf387
data/.gitignore ADDED
@@ -0,0 +1,30 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ ### https://raw.github.com/github/gitignore/0aeefb48c0ac885a516fb92f7f5bfd7e85ce7a37/Rails.gitignore
11
+
12
+ *.rbc
13
+ *.sassc
14
+ .sass-cache
15
+ capybara-*.html
16
+ .rspec
17
+ .rvmrc
18
+ /.bundle
19
+ /vendor/bundle
20
+ /log/*
21
+ /tmp/*
22
+ /db/*.sqlite3
23
+ /public/system/*
24
+ /coverage/
25
+ /spec/tmp/*
26
+ **.orig
27
+ rerun.txt
28
+ pickle-email-*.html
29
+ .project
30
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+
2
+ AllCops:
3
+ Exclude:
4
+ - 'vendor/**/*'
5
+ - 'spec/fixtures/**/*'
6
+ - 'tmp/**/*'
7
+ - 'templates/*'
8
+ ##################### Metrics ##################################
9
+
10
+ Metrics/LineLength:
11
+ Max: 130
12
+
13
+ Metrics/ClassLength:
14
+ CountComments: false
15
+ Max: 150
16
+
17
+ Metrics/LineLength:
18
+ Max: 150
19
+
20
+ ##################### Style ##################################
21
+
22
+ # Use nested module/class definitions instead of compact style.
23
+ # NG: module Gem::Search
24
+ Style/ClassAndModuleChildren:
25
+ Enabled: false
26
+
27
+ Style/AsciiComments:
28
+ Enabled: false
29
+
30
+ Style/Documentation:
31
+ Enabled: false
32
+
33
+ Style/DotPosition:
34
+ EnforcedStyle: trailing
35
+
36
+ Style/FrozenStringLiteralComment:
37
+ Enabled: false
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at rike422@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kiki.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ group :red_green_refactor, halt_on_fail: true do
2
+ guard :rubocop, all_on_start: false, cli: ['--format', 'clang', '-D'] do
3
+ watch(/.+\.rb$/)
4
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
5
+ end
6
+
7
+ guard :rspec, cmd: 'bundle exec rspec --color --format documentation' do
8
+ watch(%r{^spec/.+_spec\.rb$})
9
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
10
+ watch('spec/spec_helper.rb') { 'spec' }
11
+ end
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 akira.takahashi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
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)
2
+
3
+ The pluggalbe mail processing framework
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rounders'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rounders
20
+
21
+ ## Usage
22
+
23
+ ### create bot
24
+
25
+ create a new bot by running the rounders new command after installing rounders.
26
+
27
+ ```
28
+ rounders new [name]
29
+ ```
30
+
31
+ ### generator
32
+
33
+ #### handler
34
+
35
+ The `rounders generate handler` command create template of handler into ./plugins/handlers/
36
+
37
+ ```
38
+ rounders generate handler [name] [method1, method2...] `
39
+ ```
40
+ ##### example
41
+
42
+ ```ruby
43
+ module Rounders
44
+ module Handlers
45
+ class MyHandler < Rounders::Handlers::Handler
46
+ # mail.body is include 'exmpale'
47
+ on({ body: 'example' }, :callback_method1)
48
+ # body include 'exmpale' AND subject include 'my_subject'
49
+ on({
50
+ body: 'example',
51
+ subject: /my_subject/},
52
+ :callback_method2)
53
+
54
+ def method1(mail)
55
+ // any process
56
+ end
57
+
58
+ def method2(mail)
59
+ // any process
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ ```
66
+
67
+ #### matcher
68
+
69
+ coming soon...
70
+
71
+ #### reciever
72
+
73
+ coming soon...
74
+
75
+ ## Development
76
+
77
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
78
+
79
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rounders. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
84
+
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
89
+
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task default: :spec
data/bin/rounders ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'bundler/setup'
4
+ require 'kiki'
5
+
6
+ Kiki::Commander.start(ARGV)
data/circle.yml ADDED
@@ -0,0 +1,36 @@
1
+ ########################
2
+ # Customize the test machine
3
+ ########################
4
+ machine:
5
+
6
+ timezone:
7
+ Asia/Tokyo
8
+
9
+ # Version of ruby to use
10
+ ruby:
11
+ version:
12
+ 2.3.0
13
+
14
+ ########################
15
+ # Customize dependencies
16
+ ########################
17
+ dependencies:
18
+ cache_directories:
19
+ - "vendor/bundle"
20
+ override:
21
+ - bundle -j4 --path=vendor/bundle
22
+
23
+ ########################
24
+ # Customize test commands
25
+ ########################
26
+ test:
27
+ post:
28
+ - bundle exec rake
29
+
30
+ ########################
31
+ # Custom notifications
32
+ ########################
33
+ notify:
34
+ webhooks:
35
+ # A list of hashes representing hooks. Only the url field is supported.
36
+ - url: https://circleci.com/hooks/circle
@@ -0,0 +1,5 @@
1
+ module Rounders
2
+ module Brains
3
+ class Base; end
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'rounders/commands/sub_commands/generate'
2
+
3
+ module Rounders
4
+ class Commander
5
+ class << self
6
+ def start(argv = ARGV)
7
+ if app?
8
+ require 'rounders/commands/local_command'
9
+ Commands::LocalCommand.start(argv)
10
+ else
11
+ require 'rounders/commands/global_command'
12
+ Commands::GlobalCommand.start(argv)
13
+ end
14
+ end
15
+
16
+ def app?
17
+ Pathname(Rounders::CONFIG_DIR_PATH).exist?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ module Rounders
2
+ module Commands
3
+ class GlobalCommand < Thor
4
+ class_option :help, type: :boolean, aliases: '-h', desc: 'Help message.'
5
+ package_name 'rounders'
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
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module Rounders
2
+ module Commands
3
+ class LocalCommand < Thor
4
+ class_option :help, type: :boolean, aliases: '-h', desc: 'Help message.'
5
+ package_name 'rounders'
6
+
7
+ desc 'start', 'Start the Rounders'
8
+ method_option aliases: '-s'
9
+ option :dotenv
10
+ def start
11
+ rounder = Rounders::Rounder.new
12
+ rounder.dotenv if options[:dotenv]
13
+ rounder.start
14
+ end
15
+
16
+ desc 'generate [Type]', 'Generate new code'
17
+ method_option aliases: '-g'
18
+ subcommand 'generate', SubCommands::Generate
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Rounders
2
+ module Commands
3
+ module SubCommands
4
+ class Generate < Thor
5
+ desc 'handler <name> [<handlers>...]', 'Generate new handler'
6
+
7
+ def handler(name, *handlers)
8
+ Rounders::Generators::Handler.new(name: name, handlers: handlers).generate
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ module Rounders
2
+ module Generators
3
+ class App
4
+ attr_reader :name
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ def target
10
+ @target ||= Pathname(File.join(Dir.pwd, name))
11
+ end
12
+
13
+ def generate!
14
+ if target.exist?
15
+ puts "#{name} already exists"
16
+ exit
17
+ end
18
+ target.mkpath
19
+ FileUtils.copy_entry(template_path, target)
20
+ puts "create #{name}"
21
+ end
22
+
23
+ def template_path
24
+ Pathname('../../../../templates/app').expand_path(__FILE__)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,48 @@
1
+ require 'mustache'
2
+ module Rounders
3
+ module Generators
4
+ class Generator < Mustache
5
+ include Rounders::Plugins::Pluggable
6
+ attr_reader :name
7
+
8
+ class << self
9
+ def inherited(klass)
10
+ klass.template_path = Pathname('../../../../templates/generators').expand_path(__FILE__).to_s.freeze
11
+ klass.template_name = klass.name.demodulize.downcase
12
+ klass.template_extension = 'mustache.rb'
13
+ end
14
+ end
15
+
16
+ def initialize(name)
17
+ @name = name
18
+ end
19
+
20
+ def class_name
21
+ name.classify
22
+ end
23
+
24
+ def generate
25
+ mkpath
26
+ file_path = output_path.join(file_name)
27
+ file_path.open('w+') do |file|
28
+ file.puts(render)
29
+ end
30
+ puts "create #{file_path}"
31
+ end
32
+
33
+ private
34
+
35
+ def file_name
36
+ "#{name.downcase.underscore}.rb"
37
+ end
38
+
39
+ def mkpath
40
+ output_path.mkpath
41
+ end
42
+
43
+ def output_path
44
+ Pathname("#{Rounders::PLUGIN_DIR_PATH}/#{self.class.directory_name}")
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,12 @@
1
+ module Rounders
2
+ module Generators
3
+ class Handler < Generator
4
+ attr_accessor :handlers
5
+
6
+ def initialize(name: nil, handlers: [])
7
+ super(name)
8
+ @handlers = handlers
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,48 @@
1
+ module Rounders
2
+ module Handlers
3
+ class Handler
4
+ include Rounders::Plugins::Pluggable
5
+ attr_reader :matches, :rouder
6
+ class << self
7
+ class Dispatcher
8
+ attr_reader :matcher, :method_name
9
+
10
+ def initialize(method_name, matcher)
11
+ @matcher = matcher
12
+ @method_name = method_name
13
+ end
14
+ end
15
+
16
+ def inherited(child_class)
17
+ Rounders.handlers << child_class
18
+ end
19
+
20
+ def dispatch(rounder, mails)
21
+ mails.map do |mail|
22
+ dispatchers.each do |dispatcher|
23
+ matches = dispatcher.matcher.match(mail)
24
+ next if matches.blank?
25
+ new(rounder, matches).public_send(dispatcher.method_name, mail)
26
+ end
27
+ end
28
+ end
29
+
30
+ def on(option, method_name)
31
+ matcher = Rounders::Matchers::Matcher.build(option)
32
+ dispatchers << Dispatcher.new(method_name, matcher)
33
+ end
34
+
35
+ private
36
+
37
+ def dispatchers
38
+ @dispatchers ||= []
39
+ end
40
+ end
41
+
42
+ def initialize(rounder, matches)
43
+ @rounder = rounder
44
+ @matches = matches
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,18 @@
1
+ module Rounders
2
+ class Mail
3
+ extend Forwardable
4
+ def_delegators :@mail,
5
+ :body,
6
+ :cc,
7
+ :envelope_from,
8
+ :from,
9
+ :date,
10
+ :sender,
11
+ :subject,
12
+ :to
13
+
14
+ def initialize(mail)
15
+ @mail = mail
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module Rounders
2
+ module Matchers
3
+ class Body < Rounders::Matchers::Matcher
4
+ attr_reader :pattern
5
+
6
+ def initialize(pattern)
7
+ @pattern = pattern
8
+ end
9
+
10
+ def match(mail)
11
+ return if mail.body.nil?
12
+ mail.body.to_s.force_encoding('UTF-8').match(pattern)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Rounders
2
+ module Matchers
3
+ class CC < Rounders::Matchers::Matcher
4
+ attr_reader :pattern
5
+
6
+ def initialize(pattern)
7
+ @pattern = pattern
8
+ end
9
+
10
+ def match(message)
11
+ return if message.cc.nil?
12
+ matches = message.cc.map do |address|
13
+ address.match(pattern)
14
+ end
15
+ matches.compact
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Rounders
2
+ module Matchers
3
+ class From < Rounders::Matchers::Matcher
4
+ attr_reader :pattern
5
+
6
+ def initialize(pattern)
7
+ @pattern = pattern
8
+ end
9
+
10
+ def match(message)
11
+ return if message.from.nil?
12
+ matches = message.from.map do |address|
13
+ address.match(pattern)
14
+ end
15
+ matches.compact
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ module Rounders
2
+ module Matchers
3
+ class NoImplementError < StandardError
4
+ end
5
+ class Matcher
6
+ include Rounders::Plugins::Pluggable
7
+ attr_reader :matchers
8
+
9
+ def initialize(matchers)
10
+ @matchers = matchers
11
+ end
12
+
13
+ def match(message)
14
+ match_data = matchers.each_with_object({}) do |matcher, memo|
15
+ memo[matcher.class.name.demodulize.underscore.to_sym] = matcher.match(message)
16
+ end
17
+ return match_data if match_data.values.all?(&:present?)
18
+ nil
19
+ end
20
+
21
+ class << self
22
+ def build(conditions)
23
+ matchers = conditions.map do |key, pattern|
24
+ matcher = "#{name.deconstantize}::#{key.to_s.classify}".constantize
25
+ matcher.new(pattern)
26
+ end
27
+ new(matchers)
28
+ rescue NameError
29
+ raise Rounders::Matchers::NoImplementError
30
+ rescue StandardError => e
31
+ raise e
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ module Rounders
2
+ module Matchers
3
+ class Subject < Rounders::Matchers::Matcher
4
+ attr_reader :pattern
5
+
6
+ def initialize(pattern)
7
+ @pattern = pattern
8
+ end
9
+
10
+ def match(mail)
11
+ return if mail.subject.nil?
12
+ mail.subject.match(pattern)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Rounders
2
+ module Matchers
3
+ class To < Rounders::Matchers::Matcher
4
+ attr_reader :pattern
5
+
6
+ def initialize(pattern)
7
+ @pattern = pattern
8
+ end
9
+
10
+ def match(message)
11
+ return if message.to.nil?
12
+ matches = message.to.map do |address|
13
+ address.match(pattern)
14
+ end
15
+ matches.compact
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ require 'pathname'
2
+ module Rounders
3
+ module Plugins
4
+ module Pluggable
5
+ class << self
6
+ def included(klass)
7
+ klass.extend ClassMethods
8
+ PluginLoader.register(klass)
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def directory_name
14
+ @directory_name ||= name.demodulize.downcase.pluralize
15
+ end
16
+
17
+ def load_path
18
+ @load_path ||= File.join(Rounders::PLUGIN_DIR_PATH, directory_name)
19
+ end
20
+
21
+ def load_plugins
22
+ Pathname.glob("#{load_path}/*.rb").each do |plugin|
23
+ begin
24
+ puts "load #{plugin.expand_path}"
25
+ require_relative plugin.expand_path
26
+ rescue => e
27
+ puts e
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ module Rounders
2
+ module Plugins
3
+ class PluginLoader
4
+ class << self
5
+ def register(klass)
6
+ pluggable_classes << klass
7
+ end
8
+
9
+ def load_plugins
10
+ pluggable_classes.each(&:load_plugins)
11
+ end
12
+
13
+ private
14
+
15
+ def pluggable_classes
16
+ @_pluggable_classes ||= []
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,67 @@
1
+ module Rounders
2
+ class Receiver
3
+ class Config
4
+ attr_accessor :protocol
5
+ attr_accessor :mail_server_settings
6
+ attr_accessor :find_options
7
+ end
8
+ DEFALUT_FIND_OPTION = {
9
+ keys: %w(NOT SEEN)
10
+ }.freeze
11
+ attr_reader :client, :find_options
12
+
13
+ def initialize(client: nil, find_options: {})
14
+ @client = client
15
+ @find_options = DEFALUT_FIND_OPTION.merge(find_options)
16
+ end
17
+
18
+ def configure
19
+ config = Config.new
20
+ yield config
21
+ @client = Receiver.create_client(config)
22
+ end
23
+
24
+ def receive
25
+ @client.
26
+ find(find_options).
27
+ map { |message| Rounders::Mail.new(message) }
28
+ end
29
+
30
+ class << self
31
+ def configure
32
+ config = Config.new
33
+ yield config
34
+ @receiver = create_client(config)
35
+ end
36
+
37
+ def create_client(config)
38
+ retriever = parser.lookup_retriever_method(config.protocol)
39
+ new(
40
+ client: retriever.new(config.mail_server_settings),
41
+ find_options: config.find_options)
42
+ end
43
+
44
+ def receive
45
+ receivers.
46
+ map(&:receive).
47
+ flatten.
48
+ sort { |a, b| a.date <=> b.date }
49
+ end
50
+
51
+ def reset
52
+ @receiver = nil
53
+ @receivers = nil
54
+ end
55
+
56
+ private
57
+
58
+ def receivers
59
+ [@receiver]
60
+ end
61
+
62
+ def parser
63
+ ::Mail::Configuration.instance
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,50 @@
1
+ module Rounders
2
+ class Rounder
3
+ DEFAULT_INTERVAL = 10
4
+
5
+ def dotenv
6
+ Dotenv.load
7
+ end
8
+
9
+ def start
10
+ setup
11
+ polling
12
+ end
13
+
14
+ private
15
+
16
+ def handle(mails)
17
+ Rounders.handlers.map { |handler| handler.dispatch(self, mails) }
18
+ end
19
+
20
+ def load_config
21
+ Pathname.glob(File.join(Dir.pwd, 'config/initializers/*.rb')).each do |config|
22
+ require_relative config
23
+ end
24
+ end
25
+
26
+ def polling
27
+ loop do
28
+ round
29
+ sleep interval
30
+ end
31
+ end
32
+
33
+ def round
34
+ handle receive_mail
35
+ end
36
+
37
+ def receive_mail
38
+ Rounders::Receiver.receive
39
+ end
40
+
41
+ def setup
42
+ load_config
43
+ Rounders::Plugins::PluginLoader.load_plugins
44
+ end
45
+
46
+ def interval
47
+ DEFAULT_INTERVAL
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Rounders
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/rounders.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'rounders/version'
2
+
3
+ module Rounders
4
+ # Your code goes here...
5
+ CONFIG_DIR_PATH = File.join(Dir.pwd, 'config').freeze
6
+ PLUGIN_DIR_PATH = File.join(Dir.pwd, 'plugins').freeze
7
+ class << self
8
+ def handlers
9
+ @handlers ||= []
10
+ end
11
+ end
12
+ end
13
+
14
+ require 'active_support/core_ext/object'
15
+ require 'active_support/inflector'
16
+ require 'thor'
17
+ require 'forwardable'
18
+ require 'mail'
19
+ require 'dotenv'
20
+
21
+ require 'rounders/mail'
22
+ require 'rounders/plugins/plugin_loader'
23
+ require 'rounders/plugins/pluggable'
24
+ require 'rounders/matchers/matcher'
25
+ require 'rounders/matchers/subject'
26
+ require 'rounders/matchers/body'
27
+ require 'rounders/matchers/cc'
28
+ require 'rounders/matchers/from'
29
+ require 'rounders/matchers/to'
30
+ require 'rounders/handlers/handler'
31
+ require 'rounders/commander'
32
+ require 'rounders/receiver'
33
+ require 'rounders/rounder'
34
+ require 'rounders/brains/base'
35
+
36
+ require 'rounders/generators/generator'
37
+ require 'rounders/generators/app'
38
+ require 'rounders/generators/handler'
data/rounders.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rounders/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rounders'
8
+ spec.version = Rounders::VERSION
9
+ spec.authors = ['akira.takahashi']
10
+ spec.email = ['rike422@gmail.com']
11
+
12
+ spec.summary = 'the Pluggable Mail processing framework inspired by ruboty and mailman'
13
+ spec.homepage = 'https://github.com/rike422/rounders'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'bin'
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.11'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'simplecov'
25
+ spec.add_development_dependency 'codeclimate-test-reporter'
26
+ spec.add_development_dependency 'guard'
27
+ spec.add_development_dependency 'guard-rspec'
28
+ spec.add_development_dependency 'guard-rubocop', '~> 1.2.0'
29
+ spec.add_development_dependency 'pry'
30
+ spec.add_dependency 'activesupport'
31
+ spec.add_dependency 'dotenv'
32
+ spec.add_dependency 'mail', '~> 2.6.3'
33
+ spec.add_dependency 'mustache'
34
+ spec.add_dependency 'thor'
35
+ end
@@ -0,0 +1 @@
1
+ gem 'kiki'
@@ -0,0 +1,16 @@
1
+ Rounders::Receiver.configure do |config|
2
+ # please show more option
3
+ #
4
+ config.protocol = :imap
5
+ config.mail_server_settings = {
6
+ address: 'imap.gmail.com',
7
+ port: 993,
8
+ user_name: ENV['GMAIL_USER_NAME'],
9
+ password: ENV['GMAIL_PASSWORD'],
10
+ enable_ssl: true
11
+ }
12
+ config.find_options = {
13
+ # flag for whether to delete each receive mail after find Default: false
14
+ # delete_after_find: true
15
+ }
16
+ end
@@ -0,0 +1,14 @@
1
+ module Rounders
2
+ module Handlers
3
+ class {{ class_name }} < Rounders::Handlers::Handler
4
+ {{#handlers}}
5
+ on({ body: 'example' }, :{{.}})
6
+ {{/handlers}}
7
+ {{#handlers}}
8
+
9
+ def {{.}}(mail)
10
+ end
11
+ {{/handlers}}
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,278 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rounders
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - akira.takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.2.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.2.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: activesupport
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: dotenv
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: mail
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 2.6.3
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 2.6.3
181
+ - !ruby/object:Gem::Dependency
182
+ name: mustache
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: thor
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :runtime
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ description:
210
+ email:
211
+ - rike422@gmail.com
212
+ executables:
213
+ - rounders
214
+ extensions: []
215
+ extra_rdoc_files: []
216
+ files:
217
+ - ".gitignore"
218
+ - ".rspec"
219
+ - ".rubocop.yml"
220
+ - CODE_OF_CONDUCT.md
221
+ - Gemfile
222
+ - Guardfile
223
+ - LICENSE.txt
224
+ - README.md
225
+ - Rakefile
226
+ - bin/rounders
227
+ - circle.yml
228
+ - lib/rounders.rb
229
+ - lib/rounders/brains/base.rb
230
+ - lib/rounders/commander.rb
231
+ - lib/rounders/commands/global_command.rb
232
+ - lib/rounders/commands/local_command.rb
233
+ - lib/rounders/commands/sub_commands/generate.rb
234
+ - lib/rounders/generators/app.rb
235
+ - lib/rounders/generators/generator.rb
236
+ - lib/rounders/generators/handler.rb
237
+ - lib/rounders/handlers/handler.rb
238
+ - lib/rounders/mail.rb
239
+ - lib/rounders/matchers/body.rb
240
+ - lib/rounders/matchers/cc.rb
241
+ - lib/rounders/matchers/from.rb
242
+ - lib/rounders/matchers/matcher.rb
243
+ - lib/rounders/matchers/subject.rb
244
+ - lib/rounders/matchers/to.rb
245
+ - lib/rounders/plugins/pluggable.rb
246
+ - lib/rounders/plugins/plugin_loader.rb
247
+ - lib/rounders/receiver.rb
248
+ - lib/rounders/rounder.rb
249
+ - lib/rounders/version.rb
250
+ - rounders.gemspec
251
+ - templates/app/Gemfile
252
+ - templates/app/config/initializers/mail.rb
253
+ - templates/generators/handler.mustache.rb
254
+ homepage: https://github.com/rike422/rounders
255
+ licenses:
256
+ - MIT
257
+ metadata: {}
258
+ post_install_message:
259
+ rdoc_options: []
260
+ require_paths:
261
+ - lib
262
+ required_ruby_version: !ruby/object:Gem::Requirement
263
+ requirements:
264
+ - - ">="
265
+ - !ruby/object:Gem::Version
266
+ version: '0'
267
+ required_rubygems_version: !ruby/object:Gem::Requirement
268
+ requirements:
269
+ - - ">="
270
+ - !ruby/object:Gem::Version
271
+ version: '0'
272
+ requirements: []
273
+ rubyforge_project:
274
+ rubygems_version: 2.5.1
275
+ signing_key:
276
+ specification_version: 4
277
+ summary: the Pluggable Mail processing framework inspired by ruboty and mailman
278
+ test_files: []