us-mail-client 0.0.5.alpha

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: 9e0fae14914ceebf9b75da83f0fa08cf67b673be
4
+ data.tar.gz: 8e158297caf093362ac70d88dc88ade81de26b24
5
+ SHA512:
6
+ metadata.gz: 84c21882d2c15ec77a532e3ef714403cbf820118cfc1e798676a23545bfd887d1ec76e689e381e43547ec79a398c11798138a1e02c6fa78e157fa7a951d33ea2
7
+ data.tar.gz: 2ed60b6122cbbeb739312236056aca597b99bcb13b2415f5aef0114f25c3699c76e1b6c14d3829abfff5e0b147826a3d857ca0882c0bbc290b78cf28672e885b
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ Union Station Metrics Models
2
+ ============================
3
+
4
+ This gem contains the mail client of the Union Station project.
5
+
6
+ These clients are in a gem so they can be shared across the Union Station applications.
7
+
8
+ Usage
9
+ ============================
10
+
11
+ require 'union_station/mail_client'
@@ -0,0 +1,18 @@
1
+ default_settings: &default_settings
2
+ email:
3
+ mailer_host: http://mailer:8205
4
+
5
+ development:
6
+ <<: *default_settings
7
+ email:
8
+ delivery_method: test
9
+ mailer_host: http://mailer:8205
10
+
11
+ test:
12
+ <<: *default_settings
13
+ email:
14
+ delivery_method: test
15
+ mailer_host: http://mailer:8205
16
+
17
+ production:
18
+ <<: *default_settings
@@ -0,0 +1,22 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'active_support/core_ext/object/to_query'
4
+
5
+ module UnionStation
6
+ class DefaultMailer
7
+ attr_accessor :uri
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ @uri = URI.parse(@config[:mailer_host]+'/mail')
12
+ end
13
+
14
+ def deliver(message)
15
+ http = Net::HTTP.new(@uri.host, @uri.port)
16
+ req = Net::HTTP::Post.new(@uri.request_uri)
17
+ req.content_type = "application/x-www-form-urlencoded"
18
+ req.body = message.to_hash.to_query
19
+ response = http.request(req)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ module UnionStation
2
+ class TestMailer
3
+ def initialize(config)
4
+ end
5
+
6
+ def TestMailer.deliveries
7
+ @@deliveries ||= []
8
+ end
9
+
10
+ def TestMailer.deliveries=(val)
11
+ @@deliveries = val
12
+ end
13
+
14
+ def deliver(message)
15
+ UnionStation::TestMailer.deliveries << message.to_hash
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,50 @@
1
+ module UnionStation
2
+ module Mail
3
+ module Fields
4
+ class RecipientsField
5
+ include Enumerable
6
+
7
+ attr_reader :values
8
+
9
+ def initialize(*values)
10
+ if values.size == 1
11
+ if values[0].is_a? Array
12
+ @values = values[0]
13
+ else
14
+ @values = values[0].split(/,/)
15
+ end
16
+ else
17
+ @values = values
18
+ end
19
+ cleanup
20
+ self
21
+ end
22
+
23
+ def <<(val)
24
+ if val.is_a? Array
25
+ @values += val
26
+ else
27
+ @values += val.split(/,/)
28
+ end
29
+ cleanup
30
+ self
31
+ end
32
+
33
+ def ==(other)
34
+ @values == other
35
+ end
36
+
37
+ def to_s
38
+ @values.join ', '
39
+ end
40
+
41
+ private
42
+
43
+ def cleanup
44
+ @values.map!(&:strip) #trim whitespace
45
+ @values.uniq!
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,21 @@
1
+ module UnionStation
2
+ module Mail
3
+ module Fields
4
+ class StringField
5
+ attr_reader :value
6
+
7
+ def initialize(value = '')
8
+ @value = value
9
+ end
10
+
11
+ def ==(other)
12
+ @value == other
13
+ end
14
+
15
+ def <<(val)
16
+ @value << val
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,136 @@
1
+ Dir[File.dirname(__FILE__) + '/fields/*.rb'].each { |file| require file }
2
+
3
+ module UnionStation
4
+ module Mail
5
+ class Message
6
+ FIELDS = [ :from, :to, :bcc, :reply_to, :subject, :body ]
7
+
8
+ attr_reader :header
9
+
10
+ def initialize(*args, &block)
11
+ init
12
+ if args && args[0].respond_to?(:each_pair)
13
+ init_with_hash(args[0])
14
+ end
15
+
16
+ if block_given?
17
+ instance_eval(&block)
18
+ end
19
+ self
20
+ end
21
+
22
+ def init
23
+ @header = {}
24
+ header[:from] = Fields::StringField.new
25
+ header[:to] = Fields::RecipientsField.new
26
+ header[:bcc] = Fields::RecipientsField.new
27
+ header[:subject] = Fields::StringField.new
28
+ header[:reply_to] = Fields::StringField.new
29
+ @body = {text: nil, html: nil}
30
+ end
31
+
32
+ def init_with_hash(args)
33
+ args.each_pair do |key, value|
34
+ if FIELDS.include?(key)
35
+ self.send(key, value)
36
+ end
37
+ end
38
+ end
39
+
40
+ def from(val = nil)
41
+ if val
42
+ header[:from] = Fields::StringField.new val
43
+ else
44
+ header[:from]
45
+ end
46
+ end
47
+
48
+ def from=(val)
49
+ header[:from] = Fields::StringField.new val
50
+ end
51
+
52
+ def to(val = nil)
53
+ if val
54
+ header[:to] = Fields::RecipientsField.new val
55
+ else
56
+ header[:to]
57
+ end
58
+ end
59
+
60
+ def to=(val)
61
+ header[:to] = Fields::RecipientsField.new val
62
+ end
63
+
64
+ def bcc(val = nil)
65
+ if val
66
+ header[:bcc] = Fields::RecipientsField.new val
67
+ else
68
+ header[:bcc]
69
+ end
70
+ end
71
+
72
+ def bcc=(val)
73
+ header[:bcc] = Fields::RecipientsField.new val
74
+ end
75
+
76
+ def subject(val = nil)
77
+ if val
78
+ header[:subject] = Fields::StringField.new val
79
+ else
80
+ header[:subject]
81
+ end
82
+ end
83
+
84
+ def subject=(val)
85
+ header[:subject] = Fields::StringField.new val
86
+ end
87
+
88
+ def reply_to(val = nil)
89
+ if val
90
+ header[:reply_to] = Fields::StringField.new val
91
+ else
92
+ header[:reply_to]
93
+ end
94
+ end
95
+
96
+ def reply_to=(val)
97
+ header[:reply_to] = Fields::StringField.new val
98
+ end
99
+
100
+ def body(val = nil, type = :all)
101
+ if type == :all
102
+ if val
103
+ @body = val
104
+ else
105
+ @body
106
+ end
107
+ else
108
+ if val
109
+ @body[type] = val
110
+ else
111
+ @body[type]
112
+ end
113
+ end
114
+ end
115
+
116
+ def body=(val, type = :all)
117
+ if type == :all
118
+ @body = val
119
+ else
120
+ @body[type] = val
121
+ end
122
+ end
123
+
124
+ def to_hash
125
+ {
126
+ from: from.value,
127
+ to: to.values,
128
+ bcc: bcc.values,
129
+ subject: subject.value,
130
+ body: @body,
131
+ reply_to: reply_to.value
132
+ }
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_support/core_ext/hash/indifferent_access'
2
+ require 'union_station/mail/message'
3
+ Dir[File.dirname(__FILE__) + '/clients/*.rb'].each { |file| require file }
4
+
5
+ module UnionStation
6
+ module MailClient
7
+ class << self
8
+ def new(config)
9
+ config = config.with_indifferent_access
10
+ if config[:delivery_method].to_s == "test"
11
+ TestMailer.new(config)
12
+ else
13
+ DefaultMailer.new(config)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ module UnionStation
2
+ module MailClient
3
+ VERSION = "0.0.5.alpha"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ # Maintain your gem's version:
5
+ require 'version'
6
+
7
+ # Describe your gem and declare its dependencies:
8
+ Gem::Specification.new do |s|
9
+ s.name = 'us-mail-client'
10
+ s.version = UnionStation::MailClient::VERSION
11
+ s.authors = ['Phusion']
12
+ s.email = ['support@unionstationapp.com']
13
+ s.homepage = 'https://www.unionstationapp.com/'
14
+ s.summary = 'Mail client of the Union Station system.'
15
+ s.description = 'Mail client of the Union Station system.'
16
+
17
+ s.files = Dir["{config,lib}/**/*"] + ['us-mail-client.gemspec','README.md']
18
+
19
+ s.add_runtime_dependency 'activesupport', '~> 4.2'
20
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: us-mail-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Phusion
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ description: Mail client of the Union Station system.
28
+ email:
29
+ - support@unionstationapp.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - config/settings.yml.example
36
+ - lib/union_station/clients/default_mailer.rb
37
+ - lib/union_station/clients/test_mailer.rb
38
+ - lib/union_station/mail/fields/recipients_field.rb
39
+ - lib/union_station/mail/fields/string_field.rb
40
+ - lib/union_station/mail/message.rb
41
+ - lib/union_station/mail_client.rb
42
+ - lib/version.rb
43
+ - us-mail-client.gemspec
44
+ homepage: https://www.unionstationapp.com/
45
+ licenses: []
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">"
59
+ - !ruby/object:Gem::Version
60
+ version: 1.3.1
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.2.3
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Mail client of the Union Station system.
67
+ test_files: []
68
+ has_rdoc: