Skip to content

Commit

Permalink
Add url coercer
Browse files Browse the repository at this point in the history
  • Loading branch information
chris committed Mar 26, 2018
1 parent af78c73 commit b102161
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ The following types are supported:
* `:time` (e.g. '14:00')
* `:hash` (e.g. 'a=1&b=2' becomes `{'a' => '1', 'b' => '2'}`)
* `:array` (e.g. 'tag1,tag2' becomes `['tag1', 'tag2']`)
* `:uri` (e.g. 'http://www.google.com' becomes `URI.parse('http://www.google.com')`
* `:uri` (e.g. 'www.google.com' becomes `#<URI::Generic www.google.com>`
* `:uri_with_scheme` (is like :uri, but will raise an exception `if uri.scheme.nil?`)

### Groups

Groups give you more flexibility to define when variables are needed.
Groups give you more flexibility to define when variables are needed.
It's similar to groups in a Gemfile:

```ruby
Expand Down Expand Up @@ -147,7 +148,7 @@ variable :FORCE_SSL, :boolean, default: 'false'
variable :PORT, :integer, default: proc {|envied| envied.FORCE_SSL ? 443 : 80 }
```

Please remember that ENVied only **reads** from ENV; it doesn't mutate ENV.
Please remember that ENVied only **reads** from ENV; it doesn't mutate ENV.
Don't let setting a default for, say `RAILS_ENV`, give you the impression that `ENV['RAILS_ENV']` is set.
As a rule of thumb you should only use defaults:
* for local development
Expand Down
2 changes: 1 addition & 1 deletion lib/envied/coercer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def coerce_method_for(type)

def self.supported_types
@supported_types ||= begin
[:hash, :array, :time, :date, :symbol, :boolean, :integer, :string, :uri, :float].sort
[:hash, :array, :time, :date, :symbol, :boolean, :integer, :string, :uri, :uri_with_scheme, :float].sort
end
end

Expand Down
5 changes: 5 additions & 0 deletions lib/envied/coercer/envied_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def to_uri(str)
::URI.parse(str)
end

def to_uri_with_scheme(str)
result = to_uri(str)
result.scheme.nil? ? raise_unsupported_coercion(str, __method__) : result
end

def to_integer(str)
Integer(str)
rescue ArgumentError
Expand Down
30 changes: 28 additions & 2 deletions spec/coercer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,34 @@ def coerce_to(type)
describe 'to uri' do
let(:coerce){ coerce_to(:uri) }

it 'converts strings to uris' do
expect(coerce['http://www.google.com']).to be_a(URI)
it 'converts strings to generic uris' do
expect(coerce['server.example.com']).to be_a(URI::Generic)
end

it 'converts strings to ftp uris' do
expect(coerce['ftp://ftp.example.com']).to be_a(URI::FTP)
end
end

describe 'to uri_with_scheme' do
let(:coerce){ coerce_to(:uri_with_scheme) }

it 'converts strings to http uris' do
expect(coerce['http://www.google.com'].scheme).to eql 'http'
end

it 'converts strings to https uris' do
expect(coerce['https://github.com'].scheme).to eql 'https'
end

it 'converts strings to redis uris' do
expect(coerce['redis://example.com:6379'].scheme).to eql 'redis'
end

it 'fails for non uris' do
expect {
coerce['server.example.com']
}.to raise_error(Coercible::UnsupportedCoercion)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/envied_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,14 @@ def envied_require(*args)
describe 'URIable' do
before do
configure do
variable :site_url, :uri
variable :site_url, :uri_with_scheme
end.and_ENV('site_url' => 'https://www.google.com')
envied_require
end

it 'yields a URI from string' do
expect(ENVied.site_url).to be_a URI
expect(ENVied.site_url.scheme).to eq 'https'
expect(ENVied.site_url.host).to eq 'www.google.com'
end
end
Expand Down

0 comments on commit b102161

Please sign in to comment.