multi_notifier 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 242f1682aeef6370a9462a404e0958e00fbf675d
4
+ data.tar.gz: 6d89c616166db72999c83ee68c364281a59078ff
5
+ SHA512:
6
+ metadata.gz: 458a7da9a429f0751b103e3a6f100d5d060132576609ccf4465e4a68e2b29753daf0d5b9b53db56f5b26d34d7f6482518ae0eaf4e4ae19de091178e4dad62c94
7
+ data.tar.gz: 9b12e09f5ddbfdd266940cda7d2423ad64098eaec4de5c37551744e992d07a0879a44b385f3ba25254bd452c96e773ad2d55f0324de7a581cf273cfd2f56c8a9
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi_notifier.gemspec
4
+ gemspec
5
+
6
+ gem "rake"
7
+
8
+ group :test do
9
+ gem "rspec"
10
+ gem "actionmailer"
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jingwen Owen Ou
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
+ # MultiNotifier
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'multi_notifier'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install multi_notifier
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,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ desc "Run specs"
7
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ require "active_model"
2
+ require_relative "model" unless defined?(ActiveModel::Model)
3
+
4
+ module MultiNotifier
5
+ class Adapter
6
+ include ActiveModel::Model
7
+
8
+ def notify
9
+ raise "Implement #{self.class.name}#notify"
10
+ end
11
+
12
+ def notify!
13
+ raise error_msg(errors.messages) unless valid?
14
+
15
+ notify
16
+ end
17
+
18
+ private
19
+
20
+ def error_msg(messages)
21
+ messages.map do |key, value|
22
+ value.map { |v| "#{key} #{v}" }
23
+ end.flatten.join("\n")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,46 @@
1
+ require "action_mailer" unless defined?(::ActionMailer)
2
+ require "multi_notifier/adapter"
3
+
4
+ module MultiNotifier
5
+ module Adapters
6
+ class Mail < Adapter
7
+ class DeliveryValidator < ::ActiveModel::Validator
8
+ def validate(model)
9
+ if model.delivery.present?
10
+ model.errors.add(:delivery, "method can't be blank") unless model.delivery[:method].present?
11
+ end
12
+ end
13
+ end
14
+
15
+ attr_accessor :delivery, :from, :to, :subject, :html_body, :text_body
16
+
17
+ validates_presence_of :delivery, :from, :to, :subject, :text_body
18
+ validates_with DeliveryValidator
19
+
20
+ class Mailer < ActionMailer::Base
21
+ def notificaiton(headers, text_body, html_body)
22
+ mail headers do |format|
23
+ format.text { text_body }
24
+ format.html { html_body.present? ? html_body : text_body }
25
+ end
26
+ end
27
+ end
28
+
29
+ def notify
30
+ Mailer.delivery_method = delivery[:method]
31
+ Mailer.send("#{delivery[:method]}_settings=", delivery[:settings] || {})
32
+
33
+ headers = {
34
+ :from => from,
35
+ :to => to,
36
+ :subject => subject
37
+ }
38
+
39
+ mailer = Mailer.notificaiton(headers, text_body, html_body)
40
+ mailer.deliver
41
+
42
+ mailer
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,29 @@
1
+ require "active_support"
2
+
3
+ module MultiNotifier
4
+ class Builder
5
+ attr_reader :adapters
6
+
7
+ def initialize
8
+ @adapters = []
9
+ end
10
+
11
+ def adapter(type, configs = {})
12
+ require "multi_notifier/adapters/#{type}"
13
+ adapter = "MultiNotifier::Adapters::#{type.to_s.classify}".constantize
14
+ adapters << adapter.new(configs)
15
+
16
+ adapter
17
+ end
18
+
19
+ def notify_all!
20
+ adapters.each do |adapter|
21
+ begin
22
+ adapter.notify!
23
+ rescue => e
24
+ puts "#{adapter.class.name} has error #{e.inspect}"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # Copy ActiveModel::Model from Rails 4.x here
2
+ module MultiNotifier
3
+ class Adapter
4
+ module ActiveModel
5
+ module Model
6
+ def self.included(base)
7
+ base.class_eval do
8
+ extend ::ActiveModel::Naming
9
+ extend ::ActiveModel::Translation
10
+ include ::ActiveModel::Validations
11
+ include ::ActiveModel::Conversion
12
+ end
13
+ end
14
+
15
+ def initialize(params={})
16
+ params.each do |attr, value|
17
+ self.public_send("#{attr}=", value)
18
+ end if params
19
+
20
+ super()
21
+ end
22
+
23
+ def persisted?
24
+ false
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module MultiNotifier
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "multi_notifier/version"
2
+ require "multi_notifier/builder"
3
+
4
+ module MultiNotifier
5
+ def self.build
6
+ builder = Builder.new
7
+ yield builder if block_given?
8
+
9
+ builder
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_notifier/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multi_notifier"
8
+ spec.version = MultiNotifier::VERSION
9
+ spec.authors = ["Jingwen Owen Ou"]
10
+ spec.email = ["jingweno@gmail.com"]
11
+ spec.description = %q{Simple and flexbile library for sending notifications}
12
+ spec.summary = %q{Simple and flexbile library for sending notifications}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activemodel", ">= 3.0.0"
22
+ end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+ require "multi_notifier/adapters/mail"
3
+
4
+ describe MultiNotifier::Adapters::Mail do
5
+ context "validations" do
6
+ [:delivery, :from, :to, :subject, :text_body].each do |field|
7
+ it "#{field} can't be blank" do
8
+ instance = described_class.new
9
+ instance.should be_invalid
10
+ instance.errors[field].should include("can't be blank")
11
+ end
12
+ end
13
+
14
+ it "delivery method can't be blank" do
15
+ instance = described_class.new(:delivery => {
16
+ :foo => :bar
17
+ })
18
+ instance.should be_invalid
19
+ instance.errors[:delivery].should include("method can't be blank")
20
+ end
21
+ end
22
+
23
+ context "notify" do
24
+ it "sends email" do
25
+ mail = MultiNotifier::Adapters::Mail.new
26
+ mail.delivery = {
27
+ :method => :test
28
+ }
29
+ mail.from = "from@owenou.com"
30
+ mail.to = "to@owenou.com"
31
+ mail.subject = "subject"
32
+ mail.text_body = "text body"
33
+ mail.html_body = "html body"
34
+
35
+ mailer = mail.notify!
36
+ mailer.from.should == [mail.from]
37
+ mailer.to.should == [mail.to]
38
+ mailer.subject.should == mail.subject
39
+ mailer.body.encoded.should include(mail.text_body)
40
+ mailer.body.encoded.should include(mail.html_body)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ describe MultiNotifier::Builder do
4
+ it "builds adapter by types" do
5
+ builder = described_class.new
6
+ builder.adapter :mail
7
+
8
+ builder.adapters.size.should == 1
9
+ adapter = builder.adapters.first
10
+ adapter.class.name.should == "MultiNotifier::Adapters::Mail"
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift File.expand_path("..", __FILE__)
2
+ $:.unshift File.expand_path("../../lib", __FILE__)
3
+
4
+ require "rspec"
5
+ require "multi_notifier"
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jingwen Owen Ou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Simple and flexbile library for sending notifications
28
+ email:
29
+ - jingweno@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/multi_notifier.rb
40
+ - lib/multi_notifier/adapter.rb
41
+ - lib/multi_notifier/adapters/mail.rb
42
+ - lib/multi_notifier/builder.rb
43
+ - lib/multi_notifier/model.rb
44
+ - lib/multi_notifier/version.rb
45
+ - multi_notifier.gemspec
46
+ - spec/multi_notifier/adapters/mail_spec.rb
47
+ - spec/multi_notifier/builder_spec.rb
48
+ - spec/spec_helper.rb
49
+ homepage: ''
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.0.2
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Simple and flexbile library for sending notifications
73
+ test_files:
74
+ - spec/multi_notifier/adapters/mail_spec.rb
75
+ - spec/multi_notifier/builder_spec.rb
76
+ - spec/spec_helper.rb