Skip to content

Commit d838d74

Browse files
author
chris
committed
Add url coercer
1 parent af78c73 commit d838d74

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ The following types are supported:
9191
* `:time` (e.g. '14:00')
9292
* `:hash` (e.g. 'a=1&b=2' becomes `{'a' => '1', 'b' => '2'}`)
9393
* `:array` (e.g. 'tag1,tag2' becomes `['tag1', 'tag2']`)
94-
* `:uri` (e.g. 'http://www.google.com' becomes `URI.parse('http://www.google.com')`
94+
* `:uri` (e.g. 'www.google.com' becomes `#<URI::Generic www.google.com>`
95+
* `:uri_with_scheme` (is like :uri, but will raise an exception `if uri.scheme.nil?`)
9596

9697
### Groups
9798

98-
Groups give you more flexibility to define when variables are needed.
99+
Groups give you more flexibility to define when variables are needed.
99100
It's similar to groups in a Gemfile:
100101

101102
```ruby
@@ -147,7 +148,7 @@ variable :FORCE_SSL, :boolean, default: 'false'
147148
variable :PORT, :integer, default: proc {|envied| envied.FORCE_SSL ? 443 : 80 }
148149
```
149150

150-
Please remember that ENVied only **reads** from ENV; it doesn't mutate ENV.
151+
Please remember that ENVied only **reads** from ENV; it doesn't mutate ENV.
151152
Don't let setting a default for, say `RAILS_ENV`, give you the impression that `ENV['RAILS_ENV']` is set.
152153
As a rule of thumb you should only use defaults:
153154
* for local development

lib/envied/coercer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def coerce_method_for(type)
2626

2727
def self.supported_types
2828
@supported_types ||= begin
29-
[:hash, :array, :time, :date, :symbol, :boolean, :integer, :string, :uri, :float].sort
29+
[:hash, :array, :time, :date, :symbol, :boolean, :integer, :string, :uri, :uri_with_scheme, :float].sort
3030
end
3131
end
3232

lib/envied/coercer/envied_string.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ def to_uri(str)
1515
::URI.parse(str)
1616
end
1717

18+
def to_uri_with_scheme(str)
19+
result = to_uri(str)
20+
result.scheme.nil? ? raise_unsupported_coercion(str, __method__) : result
21+
end
22+
1823
def to_integer(str)
1924
Integer(str)
2025
rescue ArgumentError

spec/coercer_spec.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,34 @@ def coerce_to(type)
114114
describe 'to uri' do
115115
let(:coerce){ coerce_to(:uri) }
116116

117-
it 'converts strings to uris' do
118-
expect(coerce['http://www.google.com']).to be_a(URI)
117+
it 'converts strings to generic uris' do
118+
expect(coerce['3']).to be_a(URI::Generic)
119+
end
120+
121+
it 'converts strings to ftp uris' do
122+
expect(coerce['ftp://ftp.example.com']).to be_a(URI::FTP)
123+
end
124+
end
125+
126+
describe 'to uri_with_scheme' do
127+
let(:coerce){ coerce_to(:uri_with_scheme) }
128+
129+
it 'converts strings to http uris' do
130+
expect(coerce['http://www.google.com'].scheme).to eql 'http'
131+
end
132+
133+
it 'converts strings to https uris' do
134+
expect(coerce['https://github.com'].scheme).to eql 'https'
135+
end
136+
137+
it 'converts strings to redis uris' do
138+
expect(coerce['redis://example.com:6379'].scheme).to eql 'redis'
139+
end
140+
141+
it 'fails for non urls' do
142+
expect {
143+
coerce['3']
144+
}.to raise_error(Coercible::UnsupportedCoercion)
119145
end
120146
end
121147
end

0 commit comments

Comments
 (0)