smtp_url 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.3)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ diff-lcs (1.1.3)
8
+ i18n (0.6.0)
9
+ multi_json (1.3.5)
10
+ rspec (2.10.0)
11
+ rspec-core (~> 2.10.0)
12
+ rspec-expectations (~> 2.10.0)
13
+ rspec-mocks (~> 2.10.0)
14
+ rspec-core (2.10.0)
15
+ rspec-expectations (2.10.0)
16
+ diff-lcs (~> 1.1.3)
17
+ rspec-mocks (2.10.1)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ activesupport
24
+ rspec
data/Readme.md CHANGED
@@ -7,18 +7,10 @@ variable.
7
7
 
8
8
  ## Installation
9
9
 
10
- Either in your `Gemfile`:
10
+ Add to your `Gemfile`:
11
11
 
12
12
  gem "smtp_url"
13
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
14
  ## Usage
23
15
 
24
16
  Use a URL like so:
@@ -37,10 +29,13 @@ Set the URL in an enviroment variable in your server setup:
37
29
 
38
30
  export SMTP_URL=smtp://user:secret@mailserver:587/?domain=test.com
39
31
 
40
- And then wherever you setup your smtp settings(often config/application.rb) add:
32
+ ## Development
33
+
34
+ Can be very useful in development when paired with something like
35
+ [Mailcatcher](http://mailcatcher.me/):
36
+
37
+ export SMTP_URL=smtp://127.0.0.1:1025
41
38
 
42
- config.action_mailer.delivery_method = :smtp
43
- config.action_mailer.smtp_settings = SmtpURL.parse ENV["SMTP_URL"]
44
39
 
45
40
  ## License
46
41
 
@@ -0,0 +1,3 @@
1
+ module SmtpURL
2
+ class InvalidUrlException < Exception; end
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'uri'
2
+ require 'active_support/core_ext/object/try'
3
+ require 'active_support/core_ext/hash/keys'
4
+
5
+ module SmtpURL
6
+ class Parser
7
+
8
+ def initialize(url)
9
+ @url = url
10
+ end
11
+
12
+ def parse
13
+ config = parse_url
14
+ query = split_query_params(config.query)
15
+ settings = build_hash(config, query)
16
+ settings.reject!{ |key,value| value.nil? }
17
+ settings
18
+ end
19
+
20
+ private
21
+
22
+ def parse_url
23
+ parsed_url = URI.parse(@url)
24
+ raise InvalidUrlException, "Improper format of SMTP_URL env var, must be smtp://" unless parsed_url.scheme == 'smtp'
25
+ parsed_url
26
+ rescue URI::InvalidURIError => e
27
+ raise InvalidUrlException, "Could not parse SMTP_URL env var"
28
+ end
29
+
30
+ def build_hash(config, query)
31
+ {
32
+ :address => config.host,
33
+ :port => config.port || 25,
34
+ :domain => query[:domain],
35
+ :user_name => config.user,
36
+ :password => config.password,
37
+ :authentication => query[:authentication].try(:to_sym)
38
+ }
39
+ end
40
+
41
+ def split_query_params(query = nil)
42
+ return {} unless query
43
+ Hash[query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ module SmtpURL
2
+ class Railtie < Rails::Railtie
3
+ config.action_mailer.delivery_method = :smtp
4
+ config.action_mailer.smtp_settings = SmtpURL::Parser.new(ENV["SMTP_URL"]).parse
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module SmtpURL
2
+ VERSION = '0.1.0'
3
+ end
data/lib/smtp_url.rb CHANGED
@@ -1,31 +1,4 @@
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
1
+ require 'smtp_url/version'
2
+ require 'smtp_url/invalid_url_exception'
3
+ require 'smtp_url/parser'
4
+ require 'smtp_url/railtie' if defined?(Rails)
data/smtp_url.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Daniel Farrell"]
10
10
  s.email = ["danielfarrell76@gmail.com"]
11
- s.homepage = ""
11
+ s.homepage = "https://github.com/danielfarrell/smtp_url"
12
12
  s.summary = %q{Convert an smtp url into mail/action mailer friendly hashes}
13
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
14
  s.files = `git ls-files`.split("\n")
@@ -1,21 +1,22 @@
1
- require 'smtp_url'
1
+ require 'smtp_url/invalid_url_exception'
2
+ require 'smtp_url/parser'
2
3
 
3
- describe SmtpURL, '#parse' do
4
+ describe SmtpURL::Parser, '#parse' do
4
5
  it "should set port to 25 is not in url" do
5
6
  url = "smtp://test.com"
6
- settings = SmtpURL.parse url
7
+ settings = SmtpURL::Parser.new(url).parse
7
8
  settings[:port].should eq(25)
8
9
  end
9
10
 
10
11
  it "should allow setting port" do
11
12
  url = "smtp://test.com:587"
12
- settings = SmtpURL.parse url
13
+ settings = SmtpURL::Parser.new(url).parse
13
14
  settings[:port].should eq(587)
14
15
  end
15
16
 
16
17
  it "should handle urls without authentication" do
17
18
  url = "smtp://test.com"
18
- settings = SmtpURL.parse url
19
+ settings = SmtpURL::Parser.new(url).parse
19
20
  settings[:address].should eq('test.com')
20
21
  settings[:user_name].should eq(nil)
21
22
  settings[:passsword].should eq(nil)
@@ -23,7 +24,7 @@ describe SmtpURL, '#parse' do
23
24
 
24
25
  it "should handle urls with authentication" do
25
26
  url = "smtp://user:secret@test.com"
26
- settings = SmtpURL.parse url
27
+ settings = SmtpURL::Parser.new(url).parse
27
28
  settings[:address].should eq('test.com')
28
29
  settings[:user_name].should eq('user')
29
30
  settings[:password].should eq('secret')
@@ -31,27 +32,25 @@ describe SmtpURL, '#parse' do
31
32
 
32
33
  it "should parse domain from query params" do
33
34
  url = "smtp://test.com/?domain=test2.com"
34
- settings = SmtpURL.parse url
35
+ settings = SmtpURL::Parser.new(url).parse
35
36
  settings[:domain].should eq('test2.com')
36
37
  end
37
38
 
38
39
  it "should parse authentication from query params" do
39
40
  url = "smtp://user:secret@test.com/?authentication=digest"
40
- settings = SmtpURL.parse url
41
+ settings = SmtpURL::Parser.new(url).parse
41
42
  settings[:user_name].should eq('user')
42
43
  settings[:password].should eq('secret')
43
44
  settings[:authentication].should eq(:digest)
44
45
  end
45
46
 
46
- it "should return empty hash for invalid url" do
47
+ it "should raise InvalidUriException for invalid url" do
47
48
  url = "just a string of stuff"
48
- settings = SmtpURL.parse url
49
- settings.should eq({})
49
+ lambda {SmtpURL::Parser.new(url).parse}.should raise_error(SmtpURL::InvalidUrlException, "Could not parse SMTP_URL env var")
50
50
  end
51
51
 
52
- it "should return empty hash if url is not smtp" do
52
+ it "should raise InvalidUriException if url is not smtp" do
53
53
  url = "http://test.com"
54
- settings = SmtpURL.parse url
55
- settings.should eq({})
54
+ lambda {SmtpURL::Parser.new(url).parse}.should raise_error(SmtpURL::InvalidUrlException, "Improper format of SMTP_URL env var, must be smtp://")
56
55
  end
57
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smtp_url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-17 00:00:00.000000000 Z
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -36,11 +36,16 @@ extensions: []
36
36
  extra_rdoc_files: []
37
37
  files:
38
38
  - Gemfile
39
+ - Gemfile.lock
39
40
  - Readme.md
40
41
  - lib/smtp_url.rb
42
+ - lib/smtp_url/invalid_url_exception.rb
43
+ - lib/smtp_url/parser.rb
44
+ - lib/smtp_url/railtie.rb
45
+ - lib/smtp_url/version.rb
41
46
  - smtp_url.gemspec
42
- - spec/smtp_url_spec.rb
43
- homepage: ''
47
+ - spec/smtp_url/parser_spec.rb
48
+ homepage: https://github.com/danielfarrell/smtp_url
44
49
  licenses: []
45
50
  post_install_message:
46
51
  rdoc_options: []
@@ -65,4 +70,4 @@ signing_key:
65
70
  specification_version: 3
66
71
  summary: Convert an smtp url into mail/action mailer friendly hashes
67
72
  test_files:
68
- - spec/smtp_url_spec.rb
73
+ - spec/smtp_url/parser_spec.rb