smtp_url 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -0
- data/Readme.md +68 -0
- data/lib/smtp_url.rb +31 -0
- data/smtp_url.gemspec +19 -0
- data/spec/smtp_url_spec.rb +57 -0
- metadata +68 -0
data/Gemfile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# SmtpURL setup
|
2
|
+
|
3
|
+
This gem allows you to configure your smtp settings for mail and
|
4
|
+
action mailer using a url. The intention is to keep those settings
|
5
|
+
out of your code and allow easily setting them with an environment
|
6
|
+
variable.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Either in your `Gemfile`:
|
11
|
+
|
12
|
+
gem "smtp_url"
|
13
|
+
|
14
|
+
Or install it directly onto your system with rubygems:
|
15
|
+
|
16
|
+
gem install smtp_url
|
17
|
+
|
18
|
+
Then require in your app:
|
19
|
+
|
20
|
+
require "smtp_url"
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Use a URL like so:
|
25
|
+
|
26
|
+
smtp://<user>:<password>@<hostname>:<port>/?<options>
|
27
|
+
|
28
|
+
Options is a query string of key value pairs and is used for setting
|
29
|
+
both the domain and authentication options. For example:
|
30
|
+
|
31
|
+
smtp://user:secret@mailserver:587/?domain=test.com&authentication=digest
|
32
|
+
|
33
|
+
Only `hostname`, is required. Everything else is optional. It will
|
34
|
+
default to port 25 if it is not specified.
|
35
|
+
|
36
|
+
Set the URL in an enviroment variable in your server setup:
|
37
|
+
|
38
|
+
export SMTP_URL=smtp://user:secret@mailserver:587/?domain=test.com
|
39
|
+
|
40
|
+
And then wherever you setup your smtp settings(often config/application.rb) add:
|
41
|
+
|
42
|
+
config.action_mailer.delivery_method = :smtp
|
43
|
+
config.action_mailer.smtp_settings = SmtpURL.parse ENV["SMTP_URL"]
|
44
|
+
|
45
|
+
## License
|
46
|
+
|
47
|
+
SmtpURL is MIT Licensed
|
48
|
+
|
49
|
+
Copyright (C) 2012 by Daniel Farrell
|
50
|
+
|
51
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
52
|
+
of this software and associated documentation files (the "Software"), to deal
|
53
|
+
in the Software without restriction, including without limitation the rights
|
54
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
55
|
+
copies of the Software, and to permit persons to whom the Software is
|
56
|
+
furnished to do so, subject to the following conditions:
|
57
|
+
|
58
|
+
The above copyright notice and this permission notice shall be included in
|
59
|
+
all copies or substantial portions of the Software.
|
60
|
+
|
61
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
62
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
63
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
64
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
65
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
66
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
67
|
+
SOFTWARE.
|
68
|
+
|
data/lib/smtp_url.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'active_support/core_ext/object/try'
|
3
|
+
require 'active_support/core_ext/hash/keys'
|
4
|
+
|
5
|
+
module SmtpURL
|
6
|
+
VERSION = '0.0.2'
|
7
|
+
|
8
|
+
def self.parse(uri)
|
9
|
+
config = URI.parse(uri)
|
10
|
+
return {} if config.scheme != 'smtp'
|
11
|
+
query_params = split_query_params(config.query)
|
12
|
+
settings = { :address => config.host,
|
13
|
+
:port => config.port || 25,
|
14
|
+
:domain => query_params[:domain],
|
15
|
+
:user_name => config.user,
|
16
|
+
:password => config.password,
|
17
|
+
:authentication => query_params[:authentication].try(:to_sym)
|
18
|
+
}
|
19
|
+
settings.reject!{ |key,value| value.nil? }
|
20
|
+
settings
|
21
|
+
rescue URI::InvalidURIError
|
22
|
+
return {}
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def self.split_query_params(query = nil)
|
27
|
+
return {} unless query
|
28
|
+
Hash[query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/smtp_url.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "smtp_url"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "smtp_url"
|
7
|
+
s.version = SmtpURL::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Daniel Farrell"]
|
10
|
+
s.email = ["danielfarrell76@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Convert an smtp url into mail/action mailer friendly hashes}
|
13
|
+
s.description = %q{Allows smtp settings to be defined as URL in environment variable, then converted to hash for use in Mail/ActionMailer}
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.add_dependency('activesupport')
|
19
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'smtp_url'
|
2
|
+
|
3
|
+
describe SmtpURL, '#parse' do
|
4
|
+
it "should set port to 25 is not in url" do
|
5
|
+
url = "smtp://test.com"
|
6
|
+
settings = SmtpURL.parse url
|
7
|
+
settings[:port].should eq(25)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should allow setting port" do
|
11
|
+
url = "smtp://test.com:587"
|
12
|
+
settings = SmtpURL.parse url
|
13
|
+
settings[:port].should eq(587)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should handle urls without authentication" do
|
17
|
+
url = "smtp://test.com"
|
18
|
+
settings = SmtpURL.parse url
|
19
|
+
settings[:address].should eq('test.com')
|
20
|
+
settings[:user_name].should eq(nil)
|
21
|
+
settings[:passsword].should eq(nil)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should handle urls with authentication" do
|
25
|
+
url = "smtp://user:secret@test.com"
|
26
|
+
settings = SmtpURL.parse url
|
27
|
+
settings[:address].should eq('test.com')
|
28
|
+
settings[:user_name].should eq('user')
|
29
|
+
settings[:password].should eq('secret')
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should parse domain from query params" do
|
33
|
+
url = "smtp://test.com/?domain=test2.com"
|
34
|
+
settings = SmtpURL.parse url
|
35
|
+
settings[:domain].should eq('test2.com')
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should parse authentication from query params" do
|
39
|
+
url = "smtp://user:secret@test.com/?authentication=digest"
|
40
|
+
settings = SmtpURL.parse url
|
41
|
+
settings[:user_name].should eq('user')
|
42
|
+
settings[:password].should eq('secret')
|
43
|
+
settings[:authentication].should eq(:digest)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return empty hash for invalid url" do
|
47
|
+
url = "just a string of stuff"
|
48
|
+
settings = SmtpURL.parse url
|
49
|
+
settings.should eq({})
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return empty hash if url is not smtp" do
|
53
|
+
url = "http://test.com"
|
54
|
+
settings = SmtpURL.parse url
|
55
|
+
settings.should eq({})
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smtp_url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Farrell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
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
|
+
description: Allows smtp settings to be defined as URL in environment variable, then
|
31
|
+
converted to hash for use in Mail/ActionMailer
|
32
|
+
email:
|
33
|
+
- danielfarrell76@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- Gemfile
|
39
|
+
- Readme.md
|
40
|
+
- lib/smtp_url.rb
|
41
|
+
- smtp_url.gemspec
|
42
|
+
- spec/smtp_url_spec.rb
|
43
|
+
homepage: ''
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.23
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Convert an smtp url into mail/action mailer friendly hashes
|
67
|
+
test_files:
|
68
|
+
- spec/smtp_url_spec.rb
|