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 option to disable URL normalization #135

Merged
merged 2 commits into from May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.rdoc
@@ -1,4 +1,4 @@
{<img src="https://secure.travis-ci.org/jpmcgrath/shortener.svg?branch=master" alt="Build Status" />}[http://travis-ci.org/jpmcgrath/shortener]
{<img src="https://secure.travis-ci.org/jpmcgrath/shortener.svg?branch=master" alt="Build Status" />}[http://travis-ci.org/jpmcgrath/shortener]
{<img src="https://codeclimate.com/github/jpmcgrath/shortener/badges/gpa.svg" />}[https://codeclimate.com/github/jpmcgrath/shortener]
{<img src="https://badge.fury.io/rb/shortener.svg" alt="Gem version" />}[http://badge.fury.io/rb/shortener]

Expand Down Expand Up @@ -96,11 +96,16 @@ By default, Shortener will generate unique keys using numbers and lowercase a-z.
the upper and lower case charset, by including the following:

Shortener.charset = :alphanumcase

If you want to use a custom charset, you can create your own combination by creating an array of possible values, such as allowing underscore and dashes:

Shortener.charset = ("a".."z").to_a + (0..9).to_a + ["-", "_"]

By default, <b>Shortener assumes URLs to be valid web URLs</b> and normalizes them in an effort to make sure there are no duplicate records generated for effectively same URLs with differences of only non-effective slash etc.
You can control this option if it interferes for any of your logic. One common case is for mobile app links or universal links where normalization can corrupt the URLs of form <tt>appname://some_route</tt>

Shortener.auto_clean_url = true

== Usage

To generate a Shortened URL object for the URL "http://example.com" within your controller / models do the following:
Expand Down Expand Up @@ -141,7 +146,7 @@ And to access those URLs:

=== Shortened URLs with custom unique key

You can pass in your own key when generating a shortened URL. This should be unique.
You can pass in your own key when generating a shortened URL. This should be unique.

*Important:* Custom keys can't contain characters other than those defined in *Shortener.charset*. Default is numbers and lowercase a-z (See *Configuration*).

Expand Down
3 changes: 2 additions & 1 deletion app/models/shortener/shortened_url.rb
Expand Up @@ -52,7 +52,8 @@ def self.generate!(destination_url, owner: nil, custom_key: nil, expires_at: nil
scope = owner ? owner.shortened_urls : self
creation_method = fresh ? 'create' : 'first_or_create'

scope.where(url: clean_url(destination_url), category: category).send(
url_to_save = Shortener.auto_clean_url ? clean_url(destination_url) : destination_url
scope.where(url: url_to_save, category: category).send(
creation_method,
custom_key: custom_key,
expires_at: expires_at
Expand Down
4 changes: 4 additions & 0 deletions lib/shortener.rb
Expand Up @@ -41,6 +41,10 @@ module Shortener
mattr_accessor :persist_retries
self.persist_retries = 3

# auto_clean_url - controls url cleaning mechanism, set it to false to disable
mattr_accessor :auto_clean_url
self.auto_clean_url = true

def self.key_chars
charset.is_a?(Symbol) ? CHARSETS[charset] : charset
end
Expand Down
22 changes: 20 additions & 2 deletions spec/models/shortened_url_spec.rb
Expand Up @@ -177,8 +177,26 @@
it 'finds the shortened url from slashless oath' do
expect(Shortener::ShortenedUrl.generate!(path)).to eq existing_shortened_url
end
it "should look up exsiting URL" do
expect(Shortener::ShortenedUrl.generate!("/#{path}")).to eq existing_shortened_url

context 'with auto_clean_url enabled by default' do
it "looks up existing cleaned URL" do
expect(Shortener::ShortenedUrl.generate!("/#{path}")).to eq existing_shortened_url
end
end

context 'with auto_clean_url disabled' do
around do |spec|
tries = Shortener.auto_clean_url
Shortener.auto_clean_url = false
spec.run
Shortener.auto_clean_url = tries
end

it "does not look up existing cleaned URL" do
shortened_url = Shortener::ShortenedUrl.generate!("/#{path}")
expect(shortened_url).not_to eq existing_shortened_url
expect(shortened_url.url).to eq "/#{path}"
end
end
end
end
Expand Down