log4r-mail 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/bundler
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ -fs
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in log4r-mail.gemspec
4
+ gemspec
5
+ gem 'rake'
6
+ gem 'rspec'
7
+ gem 'yard'
8
+ gem 'redcarpet'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 aoyagikouhei
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Log4r::Mail
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'log4r-mail'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install log4r-mail
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ require 'yard'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
7
+
8
+ YARD::Rake::YardocTask.new(:doc)
@@ -0,0 +1,67 @@
1
+ #coding: utf-8
2
+ require "log4r/outputter/outputter"
3
+ require "mail"
4
+
5
+ module Log4r
6
+ class MailOutputter < Log4r::Outputter
7
+ VERSION = "0.0.1"
8
+ attr_accessor :host, :port, :from, :to, :subject, :body, :encoding
9
+ def initialize(_name, hash={})
10
+ super(_name, hash)
11
+ @host = hash[:host] || "localhost"
12
+ @port = hash[:port] || 25
13
+ @from = hash[:from] || ""
14
+ @to = hash[:to] || ""
15
+ @subject = hash[:subject] || ""
16
+ @body = hash[:body] || ""
17
+ @encoding = hash[:encoding] || "UTF-8"
18
+ end
19
+
20
+ def write(data)
21
+ send_mail(data.is_a?(Hash) ? data : {body: data})
22
+ end
23
+
24
+ def send_mail(params)
25
+ mail = make_mail(params)
26
+ add_files(mail, params)
27
+ mail.delivery_method :smtp, {
28
+ :enable_starttls_auto => false,
29
+ :address => params[:host] || @host,
30
+ :port => params[:port] || @port,
31
+ }
32
+ mail.deliver
33
+ end
34
+
35
+ def make_mail(params)
36
+ encoding = params[:encoding] || @encoding
37
+ from = params[:from] || @from
38
+ to = params[:to] || @to
39
+ subject = apply_encoding(encoding, params[:subject] || @subject)
40
+ body = apply_encoding(encoding, params[:body] || @body)
41
+
42
+ mail = ::Mail.new do
43
+ from from
44
+ to to
45
+ subject subject
46
+ body body
47
+ end
48
+ mail.charset = encoding
49
+ mail
50
+ end
51
+
52
+ def add_files(mail, params)
53
+ return if !params[:files]
54
+ params[:files].each do |file|
55
+ mail.add_file(file)
56
+ end
57
+ end
58
+
59
+ def apply_encoding(encoding, content)
60
+ if encoding == "ISO-2022-JP"
61
+ NKF.nkf("-j", content).force_encoding("binary")
62
+ else
63
+ content
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'log4r/mail_outputter'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "log4r-mail"
8
+ gem.version = Log4r::MailOutputter::VERSION
9
+ gem.authors = ["aoyagikouhei"]
10
+ gem.email = ["aoyagi.kouhei@gmail.com"]
11
+ gem.description = %q{Log4R Outputter for mail}
12
+ gem.summary = %q{Log4R Outputter for mail}
13
+ gem.homepage = "https://github.com/aoyagikouhei/log4r-mail"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency(%q<log4r>)
20
+ gem.add_dependency(%q<mail>)
21
+ end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+ describe Log4r::MailOutputter do
3
+ it "initialize" do
4
+ obj = Log4r::MailOutputter.new("name")
5
+ obj.host.should == "localhost"
6
+ obj.port.should == 25
7
+ obj.from.should == ""
8
+ obj.to.should == ""
9
+ obj.subject.should == ""
10
+ obj.body.should == ""
11
+ obj.encoding.should == "UTF-8"
12
+ end
13
+
14
+ it "initialize with hash" do
15
+ obj = Log4r::MailOutputter.new("name", {
16
+ host: "192.168.0.1",
17
+ port: 26,
18
+ from: "from@example.com",
19
+ to: "to@example.com",
20
+ subject: "hello",
21
+ body: "world",
22
+ encoding: "ISO-2022-JP",
23
+ })
24
+ obj.host.should == "192.168.0.1"
25
+ obj.port.should == 26
26
+ obj.from.should == "from@example.com"
27
+ obj.to.should == "to@example.com"
28
+ obj.subject.should == "hello"
29
+ obj.body.should == "world"
30
+ obj.encoding.should == "ISO-2022-JP"
31
+ end
32
+
33
+ it "write" do
34
+ obj = Log4r::MailOutputter.new("name", {
35
+ from: "from@example.com",
36
+ to: "to@example.com",
37
+ subject: "hello",
38
+ })
39
+ proc {
40
+ obj.write("hi")
41
+ }.should raise_error(Errno::ECONNREFUSED, /^Connection refused/)
42
+ end
43
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'log4r/mail_outputter'
3
+
4
+ RSpec.configure do |config|
5
+ # some (optional) config here
6
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log4r-mail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - aoyagikouhei
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: log4r
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mail
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Log4R Outputter for mail
47
+ email:
48
+ - aoyagi.kouhei@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/log4r/mail_outputter.rb
60
+ - log4r-mail.gemspec
61
+ - spec/log4r-mail_spec.rb
62
+ - spec/spec_helper.rb
63
+ homepage: https://github.com/aoyagikouhei/log4r-mail
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: 3317884028879765247
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ segments:
85
+ - 0
86
+ hash: 3317884028879765247
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.24
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Log4R Outputter for mail
93
+ test_files:
94
+ - spec/log4r-mail_spec.rb
95
+ - spec/spec_helper.rb
96
+ has_rdoc: