Skip to content

Instantly share code, notes, and snippets.

@marcioj
Last active April 25, 2016 19:12
Show Gist options
  • Save marcioj/6f40f3d6fbec0b3c1ed1 to your computer and use it in GitHub Desktop.
Save marcioj/6f40f3d6fbec0b3c1ed1 to your computer and use it in GitHub Desktop.
Url normalization. Appends http to url which doesn't have it
module ApplicationHelper
def expand_url(url)
return url if url.blank? || /(http:\/\/|https:\/\/)/ =~ url
"http://#{url}"
end
end
require 'rails_helper'
RSpec.describe ApplicationHelper do
RSpec.describe '#expand_url' do
it { expect(helper.expand_url('google.com')).to eq('http://google.com') }
it { expect(helper.expand_url('http://google.com')).to eq('http://google.com') }
it { expect(helper.expand_url('https://google.com')).to eq('https://google.com') }
it { expect(helper.expand_url('')).to eq('') }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment