shortener 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +29 -11
- data/app/controllers/shortener/shortened_urls_controller.rb +5 -1
- data/app/helpers/shortener/shortener_helper.rb +4 -0
- data/app/models/shortener/shortened_url.rb +4 -0
- data/lib/generators/shortener/templates/migration.rb +2 -2
- data/lib/shortener.rb +4 -0
- data/lib/shortener/version.rb +1 -1
- data/spec/models/shortened_url_spec.rb +15 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f83263f62775a48c270b4d54188ad42b61f33180
|
4
|
+
data.tar.gz: 1a942c5240508df45fd79872054c2378af3fdf8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 327d337f3a099622a910aa5b840ccff3dc397968452879fa2e061d54f2e969611da8f1a0a5c8ea557b58ad7dd32fd1e6f726de97a5d64c4067f7f067a5dea2db
|
7
|
+
data.tar.gz: e4ab931bd972959a8a95eaa8cddfe973a00b57ed4cb5712c6e03c7e640fcd9f1d840b8c7ca6026b4c84175bb9b8b545b74aa1f91ff81e4686892b28c404f3ee1
|
data/README.rdoc
CHANGED
@@ -80,6 +80,8 @@ After you install Shortener run the generator:
|
|
80
80
|
|
81
81
|
This generator will create a migration to create the shortened_urls table where your shortened URLs will be stored.
|
82
82
|
|
83
|
+
*Note:* The default length of the url field in the generated migration is 2083. This is because MySQL requires fixed length indicies and browsers have a defacto limit on URL length. This may not be right for you or your database. The discussion can be seen here https://github.com/jpmcgrath/shortener/pull/98
|
84
|
+
|
83
85
|
Then add to your routes:
|
84
86
|
|
85
87
|
get '/:id' => "shortener/shortened_urls#show"
|
@@ -183,6 +185,21 @@ your shortener configuration:
|
|
183
185
|
|
184
186
|
Shortener.ignore_robots = true
|
185
187
|
|
188
|
+
=== Mounting on a Subdomain
|
189
|
+
|
190
|
+
If you want to constrain the shortener route to a subdomain, the following config will
|
191
|
+
prevent the subdomain parameter from leaking in to shortened URLs if it matches the configured subdomain.
|
192
|
+
|
193
|
+
Within config/initializers/shortener.rb
|
194
|
+
|
195
|
+
Shortener.subdomain = 's'
|
196
|
+
|
197
|
+
Within config/routes.rb
|
198
|
+
|
199
|
+
constraints subdomain: 's' do
|
200
|
+
get '/:id' => "shortener/shortened_urls#show"
|
201
|
+
end
|
202
|
+
|
186
203
|
=== URL Parameters
|
187
204
|
|
188
205
|
Parameters are passed though from the shortened url, to the destination URL. If the destination
|
@@ -190,7 +207,7 @@ URL has the same parameters as the destination URL, the parameters on the shorte
|
|
190
207
|
precedence over those on the destination URL.
|
191
208
|
|
192
209
|
For example, if we have an orginal URL of:
|
193
|
-
> http://destination.com?test=yes&happy=defo (
|
210
|
+
> http://destination.com?test=yes&happy=defo (identified with token ABCDEF)
|
194
211
|
Which is shortened into:
|
195
212
|
> http://coolapp.io/s/ABCDEF?test=no&why=not
|
196
213
|
Then, the resulting URL will be:
|
@@ -247,20 +264,21 @@ Shortener is used in a number of production systems, including, but not limited
|
|
247
264
|
|
248
265
|
If you are using Shortener in your project and would like to be added to this list, please get in touch!
|
249
266
|
|
250
|
-
== Authors
|
251
|
-
|
252
|
-
* {James McGrath}[https://github.com/jpmcgrath]
|
253
|
-
* {Michael Reinsch}[https://github.com/mreinsch]
|
254
|
-
|
255
267
|
== Contributing
|
256
268
|
|
257
|
-
|
258
|
-
|
269
|
+
We welcome new contributors. Because we're all busy people, and because Shortener
|
270
|
+
is used/relied upon by many projects, it is essential that new Pull Requests
|
271
|
+
are opened with good spec coverage, and a passing build on supported ruby versions
|
272
|
+
and Rails versions.
|
259
273
|
|
260
274
|
To contribute:
|
261
275
|
|
262
276
|
1. Fork it
|
263
277
|
2. Create your feature branch (git checkout -b my-new-feature)
|
264
|
-
3.
|
265
|
-
4.
|
266
|
-
5.
|
278
|
+
3. Write spec coverage of changes
|
279
|
+
4. Commit your changes (git commit -am 'Add some feature')
|
280
|
+
5. Push to the branch (git push origin my-new-feature)
|
281
|
+
6. Create a new Pull Request
|
282
|
+
7. Ensure the build is passing
|
283
|
+
|
284
|
+
Note: We adhere to the community driven Ruby style guide: https://github.com/bbatsov/ruby-style-guide
|
@@ -1,4 +1,8 @@
|
|
1
|
-
class Shortener::ShortenedUrlsController < ActionController::
|
1
|
+
class Shortener::ShortenedUrlsController < ActionController::Metal
|
2
|
+
include ActionController::StrongParameters
|
3
|
+
include ActionController::Redirecting
|
4
|
+
include ActionController::Instrumentation
|
5
|
+
include Rails.application.routes.url_helpers
|
2
6
|
include Shortener
|
3
7
|
|
4
8
|
def show
|
@@ -12,6 +12,10 @@ module Shortener::ShortenerHelper
|
|
12
12
|
)
|
13
13
|
|
14
14
|
if short_url
|
15
|
+
if subdomain = Shortener.subdomain
|
16
|
+
url_options = url_options.merge(subdomain: subdomain)
|
17
|
+
end
|
18
|
+
|
15
19
|
options = { controller: :"/shortener/shortened_urls", action: :show, id: short_url.unique_key, only_path: false }.merge(url_options)
|
16
20
|
url_for(options)
|
17
21
|
else
|
@@ -98,6 +98,10 @@ class Shortener::ShortenedUrl < ActiveRecord::Base
|
|
98
98
|
params = params.permit!.to_h.with_indifferent_access.except!(:id, :action, :controller)
|
99
99
|
end
|
100
100
|
|
101
|
+
if Shortener.subdomain
|
102
|
+
params.try(:except!, :subdomain) if params[:subdomain] == Shortener.subdomain
|
103
|
+
end
|
104
|
+
|
101
105
|
if params.present?
|
102
106
|
uri = URI.parse(url)
|
103
107
|
existing_params = Rack::Utils.parse_nested_query(uri.query)
|
@@ -6,7 +6,7 @@ class CreateShortenedUrlsTable < ActiveRecord::Migration[4.2]
|
|
6
6
|
t.string :owner_type, limit: 20
|
7
7
|
|
8
8
|
# the real url that we will redirect to
|
9
|
-
t.text :url, null: false
|
9
|
+
t.text :url, null: false, length: 2083
|
10
10
|
|
11
11
|
# the unique key
|
12
12
|
t.string :unique_key, limit: 10, null: false
|
@@ -26,7 +26,7 @@ class CreateShortenedUrlsTable < ActiveRecord::Migration[4.2]
|
|
26
26
|
# we will lookup the links in the db by key, urls and owners.
|
27
27
|
# also make sure the unique keys are actually unique
|
28
28
|
add_index :shortened_urls, :unique_key, unique: true
|
29
|
-
add_index :shortened_urls, :url
|
29
|
+
add_index :shortened_urls, :url, length: 2083
|
30
30
|
add_index :shortened_urls, [:owner_id, :owner_type]
|
31
31
|
add_index :shortened_urls, :category
|
32
32
|
end
|
data/lib/shortener.rb
CHANGED
@@ -10,6 +10,10 @@ module Shortener
|
|
10
10
|
alphanumcase: ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a
|
11
11
|
}
|
12
12
|
|
13
|
+
# subdomain if not mounted on site root
|
14
|
+
mattr_accessor :subdomain
|
15
|
+
self.subdomain = false
|
16
|
+
|
13
17
|
# default key length: 5 characters
|
14
18
|
mattr_accessor :unique_key_length
|
15
19
|
self.unique_key_length = 5
|
data/lib/shortener/version.rb
CHANGED
@@ -222,6 +222,21 @@ describe Shortener::ShortenedUrl, type: :model do
|
|
222
222
|
expect(Shortener::ShortenedUrl.merge_params_to_url(url: url, params: params)).to eq 'http://example.com/pathname?different=yes&foo=manchoo&hello=world'
|
223
223
|
end
|
224
224
|
end
|
225
|
+
|
226
|
+
context 'with Shortener.subdomain configured' do
|
227
|
+
let(:url) { 'http://example.com/pathname'}
|
228
|
+
|
229
|
+
before { allow(Shortener).to receive(:subdomain).and_return('go') }
|
230
|
+
|
231
|
+
it 'filters the subdomain parameter if it matches the subdomain' do
|
232
|
+
params = {foo: 'test', subdomain: 'go' }
|
233
|
+
expect(Shortener::ShortenedUrl.merge_params_to_url(url: url, params: params)).to eq 'http://example.com/pathname?foo=test'
|
234
|
+
end
|
235
|
+
it 'merges the subdomain parameter if it does not match the subdomain' do
|
236
|
+
params = {foo: 'test', subdomain: 's' }
|
237
|
+
expect(Shortener::ShortenedUrl.merge_params_to_url(url: url, params: params)).to eq 'http://example.com/pathname?foo=test&subdomain=s'
|
238
|
+
end
|
239
|
+
end
|
225
240
|
end
|
226
241
|
end
|
227
242
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shortener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James P. McGrath
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-
|
12
|
+
date: 2018-05-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: voight_kampff
|