shortener 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1ff25934680ccaffed2ea4b7a05d14c033fb450
|
4
|
+
data.tar.gz: dedf6fdcd81671c96dadccbd14b89e724a919107
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a87d5020c3f51791794d037ee206ebfee4e6f6595ec88e749c03e02a88e5c8aa9914f7dd8a0f25e680f4466c43c523989d5b4eac597393dea3f5fc24c46691d
|
7
|
+
data.tar.gz: f3dcc1ad1ec6b6bd5f6a93ff9838c022c3d2b9c407e57c026915f98202ad4ebd1518421e85c88e439fcf20108bf2f12373a065de52fa1fe67ad06ab3e9438f02
|
data/README.rdoc
CHANGED
@@ -58,7 +58,7 @@ This generator will create a migration to create the shortened_urls table where
|
|
58
58
|
|
59
59
|
Then add to your routes:
|
60
60
|
|
61
|
-
|
61
|
+
get '/:id' => "shortener/shortened_urls#show"
|
62
62
|
|
63
63
|
== Configuration
|
64
64
|
The gem can be configured in a config/initializers/shortener.rb file.
|
@@ -131,6 +131,12 @@ In rails you can put next line into config/initializers/shortener.rb
|
|
131
131
|
|
132
132
|
Shortener.forbidden_keys.concat %w(terms promo)
|
133
133
|
|
134
|
+
=== Parameters
|
135
|
+
|
136
|
+
Parameters are passed though from the shortened url, to the destination URL. If the destination
|
137
|
+
URL has the same parameters as the destination URL, the parameters on the shortened url take
|
138
|
+
precedence over those on the destination URL.
|
139
|
+
|
134
140
|
=== Shorten URLs in generated emails
|
135
141
|
|
136
142
|
You can register the included mail interceptor to shorten all links in the emails generated by your Rails app. For example, add to your mailer:
|
@@ -17,8 +17,20 @@ class Shortener::ShortenedUrlsController < ActionController::Base
|
|
17
17
|
sl.increment!(:use_count)
|
18
18
|
ActiveRecord::Base.connection.close
|
19
19
|
end
|
20
|
+
|
21
|
+
params.except! *[:id, :action, :controller]
|
22
|
+
url = sl.url
|
23
|
+
|
24
|
+
if params.present?
|
25
|
+
uri = URI.parse(sl.url)
|
26
|
+
existing_params = Rack::Utils.parse_nested_query(uri.query)
|
27
|
+
merged_params = existing_params.merge(params)
|
28
|
+
uri.query = merged_params.to_query
|
29
|
+
url = uri.to_s
|
30
|
+
end
|
31
|
+
|
20
32
|
# do a 301 redirect to the destination url
|
21
|
-
redirect_to
|
33
|
+
redirect_to url, status: :moved_permanently
|
22
34
|
else
|
23
35
|
# if we don't find the shortened link, redirect to the root
|
24
36
|
# make this configurable in future versions
|
data/lib/shortener/version.rb
CHANGED
@@ -6,8 +6,9 @@ describe Shortener::ShortenedUrlsController, type: :controller do
|
|
6
6
|
let(:short_url) { Shortener::ShortenedUrl.generate(destination) }
|
7
7
|
|
8
8
|
describe '#show' do
|
9
|
+
let(:params) { {} }
|
9
10
|
before do
|
10
|
-
get :show, id: key
|
11
|
+
get :show, { id: key }.merge(params)
|
11
12
|
end
|
12
13
|
|
13
14
|
context 'valid keys' do
|
@@ -26,6 +27,33 @@ describe Shortener::ShortenedUrlsController, type: :controller do
|
|
26
27
|
expect(response).to redirect_to destination
|
27
28
|
end
|
28
29
|
end
|
30
|
+
|
31
|
+
context 'parameters on short url' do
|
32
|
+
let(:params) { { foo: 34, bar: 49 } }
|
33
|
+
let(:key) { short_url.unique_key }
|
34
|
+
|
35
|
+
context 'no parameters on long url' do
|
36
|
+
it 'redirects to the destination url with the parmeters' do
|
37
|
+
redirect_url_params = Rack::Utils.parse_nested_query(URI.parse(response.location).query)
|
38
|
+
|
39
|
+
expect(redirect_url_params['foo']).to eq '34'
|
40
|
+
expect(redirect_url_params['bar']).to eq '49'
|
41
|
+
expect(response.code).to eq '301'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
context 'clashing parameters on long url' do
|
45
|
+
let(:destination) { "#{Faker::Internet.url}?foo=26&noclash=56" }
|
46
|
+
|
47
|
+
it 'redirects to the destination url with the parmeters on the short url taking priority' do
|
48
|
+
redirect_url_params = Rack::Utils.parse_nested_query(URI.parse(response.location).query)
|
49
|
+
|
50
|
+
expect(redirect_url_params['foo']).to eq '34'
|
51
|
+
expect(redirect_url_params['bar']).to eq '49'
|
52
|
+
expect(redirect_url_params['noclash']).to eq '56'
|
53
|
+
expect(response.code).to eq '301'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
29
57
|
end
|
30
58
|
|
31
59
|
context 'invalid keys' do
|