fragile 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,11 +28,17 @@ $ fragile -f recipe/example.rb
28
28
 
29
29
  ##What is Recipe?
30
30
 
31
+ The "Recipe" is ruby script.
32
+
31
33
  ```ruby
34
+ require "fragile/plugin/rss_input"
35
+ require "fragile/plugin/select_filter"
36
+ require "fragile/plugin/console_output"
37
+
32
38
  pipeline "foobar" do
33
- use "feed_input", :url => "http://d.hatena.ne.jp/griefworker/rss"
34
- use "unduplicate_filter"
35
- use "mail_output", :to => "foobar@example.com"
39
+ use "rss_input", :url => "http://d.hatena.ne.jp/griefworker/rss"
40
+ use "select_filter", :proc => lambda{|x| x[:title].include?("[Ruby]")}
41
+ use "console_output"
36
42
  end
37
43
  ```
38
44
 
data/examples/example.rb CHANGED
@@ -1,15 +1,33 @@
1
- pipeline :foobar do
2
- use :foo, :name => "hoge"
3
- use :bar, :suffix => "fuga"
4
- use :bar, :suffix => "moga"
5
- use :hoge
1
+ # coding: utf-8
2
+ require "fragile/plugin/rss_input"
3
+ require "fragile/plugin/select_filter"
4
+ require "fragile/plugin/map_filter"
5
+ require "fragile/plugin/console_output"
6
+ require "fragile/plugin/mail_output"
7
+
8
+ pipeline :console_sample do
9
+ retry_count 4
10
+
11
+ use :rss_input, :url => "http://d.hatena.ne.jp/griefworker/rss"
12
+ use :select_filter, :proc => lambda{|x| x[:title].include?("[Ruby]")}
13
+ use :map_filter, :proc => lambda{|x| x[:title] = x[:title].encode("UTF-8")}
14
+ use :console_output
6
15
  end
7
16
 
8
- pipeline :hogefuga do
9
- retry_count 3
17
+ pipeline :mail_sample do
18
+ retry_count 4
10
19
 
11
- use :foo, :name => "aaa"
12
- use :bar, :suffix => "bbb"
13
- use :hoge
20
+ use :rss_input, :url => "http://d.hatena.ne.jp/griefworker/rss"
21
+ use :select_filter, :proc => lambda{|x| x[:title].include?("[Ruby]")}
22
+ use :mail_output, {
23
+ :port => 587,
24
+ :helo_domain => "gmail.com",
25
+ :auth_type => :login,
26
+ :smtp => "smtp.gmail.com",
27
+ :account => "yourname@gmail.com",
28
+ :password => "password",
29
+ :from => "from@example.com",
30
+ :to => "to@example.com"
31
+ }
14
32
  end
15
33
 
@@ -26,33 +26,28 @@ module Fragile
26
26
 
27
27
  def run
28
28
  handle_options
29
- load_plugins
30
29
  load_recipe_file
31
30
  run_pipeline(ARGV)
32
31
  end
33
32
 
34
33
  def load_recipe_file
35
34
  load @recipe_file
35
+ self.logger.info "#{@recipe_file} was loaded."
36
36
  end
37
37
 
38
38
  def handle_options
39
- opts = OptionParser.new
40
- opts.version = Fragile::VERSION
41
- opts.banner = "fragile [-f RECIPE_FILE] {options} targets..."
42
- opts.separator ""
43
- opts.separator "Options are ..."
44
- opts.on_tail("-h", "--help", "-H", "Display this help message.") do
45
- puts opts
46
- exit
39
+ opts = OptionParser.new do |opts|
40
+ opts.version = Fragile::VERSION
41
+ opts.banner = "fragile [-f RECIPE_FILE] {options} targets..."
42
+ opts.separator ""
43
+ opts.separator "Options are ..."
44
+ opts.on_tail("-h", "--help", "-H", "Display this help message.") do
45
+ puts opts
46
+ exit
47
+ end
48
+ opts.on("-f", "--recipefile=RECIPE_FILE", "Recipe file path"){|v| @recipe_file = v}
49
+ opts.on("-V", "--verbose", "Show detail log"){|v| @logger.level = Logger::DEBUG}
47
50
  end
48
- opts.on("-f", "--recipefile=RECIPE_FILE", "Recipe file path") { |v|
49
- @recipe_file = v
50
- @plugin_dir ||= File.join(File::expand_path(File.dirname(@recipe_file)), "plugins")
51
- }
52
- opts.on("-p", "--plugindir=plugin_dir", "Plugin directory") { |v|
53
- @plugin_dir = File::expand_path(v)
54
- }
55
- opts.on("-V", "--verbose", "Show detail log") { |v| @logger.level = Logger::DEBUG }
56
51
  opts.parse!(ARGV)
57
52
 
58
53
  unless 0 < ARGV.count
@@ -28,7 +28,7 @@ module Fragile
28
28
  end
29
29
 
30
30
  def run
31
- data = nil
31
+ data = []
32
32
  @plugins.each do |plugin|
33
33
  retry_handler do
34
34
  data = plugin.call(data)
@@ -41,7 +41,8 @@ module Fragile
41
41
  count = 0
42
42
  begin
43
43
  block.call
44
- rescue
44
+ rescue => ex
45
+ Fragile.logger.error(ex)
45
46
  if @retry_count < count
46
47
  raise PipelineError.new("retry over error.")
47
48
  end
@@ -1,12 +1,15 @@
1
+ # coding: utf-8
2
+
1
3
  module Fragile
2
4
  module Plugin
3
- class Foo
5
+ class ConsoleOutput
4
6
  def initialize(config)
5
- @name = config[:name]
6
7
  end
7
8
 
8
9
  def call(data)
9
- { :name => @name }
10
+ data.each do |v|
11
+ puts v
12
+ end
10
13
  end
11
14
  end
12
15
  end
@@ -0,0 +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
+
@@ -0,0 +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
+
@@ -0,0 +1,21 @@
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
+
@@ -0,0 +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
+
@@ -13,23 +13,6 @@ module Fragile
13
13
  end
14
14
 
15
15
  module PluginManager
16
- attr_accessor :plugin_dir
17
-
18
- def load_plugins
19
- Fragile.logger.info "plugin_dir is '#{@plugin_dir}'."
20
- unless Dir.exist?(@plugin_dir)
21
- Fragile.logger.warn "'#{@plugin_dir}' is not exist."
22
- return
23
- end
24
-
25
- pattern = File.join(@plugin_dir, "*.rb")
26
- Fragile.logger.info "pattern is '#{pattern}'."
27
- Dir.glob(pattern) do |path|
28
- load path
29
- Fragile.logger.info "'#{path}' is loaded."
30
- end
31
- end
32
-
33
16
  def create_plugin(plugin, config)
34
17
  if plugin.instance_of?(Class)
35
18
  # クラスなら直接 new する
@@ -2,10 +2,10 @@
2
2
  # Name:: Fragile
3
3
  # Author:: tnakamura <http://d.hatena.ne.jp/griefworker>
4
4
  # Created:: Jun 15, 2012
5
- # Updated:: Jun 17, 2012
5
+ # Updated:: Jun 23, 2012
6
6
  # Copyright:: tnakamura Copyright (c) 2012
7
7
  # License:: Licensed under the MIT LICENSE.
8
8
 
9
9
  module Fragile
10
- VERSION = "0.0.2"
10
+ VERSION = "0.0.3"
11
11
  end
data/test/dsl_test.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  # coding: utf-8
2
2
  require_relative "test_helper"
3
- require "minitest/spec"
4
- require "minitest/autorun"
5
- require "fragile"
6
3
 
7
4
  describe "DSL" do
8
5
  describe "#pipeline" do
@@ -1,8 +1,5 @@
1
1
  # coding: utf-8
2
2
  require_relative "test_helper"
3
- require "minitest/spec"
4
- require "minitest/autorun"
5
- require "fragile"
6
3
 
7
4
  class TestApp
8
5
  include Fragile::PipelineManager
@@ -1,8 +1,5 @@
1
1
  # coding: utf-8
2
2
  require_relative "test_helper"
3
- require "minitest/spec"
4
- require "minitest/autorun"
5
- require "fragile"
6
3
 
7
4
  describe "Pipeline" do
8
5
  describe "#name" do
@@ -1,15 +1,8 @@
1
1
  # coding: utf-8
2
2
  require_relative "test_helper"
3
- require "minitest/spec"
4
- require "minitest/autorun"
5
- require "fragile"
6
3
 
7
4
  class TestApp
8
5
  include Fragile::PluginManager
9
-
10
- def initialize(plugin_dir)
11
- @plugin_dir = plugin_dir
12
- end
13
6
  end
14
7
 
15
8
  module Fragile::Plugin
@@ -24,26 +17,10 @@ module Fragile::Plugin
24
17
  end
25
18
 
26
19
  describe "PluginManager" do
27
- describe "#load_plugins" do
28
- describe "プラグインディレクトリが存在するとき" do
29
- before do
30
- @plugin_dir = File.join(File.dirname(__FILE__), "../examples/plugins")
31
- @manager = TestApp.new(@plugin_dir)
32
- end
33
-
34
- it "プラグインをロードできるべき" do
35
- @manager.load_plugins
36
- Fragile::Plugin.const_get("Foo")
37
- pass
38
- end
39
- end
40
- end
41
-
42
20
  describe "#create_plugin" do
43
21
  describe "クラスを指定したとき" do
44
22
  before do
45
- @plugin_dir = File.join(File.dirname(__FILE__), "../examples/plugins")
46
- @manager = TestApp.new(@plugin_dir)
23
+ @manager = TestApp.new
47
24
  end
48
25
 
49
26
  it "プラグインのインスタンスを生成できるべき" do
@@ -54,8 +31,7 @@ describe "PluginManager" do
54
31
 
55
32
  describe "存在しないプラグイン名を指定したとき" do
56
33
  before do
57
- @plugin_dir = File.join(File.dirname(__FILE__), "../examples/plugins")
58
- @manager = TestApp.new(@plugin_dir)
34
+ @manager = TestApp.new
59
35
  end
60
36
 
61
37
  it "NameError が発生するべき" do
@@ -67,28 +43,13 @@ describe "PluginManager" do
67
43
 
68
44
  describe "存在するプラグイン名を指定したとき" do
69
45
  before do
70
- @plugin_dir = File.join(File.dirname(__FILE__), "../examples/plugins")
71
- @manager = TestApp.new(@plugin_dir)
72
- @manager.load_plugins
46
+ @manager = TestApp.new
47
+ require "fragile/plugin/console_output"
73
48
  end
74
49
 
75
50
  it "プラグインのインスタンスを生成できるべき" do
76
- obj = @manager.create_plugin("foo", {})
77
- assert_equal Fragile::Plugin::Foo, obj.class
78
- end
79
- end
80
-
81
- describe "プラグインディレクトリが存在しないとき" do
82
- before do
83
- @plugin_dir = File.join(File.dirname(__FILE__), "not_exist")
84
- @manager = TestApp.new(@plugin_dir)
85
- end
86
-
87
- it "何もしないべき" do
88
- @manager.load_plugins
89
- assert_raises NameError do
90
- Fragile::Plugin.const_get("Foo")
91
- end
51
+ obj = @manager.create_plugin(:console_output, {})
52
+ assert_equal Fragile::Plugin::ConsoleOutput, obj.class
92
53
  end
93
54
  end
94
55
  end
data/test/test_helper.rb CHANGED
@@ -9,3 +9,6 @@ require "logger"
9
9
  require "fragile"
10
10
  Fragile.logger.level = Logger::DEBUG
11
11
 
12
+ require "minitest/spec"
13
+ require "minitest/autorun"
14
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fragile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-17 00:00:00.000000000 Z
12
+ date: 2012-06-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby Pipeline Framework
15
15
  email:
@@ -26,15 +26,17 @@ files:
26
26
  - Rakefile
27
27
  - bin/fragile
28
28
  - examples/example.rb
29
- - examples/plugins/bar.rb
30
- - examples/plugins/foo.rb
31
- - examples/plugins/hoge.rb
32
29
  - fragile.gemspec
33
30
  - lib/fragile.rb
34
31
  - lib/fragile/application.rb
35
32
  - lib/fragile/dsl.rb
36
33
  - lib/fragile/pipeline.rb
37
34
  - lib/fragile/pipeline_manager.rb
35
+ - lib/fragile/plugin/console_output.rb
36
+ - lib/fragile/plugin/mail_output.rb
37
+ - lib/fragile/plugin/map_filter.rb
38
+ - lib/fragile/plugin/rss_input.rb
39
+ - lib/fragile/plugin/select_filter.rb
38
40
  - lib/fragile/plugin_manager.rb
39
41
  - lib/fragile/version.rb
40
42
  - test/dsl_test.rb
@@ -1,14 +0,0 @@
1
- module Fragile
2
- module Plugin
3
- class Bar
4
- def initialize(config)
5
- @suffix = config[:suffix]
6
- end
7
-
8
- def call(data)
9
- data[:name] += @suffix
10
- data
11
- end
12
- end
13
- end
14
- end
@@ -1,13 +0,0 @@
1
- module Fragile
2
- module Plugin
3
- class Hoge
4
- def initialize(config)
5
- end
6
-
7
- def call(data)
8
- puts data[:name]
9
- end
10
- end
11
- end
12
- end
13
-