fragile 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
@@ -1,21 +1,36 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require "rss"
|
3
|
-
|
4
|
-
module Fragile
|
5
|
-
module Plugin
|
6
|
-
class RssInput
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
require "rss"
|
3
|
+
|
4
|
+
module Fragile
|
5
|
+
module Plugin
|
6
|
+
class RssInput
|
7
|
+
module FeedItemNormalizer
|
8
|
+
def title
|
9
|
+
super.content
|
10
|
+
rescue
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
def link
|
15
|
+
super.href
|
16
|
+
rescue
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(config)
|
22
|
+
@url = config[:url]
|
23
|
+
end
|
24
|
+
|
25
|
+
def call(data=[])
|
26
|
+
rss = RSS::Parser.parse(@url)
|
27
|
+
urls = rss.items.map do |item|
|
28
|
+
item.extend FeedItemNormalizer
|
29
|
+
{ :title => item.title, :link => item.link }
|
30
|
+
end
|
31
|
+
data + urls
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -1,18 +1,18 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module Fragile
|
4
|
-
module Plugin
|
5
|
-
class SelectFilter
|
6
|
-
def initialize(config)
|
7
|
-
@proc = config[:proc]
|
8
|
-
end
|
9
|
-
|
10
|
-
def call(data)
|
11
|
-
data.select 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 SelectFilter
|
6
|
+
def initialize(config)
|
7
|
+
@proc = config[:proc]
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(data)
|
11
|
+
data.select do |v|
|
12
|
+
@proc.call(v)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -1,38 +1,38 @@
|
|
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 Plugin; end
|
11
|
-
|
12
|
-
class PluginError < Exception
|
13
|
-
end
|
14
|
-
|
15
|
-
module PluginManager
|
16
|
-
def create_plugin(plugin, config)
|
17
|
-
if plugin.instance_of?(Class)
|
18
|
-
# クラスなら直接 new する
|
19
|
-
plugin.new(config)
|
20
|
-
else
|
21
|
-
# 文字列かシンボルならクラスを取得して new する
|
22
|
-
plugin_name = classify(plugin.to_s)
|
23
|
-
create_plugin_instance(plugin_name, config)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
def create_plugin_instance(class_name, config)
|
29
|
-
klass = Fragile::Plugin.const_get(class_name)
|
30
|
-
klass.new(config)
|
31
|
-
end
|
32
|
-
|
33
|
-
def classify(name)
|
34
|
-
name.split("_").map{ |s| s.capitalize }.join
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
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 Plugin; end
|
11
|
+
|
12
|
+
class PluginError < Exception
|
13
|
+
end
|
14
|
+
|
15
|
+
module PluginManager
|
16
|
+
def create_plugin(plugin, config)
|
17
|
+
if plugin.instance_of?(Class)
|
18
|
+
# クラスなら直接 new する
|
19
|
+
plugin.new(config)
|
20
|
+
else
|
21
|
+
# 文字列かシンボルならクラスを取得して new する
|
22
|
+
plugin_name = classify(plugin.to_s)
|
23
|
+
create_plugin_instance(plugin_name, config)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def create_plugin_instance(class_name, config)
|
29
|
+
klass = Fragile::Plugin.const_get(class_name)
|
30
|
+
klass.new(config)
|
31
|
+
end
|
32
|
+
|
33
|
+
def classify(name)
|
34
|
+
name.split("_").map{ |s| s.capitalize }.join
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
data/lib/fragile/version.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
# Name:: Fragile
|
3
|
-
# Author:: tnakamura <http://d.hatena.ne.jp/griefworker>
|
4
|
-
# Created:: Jun 15, 2012
|
5
|
-
# Updated:: Jun 23, 2012
|
6
|
-
# Copyright:: tnakamura Copyright (c)
|
7
|
-
# License:: Licensed under the MIT LICENSE.
|
8
|
-
|
9
|
-
module Fragile
|
10
|
-
VERSION = "0.0.
|
11
|
-
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 23, 2012
|
6
|
+
# Copyright:: tnakamura Copyright (c) 2013
|
7
|
+
# License:: Licensed under the MIT LICENSE.
|
8
|
+
|
9
|
+
module Fragile
|
10
|
+
VERSION = "0.0.4"
|
11
|
+
end
|
@@ -1,18 +1,17 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require_relative "
|
3
|
-
|
4
|
-
describe
|
5
|
-
describe "#pipeline" do
|
6
|
-
include Fragile::DSL
|
7
|
-
|
8
|
-
after do
|
9
|
-
Fragile.application.pipelines.clear
|
10
|
-
end
|
11
|
-
|
12
|
-
it "パイプラインを登録できるべき" do
|
13
|
-
pipeline "foo" do;end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
require_relative "spec_helper"
|
3
|
+
|
4
|
+
describe Fragile::DSL do
|
5
|
+
describe "#pipeline" do
|
6
|
+
include Fragile::DSL
|
7
|
+
|
8
|
+
after do
|
9
|
+
Fragile.application.pipelines.clear
|
10
|
+
end
|
11
|
+
|
12
|
+
it "パイプラインを登録できるべき" do
|
13
|
+
pipeline "foo" do;end
|
14
|
+
expect(Fragile.application.pipelines.has_key?("foo")).to be_true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,94 +1,94 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require_relative "
|
3
|
-
|
4
|
-
class TestApp
|
5
|
-
include Fragile::PipelineManager
|
6
|
-
end
|
7
|
-
|
8
|
-
class TestPlugin
|
9
|
-
def initialize(config)
|
10
|
-
@config = config
|
11
|
-
end
|
12
|
-
|
13
|
-
def call(data)
|
14
|
-
@config[:called] = true
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe
|
19
|
-
describe "#define_pipeline" do
|
20
|
-
before do
|
21
|
-
@manager = TestApp.new
|
22
|
-
end
|
23
|
-
|
24
|
-
it "パイプラインを登録できるべき" do
|
25
|
-
@manager.define_pipeline :foo do;end
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe "#pipeline_exist?" do
|
31
|
-
describe "存在しないパイプライン名のとき" do
|
32
|
-
before do
|
33
|
-
@manager = TestApp.new
|
34
|
-
end
|
35
|
-
|
36
|
-
it "false を返すべき" do
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "存在するパイプライン名のとき" do
|
42
|
-
before do
|
43
|
-
@manager = TestApp.new
|
44
|
-
@manager.define_pipeline "bar" do;end
|
45
|
-
end
|
46
|
-
|
47
|
-
it "true を返すべき" do
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "#pipelines" do
|
54
|
-
describe "パイプラインが登録されていないとき" do
|
55
|
-
before do
|
56
|
-
@manager = TestApp.new
|
57
|
-
end
|
58
|
-
|
59
|
-
it "空であるべき" do
|
60
|
-
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
describe "#run_pipeline" do
|
66
|
-
describe "登録されていないパイプラインを指定したとき" do
|
67
|
-
before do
|
68
|
-
@manager = TestApp.new
|
69
|
-
end
|
70
|
-
|
71
|
-
it "PipelineError が発生するべき" do
|
72
|
-
|
73
|
-
@manager.run_pipeline("hoge")
|
74
|
-
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
describe "登録されているパイプラインを指定したとき" do
|
79
|
-
before do
|
80
|
-
@config = config = {}
|
81
|
-
@manager = TestApp.new
|
82
|
-
@manager.define_pipeline "hoge" do
|
83
|
-
use TestPlugin, config
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
it "パイプラインを実行できるべき" do
|
88
|
-
@manager.run_pipeline("hoge")
|
89
|
-
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
require_relative "spec_helper"
|
3
|
+
|
4
|
+
class TestApp
|
5
|
+
include Fragile::PipelineManager
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestPlugin
|
9
|
+
def initialize(config)
|
10
|
+
@config = config
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(data)
|
14
|
+
@config[:called] = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe Fragile::PipelineManager do
|
19
|
+
describe "#define_pipeline" do
|
20
|
+
before do
|
21
|
+
@manager = TestApp.new
|
22
|
+
end
|
23
|
+
|
24
|
+
it "パイプラインを登録できるべき" do
|
25
|
+
@manager.define_pipeline :foo do;end
|
26
|
+
expect(@manager.pipelines.has_key?("foo")).to be_true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#pipeline_exist?" do
|
31
|
+
describe "存在しないパイプライン名のとき" do
|
32
|
+
before do
|
33
|
+
@manager = TestApp.new
|
34
|
+
end
|
35
|
+
|
36
|
+
it "false を返すべき" do
|
37
|
+
expect(@manager.pipeline_exist?("foo")).to be_false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "存在するパイプライン名のとき" do
|
42
|
+
before do
|
43
|
+
@manager = TestApp.new
|
44
|
+
@manager.define_pipeline "bar" do;end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "true を返すべき" do
|
48
|
+
expect(@manager.pipeline_exist?("bar")).to be_true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#pipelines" do
|
54
|
+
describe "パイプラインが登録されていないとき" do
|
55
|
+
before do
|
56
|
+
@manager = TestApp.new
|
57
|
+
end
|
58
|
+
|
59
|
+
it "空であるべき" do
|
60
|
+
expect(@manager.pipelines).to be_empty
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#run_pipeline" do
|
66
|
+
describe "登録されていないパイプラインを指定したとき" do
|
67
|
+
before do
|
68
|
+
@manager = TestApp.new
|
69
|
+
end
|
70
|
+
|
71
|
+
it "PipelineError が発生するべき" do
|
72
|
+
expect {
|
73
|
+
@manager.run_pipeline("hoge")
|
74
|
+
}.to raise_error(Fragile::PipelineError)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "登録されているパイプラインを指定したとき" do
|
79
|
+
before do
|
80
|
+
@config = config = {}
|
81
|
+
@manager = TestApp.new
|
82
|
+
@manager.define_pipeline "hoge" do
|
83
|
+
use TestPlugin, config
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it "パイプラインを実行できるべき" do
|
88
|
+
@manager.run_pipeline("hoge")
|
89
|
+
expect(@config[:called]).to be_true
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
@@ -1,106 +1,108 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require_relative "
|
3
|
-
|
4
|
-
describe
|
5
|
-
describe "#name" do
|
6
|
-
before do
|
7
|
-
@pipeline = Fragile::Pipeline.new("test")
|
8
|
-
end
|
9
|
-
|
10
|
-
it "設定した値を取得できるべき" do
|
11
|
-
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "#retry_count" do
|
16
|
-
before do
|
17
|
-
@pipeline = Fragile::Pipeline.new("test")
|
18
|
-
end
|
19
|
-
|
20
|
-
it "値を設定できるべき" do
|
21
|
-
@pipeline.retry_count 5
|
22
|
-
|
23
|
-
actual = nil
|
24
|
-
@pipeline.instance_eval { actual = @retry_count }
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
@
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
@
|
77
|
-
|
78
|
-
@pipeline.
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
@
|
87
|
-
@pipeline.
|
88
|
-
|
89
|
-
@pipeline.
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
@
|
98
|
-
@pipeline.
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
106
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
require_relative "spec_helper"
|
3
|
+
|
4
|
+
describe Fragile::Pipeline do
|
5
|
+
describe "#name" do
|
6
|
+
before do
|
7
|
+
@pipeline = Fragile::Pipeline.new("test")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "設定した値を取得できるべき" do
|
11
|
+
expect(@pipeline.name).to eq("test")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#retry_count" do
|
16
|
+
before do
|
17
|
+
@pipeline = Fragile::Pipeline.new("test")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "値を設定できるべき" do
|
21
|
+
@pipeline.retry_count 5
|
22
|
+
|
23
|
+
actual = nil
|
24
|
+
@pipeline.instance_eval { actual = @retry_count }
|
25
|
+
|
26
|
+
expect(actual).to eq(5)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#use" do
|
31
|
+
class TestPlugin
|
32
|
+
attr_accessor :config
|
33
|
+
|
34
|
+
def initialize(config)
|
35
|
+
@config = config
|
36
|
+
end
|
37
|
+
|
38
|
+
def call(data)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
before do
|
43
|
+
@pipeline = Fragile::Pipeline.new("test")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "プラグインを設定できるべき" do
|
47
|
+
@pipeline.use TestPlugin
|
48
|
+
|
49
|
+
actual_count = nil
|
50
|
+
@pipeline.instance_eval { actual_count = @plugins.count }
|
51
|
+
|
52
|
+
expect(actual_count).to eq(1)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#run" do
|
57
|
+
class TestPlugin
|
58
|
+
attr_accessor :config
|
59
|
+
|
60
|
+
def initialize(config)
|
61
|
+
@config = config
|
62
|
+
@max_error_count = config[:max_error_count] || 0
|
63
|
+
@error_count = 0
|
64
|
+
end
|
65
|
+
|
66
|
+
def call(data)
|
67
|
+
if @error_count < @max_error_count
|
68
|
+
@error_count += 1
|
69
|
+
raise "Error #{@error_count}"
|
70
|
+
end
|
71
|
+
@config[:called] = true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
it "パイプラインを実行できるべき" do
|
76
|
+
@data = {}
|
77
|
+
@pipeline = Fragile::Pipeline.new("test")
|
78
|
+
@pipeline.use TestPlugin, @data
|
79
|
+
|
80
|
+
@pipeline.run
|
81
|
+
|
82
|
+
expect(@data[:called]).to be_true
|
83
|
+
end
|
84
|
+
|
85
|
+
it "@retry_count 回リトライできるべき" do
|
86
|
+
@data = {:max_error_count => 3}
|
87
|
+
@pipeline = Fragile::Pipeline.new("test")
|
88
|
+
@pipeline.use TestPlugin, @data
|
89
|
+
@pipeline.retry_count 2
|
90
|
+
|
91
|
+
@pipeline.run
|
92
|
+
|
93
|
+
expect(@data[:called]).to be_true
|
94
|
+
end
|
95
|
+
|
96
|
+
it "@retry_count 回以上失敗すると PipelineError が発生するべき" do
|
97
|
+
@data = {:max_error_count => 4}
|
98
|
+
@pipeline = Fragile::Pipeline.new("test")
|
99
|
+
@pipeline.use TestPlugin, @data
|
100
|
+
@pipeline.retry_count 2
|
101
|
+
|
102
|
+
expect{
|
103
|
+
@pipeline.run
|
104
|
+
}.to raise_error(Fragile::PipelineError)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|