Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url coercer #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ The following types are supported:
* `: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_with_scheme` (is like :uri, but will raise an exception `if URI.parse('www.google.com').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