fragile 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.coveralls.yml +1 -0
- data/.gitignore +54 -6
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -4
- data/LICENSE.txt +22 -0
- data/README.md +61 -60
- data/Rakefile +1 -1
- data/bin/fragile +19 -19
- data/examples/example.rb +47 -33
- data/fragile.gemspec +21 -24
- data/lib/fragile.rb +14 -14
- data/lib/fragile/application.rb +68 -68
- data/lib/fragile/dsl.rb +18 -18
- data/lib/fragile/pipeline.rb +54 -54
- data/lib/fragile/pipeline_manager.rb +36 -36
- data/lib/fragile/plugin/console_output.rb +17 -17
- data/lib/fragile/plugin/im_kayac_output.rb +56 -0
- data/lib/fragile/plugin/mail_output.rb +50 -50
- data/lib/fragile/plugin/map_filter.rb +18 -18
- data/lib/fragile/plugin/rss_input.rb +36 -21
- data/lib/fragile/plugin/select_filter.rb +18 -18
- data/lib/fragile/plugin_manager.rb +38 -38
- data/lib/fragile/version.rb +11 -11
- data/{test/dsl_test.rb → spec/dsl_spec.rb} +17 -18
- data/{test/pipeline_manager_test.rb → spec/pipeline_manager_spec.rb} +94 -94
- data/{test/pipeline_test.rb → spec/pipeline_spec.rb} +108 -106
- data/spec/plugin/im_kayac_output_spec.rb +65 -0
- data/spec/plugin_manager_spec.rb +49 -0
- data/spec/spec_helper.rb +15 -0
- metadata +39 -12
- data/MIT-LICENSE +0 -23
- data/test/plugin_manager_test.rb +0 -57
- data/test/test_helper.rb +0 -14
data/lib/fragile/dsl.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
# Name:: Fragile
|
3
|
-
# Author:: tnakamura <http://d.hatena.ne.jp/griefworker>
|
4
|
-
# Created:: Jun 15, 2012
|
5
|
-
# Updated:: Jun 15, 2012
|
6
|
-
# Copyright:: tnakamura Copyright (c) 2012
|
7
|
-
# License:: Licensed under the MIT LICENSE.
|
8
|
-
|
9
|
-
module Fragile
|
10
|
-
module DSL
|
11
|
-
def pipeline(name, &block)
|
12
|
-
Fragile.application.define_pipeline(name, &block)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
self.extend Fragile::DSL
|
18
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
# Name:: Fragile
|
3
|
+
# Author:: tnakamura <http://d.hatena.ne.jp/griefworker>
|
4
|
+
# Created:: Jun 15, 2012
|
5
|
+
# Updated:: Jun 15, 2012
|
6
|
+
# Copyright:: tnakamura Copyright (c) 2012
|
7
|
+
# License:: Licensed under the MIT LICENSE.
|
8
|
+
|
9
|
+
module Fragile
|
10
|
+
module DSL
|
11
|
+
def pipeline(name, &block)
|
12
|
+
Fragile.application.define_pipeline(name, &block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
self.extend Fragile::DSL
|
18
|
+
|
data/lib/fragile/pipeline.rb
CHANGED
@@ -1,54 +1,54 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
# Name:: Fragile
|
3
|
-
# Author:: tnakamura <http://d.hatena.ne.jp/griefworker>
|
4
|
-
# Created:: Jun 15, 2012
|
5
|
-
# Updated:: Jun 15, 2012
|
6
|
-
# Copyright:: tnakamura Copyright (c) 2012
|
7
|
-
# License:: Licensed under the MIT LICENSE.
|
8
|
-
|
9
|
-
module Fragile
|
10
|
-
class PipelineError < Exception
|
11
|
-
end
|
12
|
-
|
13
|
-
class Pipeline
|
14
|
-
attr_reader :name
|
15
|
-
|
16
|
-
def initialize(name)
|
17
|
-
@name = name
|
18
|
-
@retry_count = 3
|
19
|
-
@plugins = []
|
20
|
-
end
|
21
|
-
|
22
|
-
def retry_count(count)
|
23
|
-
@retry_count = count
|
24
|
-
end
|
25
|
-
|
26
|
-
def use(plugin, config={})
|
27
|
-
@plugins << Fragile.application.create_plugin(plugin, config)
|
28
|
-
end
|
29
|
-
|
30
|
-
def run
|
31
|
-
data = []
|
32
|
-
@plugins.each do |plugin|
|
33
|
-
retry_handler do
|
34
|
-
data = plugin.call(data)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
def retry_handler(&block)
|
41
|
-
count = 0
|
42
|
-
begin
|
43
|
-
block.call
|
44
|
-
rescue => ex
|
45
|
-
Fragile.logger.error(ex)
|
46
|
-
if @retry_count < count
|
47
|
-
raise PipelineError.new("retry over error.")
|
48
|
-
end
|
49
|
-
count += 1
|
50
|
-
retry
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
1
|
+
# coding: utf-8
|
2
|
+
# Name:: Fragile
|
3
|
+
# Author:: tnakamura <http://d.hatena.ne.jp/griefworker>
|
4
|
+
# Created:: Jun 15, 2012
|
5
|
+
# Updated:: Jun 15, 2012
|
6
|
+
# Copyright:: tnakamura Copyright (c) 2012
|
7
|
+
# License:: Licensed under the MIT LICENSE.
|
8
|
+
|
9
|
+
module Fragile
|
10
|
+
class PipelineError < Exception
|
11
|
+
end
|
12
|
+
|
13
|
+
class Pipeline
|
14
|
+
attr_reader :name
|
15
|
+
|
16
|
+
def initialize(name)
|
17
|
+
@name = name
|
18
|
+
@retry_count = 3
|
19
|
+
@plugins = []
|
20
|
+
end
|
21
|
+
|
22
|
+
def retry_count(count)
|
23
|
+
@retry_count = count
|
24
|
+
end
|
25
|
+
|
26
|
+
def use(plugin, config={})
|
27
|
+
@plugins << Fragile.application.create_plugin(plugin, config)
|
28
|
+
end
|
29
|
+
|
30
|
+
def run
|
31
|
+
data = []
|
32
|
+
@plugins.each do |plugin|
|
33
|
+
retry_handler do
|
34
|
+
data = plugin.call(data)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def retry_handler(&block)
|
41
|
+
count = 0
|
42
|
+
begin
|
43
|
+
block.call
|
44
|
+
rescue => ex
|
45
|
+
Fragile.logger.error(ex)
|
46
|
+
if @retry_count < count
|
47
|
+
raise PipelineError.new("retry over error.")
|
48
|
+
end
|
49
|
+
count += 1
|
50
|
+
retry
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -1,36 +1,36 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
# Name:: Fragile
|
3
|
-
# Author:: tnakamura <http://d.hatena.ne.jp/griefworker>
|
4
|
-
# Created:: Jun 15, 2012
|
5
|
-
# Updated:: Jun 15, 2012
|
6
|
-
# Copyright:: tnakamura Copyright (c) 2012
|
7
|
-
# License:: Licensed under the MIT LICENSE.
|
8
|
-
require "fragile/pipeline"
|
9
|
-
|
10
|
-
module Fragile
|
11
|
-
module PipelineManager
|
12
|
-
def pipelines
|
13
|
-
@pipelines ||= {}
|
14
|
-
end
|
15
|
-
|
16
|
-
def define_pipeline(name, &block)
|
17
|
-
p = Pipeline.new(name.to_s)
|
18
|
-
p.instance_eval(&block)
|
19
|
-
self.pipelines[name.to_s] = p
|
20
|
-
end
|
21
|
-
|
22
|
-
def pipeline_exist?(name)
|
23
|
-
self.pipelines.has_key?(name.to_s)
|
24
|
-
end
|
25
|
-
|
26
|
-
def run_pipeline(*names)
|
27
|
-
names.flatten.each do |name|
|
28
|
-
unless pipeline_exist?(name)
|
29
|
-
raise Fragile::PipelineError.new("Pipeline #{name} not found.")
|
30
|
-
end
|
31
|
-
self.pipelines[name.to_s].run
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
# Name:: Fragile
|
3
|
+
# Author:: tnakamura <http://d.hatena.ne.jp/griefworker>
|
4
|
+
# Created:: Jun 15, 2012
|
5
|
+
# Updated:: Jun 15, 2012
|
6
|
+
# Copyright:: tnakamura Copyright (c) 2012
|
7
|
+
# License:: Licensed under the MIT LICENSE.
|
8
|
+
require "fragile/pipeline"
|
9
|
+
|
10
|
+
module Fragile
|
11
|
+
module PipelineManager
|
12
|
+
def pipelines
|
13
|
+
@pipelines ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def define_pipeline(name, &block)
|
17
|
+
p = Pipeline.new(name.to_s)
|
18
|
+
p.instance_eval(&block)
|
19
|
+
self.pipelines[name.to_s] = p
|
20
|
+
end
|
21
|
+
|
22
|
+
def pipeline_exist?(name)
|
23
|
+
self.pipelines.has_key?(name.to_s)
|
24
|
+
end
|
25
|
+
|
26
|
+
def run_pipeline(*names)
|
27
|
+
names.flatten.each do |name|
|
28
|
+
unless pipeline_exist?(name)
|
29
|
+
raise Fragile::PipelineError.new("Pipeline #{name} not found.")
|
30
|
+
end
|
31
|
+
self.pipelines[name.to_s].run
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -1,17 +1,17 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module Fragile
|
4
|
-
module Plugin
|
5
|
-
class ConsoleOutput
|
6
|
-
def initialize(config)
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(data)
|
10
|
-
data.each do |v|
|
11
|
-
puts v
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Fragile
|
4
|
+
module Plugin
|
5
|
+
class ConsoleOutput
|
6
|
+
def initialize(config)
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(data)
|
10
|
+
data.each do |v|
|
11
|
+
puts v
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "net/http"
|
3
|
+
require "digest/sha1"
|
4
|
+
|
5
|
+
module Fragile
|
6
|
+
module Plugin
|
7
|
+
class ImKayacOutput
|
8
|
+
def initialize(config)
|
9
|
+
@base_url = "http://im.kayac.com/api/post/"
|
10
|
+
@username = config[:username]
|
11
|
+
@password = config[:password] || nil
|
12
|
+
@secret_key = config[:secret_key] || nil
|
13
|
+
@handler = config[:handler] || nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(data=[])
|
17
|
+
case
|
18
|
+
when data.empty?
|
19
|
+
# nothing
|
20
|
+
when data.all? {|v| v.kind_of?(Hash) && v.key?(:link) && v.key?(:title)}
|
21
|
+
data.each do |item|
|
22
|
+
send_im_kayac(item[:title], :handler => item[:link])
|
23
|
+
end
|
24
|
+
else
|
25
|
+
message = create_mesage(data)
|
26
|
+
send_im_kayac(message)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def create_mesage(data)
|
32
|
+
data.join("\n")
|
33
|
+
end
|
34
|
+
|
35
|
+
def send_im_kayac(message, options={})
|
36
|
+
uri = URI.parse "#{@base_url}#{@username}"
|
37
|
+
handler = options[:handler] || @handler
|
38
|
+
sig = @secret_key ? \
|
39
|
+
Digest::SHA1.new.update("#{message}#{@secret_key}").to_s : nil
|
40
|
+
|
41
|
+
request = Net::HTTP::Post.new(uri.path)
|
42
|
+
request.set_form_data(
|
43
|
+
:handler => handler,
|
44
|
+
:password => @password,
|
45
|
+
:sig => sig,
|
46
|
+
:message => message,
|
47
|
+
)
|
48
|
+
|
49
|
+
Net::HTTP.new(uri.host, uri.port).start do |http|
|
50
|
+
http.request(request)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -1,50 +1,50 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require "date"
|
3
|
-
require "securerandom"
|
4
|
-
require "net/smtp"
|
5
|
-
|
6
|
-
module Fragile
|
7
|
-
module Plugin
|
8
|
-
class MailOutput
|
9
|
-
def initialize(config)
|
10
|
-
@port = config[:port] || 25
|
11
|
-
@helo_domain = config[:helo_domain] || "localhost"
|
12
|
-
@auth_type = config[:auth_type] || :plain
|
13
|
-
@smtp = config[:smtp]
|
14
|
-
@ssl = config[:ssl] || true
|
15
|
-
@account = config[:account]
|
16
|
-
@password = config[:password]
|
17
|
-
@from = config[:from]
|
18
|
-
@to = config[:to]
|
19
|
-
@subject = config[:subject]
|
20
|
-
end
|
21
|
-
|
22
|
-
def call(data=[])
|
23
|
-
message = create_mesage(data)
|
24
|
-
send_mail(message)
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
def create_mesage(data)
|
29
|
-
message = <<-EOM
|
30
|
-
From: <#{@from}>
|
31
|
-
To: <#{@to}>
|
32
|
-
Subject: #{@subject}
|
33
|
-
Date: #{Date.today}
|
34
|
-
Message-Id: #{SecureRandom.uuid}@localhost
|
35
|
-
|
36
|
-
EOM
|
37
|
-
message += data.join("\n")
|
38
|
-
end
|
39
|
-
|
40
|
-
def send_mail(message)
|
41
|
-
@smtp = Net::SMTP.new(@smtp, @port)
|
42
|
-
@smtp.enable_starttls if @ssl
|
43
|
-
@smtp.start(@helo_domain, @account, @password, @auth_type) do |smtp|
|
44
|
-
smtp.send_message(message, @from, @to)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
require "date"
|
3
|
+
require "securerandom"
|
4
|
+
require "net/smtp"
|
5
|
+
|
6
|
+
module Fragile
|
7
|
+
module Plugin
|
8
|
+
class MailOutput
|
9
|
+
def initialize(config)
|
10
|
+
@port = config[:port] || 25
|
11
|
+
@helo_domain = config[:helo_domain] || "localhost"
|
12
|
+
@auth_type = config[:auth_type] || :plain
|
13
|
+
@smtp = config[:smtp]
|
14
|
+
@ssl = config[:ssl] || true
|
15
|
+
@account = config[:account]
|
16
|
+
@password = config[:password]
|
17
|
+
@from = config[:from]
|
18
|
+
@to = config[:to]
|
19
|
+
@subject = config[:subject]
|
20
|
+
end
|
21
|
+
|
22
|
+
def call(data=[])
|
23
|
+
message = create_mesage(data)
|
24
|
+
send_mail(message)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def create_mesage(data)
|
29
|
+
message = <<-EOM
|
30
|
+
From: <#{@from}>
|
31
|
+
To: <#{@to}>
|
32
|
+
Subject: #{@subject}
|
33
|
+
Date: #{Date.today}
|
34
|
+
Message-Id: #{SecureRandom.uuid}@localhost
|
35
|
+
|
36
|
+
EOM
|
37
|
+
message += data.join("\n")
|
38
|
+
end
|
39
|
+
|
40
|
+
def send_mail(message)
|
41
|
+
@smtp = Net::SMTP.new(@smtp, @port)
|
42
|
+
@smtp.enable_starttls if @ssl
|
43
|
+
@smtp.start(@helo_domain, @account, @password, @auth_type) do |smtp|
|
44
|
+
smtp.send_message(message, @from, @to)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -1,18 +1,18 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module Fragile
|
4
|
-
module Plugin
|
5
|
-
class MapFilter
|
6
|
-
def initialize(config)
|
7
|
-
@proc = config[:proc]
|
8
|
-
end
|
9
|
-
|
10
|
-
def call(data)
|
11
|
-
data.map do |v|
|
12
|
-
@proc.call(v)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Fragile
|
4
|
+
module Plugin
|
5
|
+
class MapFilter
|
6
|
+
def initialize(config)
|
7
|
+
@proc = config[:proc]
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(data)
|
11
|
+
data.map do |v|
|
12
|
+
@proc.call(v)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|