fragile 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,36 @@
1
- # coding: utf-8
2
- require "rss"
3
-
4
- module Fragile
5
- module Plugin
6
- class RssInput
7
- def initialize(config)
8
- @url = config[:url]
9
- end
10
-
11
- def call(data=[])
12
- rss = RSS::Parser.parse(@url)
13
- urls = rss.items.map do |item|
14
- { :title => item.title, :link => item.link }
15
- end
16
- data + urls
17
- end
18
- end
19
- end
20
- end
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
+
@@ -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) 2012
7
- # License:: Licensed under the MIT LICENSE.
8
-
9
- module Fragile
10
- VERSION = "0.0.3"
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 "test_helper"
3
-
4
- describe "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
- assert_equal true, Fragile.application.pipelines.has_key?("foo")
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 "test_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 "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
- assert_equal true, @manager.pipelines.has_key?("foo")
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
- assert_equal false, @manager.pipeline_exist?("foo")
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
- assert_equal true, @manager.pipeline_exist?("bar")
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
- assert_equal true, @manager.pipelines.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
- assert_raises Fragile::PipelineError do
73
- @manager.run_pipeline("hoge")
74
- end
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
- assert_equal true, @config[:called]
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 "test_helper"
3
-
4
- describe "Pipeline" do
5
- describe "#name" do
6
- before do
7
- @pipeline = Fragile::Pipeline.new("test")
8
- end
9
-
10
- it "設定した値を取得できるべき" do
11
- assert_equal "test", @pipeline.name
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
- assert_equal 5, actual
26
- end
27
- end
28
-
29
- describe "#use" do
30
- class TestPlugin
31
- attr_accessor :config
32
-
33
- def initialize(config)
34
- @config = config
35
- end
36
-
37
- def call(data)
38
- end
39
- end
40
-
41
- before do
42
- @pipeline = Fragile::Pipeline.new("test")
43
- end
44
-
45
- it "プラグインを設定できるべき" do
46
- @pipeline.use TestPlugin
47
-
48
- actual_count = nil
49
- @pipeline.instance_eval { actual_count = @plugins.count }
50
- assert_equal 1, actual_count
51
- end
52
- end
53
-
54
- describe "#run" do
55
- class TestPlugin
56
- attr_accessor :config
57
-
58
- def initialize(config)
59
- @config = config
60
- @max_error_count = config[:max_error_count] || 0
61
- @error_count = 0
62
- end
63
-
64
- def call(data)
65
- if @error_count < @max_error_count
66
- @error_count += 1
67
- raise "Error #{@error_count}"
68
- end
69
- @config[:called] = true
70
- end
71
- end
72
-
73
- it "パイプラインを実行できるべき" do
74
- @data = {}
75
- @pipeline = Fragile::Pipeline.new("test")
76
- @pipeline.use TestPlugin, @data
77
-
78
- @pipeline.run
79
-
80
- assert_equal true, @data[:called]
81
- end
82
-
83
- it "@retry_count 回リトライできるべき" do
84
- @data = {:max_error_count => 3}
85
- @pipeline = Fragile::Pipeline.new("test")
86
- @pipeline.use TestPlugin, @data
87
- @pipeline.retry_count 2
88
-
89
- @pipeline.run
90
-
91
- assert_equal true, @data[:called]
92
- end
93
-
94
- it "@retry_count 回以上失敗すると PipelineError が発生するべき" do
95
- @data = {:max_error_count => 4}
96
- @pipeline = Fragile::Pipeline.new("test")
97
- @pipeline.use TestPlugin, @data
98
- @pipeline.retry_count 2
99
-
100
- assert_raises Fragile::PipelineError do
101
- @pipeline.run
102
- end
103
- end
104
- end
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
+