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.
@@ -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
+
@@ -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
+