Skip to content

Commit

Permalink
feature(string_tools): add params concatenation method
Browse files Browse the repository at this point in the history
  • Loading branch information
dx0x58 committed Dec 17, 2015
1 parent f4d9b2c commit d310cbf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/string_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,14 @@ def rublej_propisju(amount)
end
end
extend SumInWords

module Uri
def add_params_to_url(url, params = nil)
uri = Addressable::URI.parse(url)
uri = Addressable::URI.parse("http://#{url}") unless uri.scheme
uri.query_values = (uri.query_values || {}).merge!(params.stringify_keys) if params.present?
uri.normalize.to_s
end
end
extend Uri
end
43 changes: 43 additions & 0 deletions spec/string_tools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,47 @@
it { expect(strip_tags_leave_br).to eq('bar<br />') }
end
end

describe '#add_params_to_url' do
subject(:add_params_to_url) { described_class.add_params_to_url(url, params) }
let(:url) { 'http://test.com' }
let(:uri) { 'http://test.com/?param=test' }

context 'when url with params' do
let(:params) { {'param' => 'test'} }

it { expect(add_params_to_url).to eq uri }
end

context 'when optional params not passed' do
it { expect(described_class.add_params_to_url(url)).to eq 'http://test.com/' }
end

context 'when url not normalized' do
let(:url) { 'http://TesT.com:80' }
let(:params) { {'param' => 'test'} }

it { expect(add_params_to_url).to eq uri }
end

context 'when url without scheme' do
let(:url) { 'test.com' }
let(:params) { {'param' => 'test'} }

it { expect(add_params_to_url).to eq uri }
end

context 'when url scheme is https' do
let(:url) { 'https://test.com' }
let(:params) { {'param' => 'test'} }

it { expect(add_params_to_url).to eq 'https://test.com/?param=test' }
end

context 'when key is a symbol with same value' do
let(:url) { 'http://test.com/?a=b' }

it { expect(described_class.add_params_to_url(url, a: 'c')).to eq 'http://test.com/?a=c' }
end
end
end

0 comments on commit d310cbf

Please sign in to comment.