shortener 0.5.3 → 0.5.4

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: e1ff25934680ccaffed2ea4b7a05d14c033fb450
4
- data.tar.gz: dedf6fdcd81671c96dadccbd14b89e724a919107
3
+ metadata.gz: eefb46cddedbe3f232ef038a7f2385b3c2a3121d
4
+ data.tar.gz: 8018071c727b482ca8c503951d4b4ad59d2ddd71
5
5
  SHA512:
6
- metadata.gz: 8a87d5020c3f51791794d037ee206ebfee4e6f6595ec88e749c03e02a88e5c8aa9914f7dd8a0f25e680f4466c43c523989d5b4eac597393dea3f5fc24c46691d
7
- data.tar.gz: f3dcc1ad1ec6b6bd5f6a93ff9838c022c3d2b9c407e57c026915f98202ad4ebd1518421e85c88e439fcf20108bf2f12373a065de52fa1fe67ad06ab3e9438f02
6
+ metadata.gz: 68fbe46e207434cb7405d149fef86e7644b05d9f76a6c050d1065f2aeb6b2608c63340b9f7365feb6069b1bf844b33a85060da9eb86265322d581a03d024bf10
7
+ data.tar.gz: 180eeff9eaa296bf89ecfaa09ffe626bdd7174c2930b6fbe69f59480b3b81bb487ccf6d78fd2f3e0f27c17ac4b3b90962fb865e622f9e0a6ec352f712dc71092
data/README.rdoc CHANGED
@@ -39,7 +39,6 @@ There have been some breaking changes:
39
39
  === Future improvements:
40
40
 
41
41
  * There has not been an attempt to remove ambiguous characters (i.e. 1 l and capital i, or 0 and O etc.) from the unique key generated for the link. This means people might copy the link incorrectly if copying the link by hand;
42
- * The shortened links are found with a case-insensitive search on the unique key. This means that the system can’t take advantage of upper and lower case to increase the number of unique combinations. This may have an effect for people copying the link by hand;
43
42
  * The system could pre-generate unique keys in advance, avoiding the database penalty when checking that a newly generated key is unique;
44
43
  * The system could store the shortened URL if the url is to be continually rendered;
45
44
  * Some implementations might want duplicate links to be generated each time a user request a shortened link.
@@ -71,6 +70,11 @@ By default, when a unique key isn't matched the site is redirected to "/". You c
71
70
 
72
71
  Shortener.default_redirect = "http://www.someurl.com"
73
72
 
73
+ By default, Shortener will generate unique keys using numbers and lowercase a-z. If you desire more combinations, you can enable
74
+ the upper and lower case charset, by including the following:
75
+
76
+ Shortener.charset = :alphanumcase
77
+
74
78
  == Usage
75
79
 
76
80
  To generate a Shortened URL object for the URL "http://example.com" within your controller / models do the following:
@@ -85,6 +89,10 @@ To generate and display a shortened URL in your application use the helper metho
85
89
 
86
90
  short_url("http://example.com")
87
91
 
92
+ Pass in subdomain, protocol and other options that the UrlHelper url_for accepts:
93
+
94
+ short_url("http://example.com", url_options: { subdomain: 'foo', host: 'bar, protocol: 'https' } )
95
+
88
96
  This will generate a shortened URL. store it to the db and return a string representing the shortened URL.
89
97
 
90
98
  === Shortened URLs with owner
@@ -124,6 +132,15 @@ You can omit owner passing nil instead:
124
132
 
125
133
  Shortener::ShortenedUrl.generate("example.com/page", nil, expires_at: 24.hours.since)
126
134
 
135
+ === Fresh Links
136
+
137
+ Sometimes you just need that feeling of a fresh, untouched Shortened URL. By default,
138
+ Shortener will find an existing ShortenedUrl record for a supplied URL. If you want
139
+ to create a fresh record, you can pass the following argument:
140
+
141
+ Shortener::ShortenedUrl.generate("example.com/page", fresh: true)
142
+ short_url("http://example.com", fresh: true)
143
+
127
144
  === Forbidden keys
128
145
 
129
146
  You can ensure that records with forbidden keys will not be generated.
@@ -131,7 +148,7 @@ In rails you can put next line into config/initializers/shortener.rb
131
148
 
132
149
  Shortener.forbidden_keys.concat %w(terms promo)
133
150
 
134
- === Parameters
151
+ === Helper Parameters
135
152
 
136
153
  Parameters are passed though from the shortened url, to the destination URL. If the destination
137
154
  URL has the same parameters as the destination URL, the parameters on the shortened url take
@@ -170,3 +187,15 @@ If you are using Shortener in your project and would like to be added to this li
170
187
  * {James McGrath}[https://github.com/jpmcgrath]
171
188
  * {Michael Reinsch}[https://github.com/mreinsch]
172
189
 
190
+ == Contributing
191
+
192
+ New feature requests are welcome, code is more welcome still. Code with Specs is the
193
+ most welcomiest there is!
194
+
195
+ To contribute:
196
+
197
+ 1. Fork it
198
+ 2. Create your feature branch (git checkout -b my-new-feature)
199
+ 3. Commit your changes (git commit -am 'Add some feature')
200
+ 4. Push to the branch (git push origin my-new-feature)
201
+ 5. Create a new Pull Request
@@ -1,9 +1,20 @@
1
1
  module Shortener::ShortenerHelper
2
2
 
3
3
  # generate a url from a url string
4
- def short_url(url, owner: nil, custom_key: nil)
5
- short_url = Shortener::ShortenedUrl.generate(url, owner: owner, custom_key: custom_key)
6
- short_url ? url_for(controller: :"shortener/shortened_urls", action: :show, id: short_url.unique_key, only_path: false) : url
4
+ def short_url(url, owner: nil, custom_key: nil, expires_at: nil, fresh: false, url_options: {})
5
+ short_url = Shortener::ShortenedUrl.generate(url,
6
+ owner: owner,
7
+ custom_key: custom_key,
8
+ expires_at: expires_at,
9
+ fresh: fresh
10
+ )
11
+
12
+ if short_url
13
+ options = { controller: :"shortener/shortened_urls", action: :show, id: short_url.unique_key, only_path: false }.merge(url_options)
14
+ url_for(options)
15
+ else
16
+ url
17
+ end
7
18
  end
8
19
 
9
20
  end
@@ -23,27 +23,37 @@ class Shortener::ShortenedUrl < ActiveRecord::Base
23
23
  # generate a shortened link from a url
24
24
  # link to a user if one specified
25
25
  # throw an exception if anything goes wrong
26
- def self.generate!(destination_url, owner: nil, custom_key: nil, expires_at: nil)
26
+ def self.generate!(destination_url, owner: nil, custom_key: nil, expires_at: nil, fresh: false)
27
27
  # if we get a shortened_url object with a different owner, generate
28
28
  # new one for the new owner. Otherwise return same object
29
29
  if destination_url.is_a? Shortener::ShortenedUrl
30
30
  if destination_url.owner == owner
31
31
  result = destination_url
32
32
  else
33
- result = generate!(destination_url.url, owner: owner, custom_key: custom_key, expires_at: expires_at)
33
+ result = generate!(destination_url.url,
34
+ owner: owner,
35
+ custom_key: custom_key,
36
+ expires_at: expires_at,
37
+ fresh: fresh
38
+ )
34
39
  end
35
40
  else
36
41
  scope = owner ? owner.shortened_urls : self
37
- result = scope.where(url: clean_url(destination_url)).first_or_create(unique_key: custom_key, expires_at: expires_at)
42
+
43
+ if fresh
44
+ result = scope.where(url: clean_url(destination_url)).create(unique_key: custom_key, expires_at: expires_at)
45
+ else
46
+ result = scope.where(url: clean_url(destination_url)).first_or_create(unique_key: custom_key, expires_at: expires_at)
47
+ end
38
48
  end
39
49
 
40
50
  result
41
51
  end
42
52
 
43
53
  # return shortened url on success, nil on failure
44
- def self.generate(destination_url, owner: nil, custom_key: nil, expires_at: nil)
54
+ def self.generate(destination_url, owner: nil, custom_key: nil, expires_at: nil, fresh: false)
45
55
  begin
46
- generate!(destination_url, owner: owner, custom_key: custom_key, expires_at: expires_at)
56
+ generate!(destination_url, owner: owner, custom_key: custom_key, expires_at: expires_at, fresh: fresh)
47
57
  rescue => e
48
58
  logger.info e
49
59
  nil
@@ -84,10 +94,7 @@ class Shortener::ShortenedUrl < ActiveRecord::Base
84
94
  end
85
95
  end
86
96
 
87
- # generate a random string
88
- # future mod to allow specifying a more expansive charst, like utf-8 chinese
89
97
  def generate_unique_key
90
- # not doing uppercase as url is case insensitive
91
98
  charset = ::Shortener.key_chars
92
99
  (0...::Shortener.unique_key_length).map{ charset[rand(charset.size)] }.join
93
100
  end
@@ -1,3 +1,3 @@
1
1
  module Shortener
2
- VERSION = "0.5.3"
2
+ VERSION = "0.5.4"
3
3
  end
@@ -7,7 +7,7 @@ describe Shortener::ShortenerHelper, type: :helper do
7
7
 
8
8
  context 'without user or custom key' do
9
9
  before do
10
- expect(Shortener::ShortenedUrl).to receive(:generate).with(destination, owner: nil, custom_key: nil).and_return(shortened_url)
10
+ expect(Shortener::ShortenedUrl).to receive(:generate).with(destination, owner: nil, custom_key: nil, expires_at: nil, fresh: false).and_return(shortened_url)
11
11
  end
12
12
 
13
13
  context 'short url was generated' do
@@ -30,16 +30,32 @@ describe Shortener::ShortenerHelper, type: :helper do
30
30
  context 'with owner' do
31
31
  let(:owner) { double('User') }
32
32
  it 'sends user to generate function' do
33
- expect(Shortener::ShortenedUrl).to receive(:generate).with(destination, owner: owner, custom_key: nil)
33
+ expect(Shortener::ShortenedUrl).to receive(:generate).with(destination, owner: owner, custom_key: nil, expires_at: nil, fresh: false)
34
34
  helper.short_url(destination, owner: owner)
35
35
  end
36
36
  end
37
37
 
38
38
  context 'with custom_key' do
39
39
  it 'sends custom key code to generate function' do
40
- expect(Shortener::ShortenedUrl).to receive(:generate).with(destination, owner: nil, custom_key: 'custkey')
40
+ expect(Shortener::ShortenedUrl).to receive(:generate).with(destination, owner: nil, custom_key: 'custkey', expires_at: nil, fresh: false)
41
41
  helper.short_url(destination, custom_key: 'custkey')
42
42
  end
43
43
  end
44
+
45
+ context 'with expires_at' do
46
+ it 'sends custom key code to generate function' do
47
+ expect(Shortener::ShortenedUrl).to receive(:generate).with(destination, owner: nil, custom_key: nil, expires_at: 'testtime', fresh: false)
48
+ helper.short_url(destination, expires_at: 'testtime')
49
+ end
50
+ end
51
+
52
+ context 'with url options for https and subdomain' do
53
+ let(:short_url) { double('ShortUrl', unique_key: 'knownkey') }
54
+ it 'sends custom key code to generate function' do
55
+ expect(Shortener::ShortenedUrl).to receive(:generate).and_return(short_url)
56
+ url = helper.short_url(destination, expires_at: 'testtime', url_options: { subdomain: 'foo', protocol: 'https'})
57
+ expect(url).to eq 'https://foo.test.host/knownkey'
58
+ end
59
+ end
44
60
  end
45
61
  end
@@ -52,6 +52,19 @@ describe Shortener::ShortenedUrl, type: :model do
52
52
  end
53
53
  end
54
54
 
55
+ context "request a fresh shortened URL" do
56
+ let(:url) { Faker::Internet.url }
57
+ it "shouldn't create duplicate urls" do
58
+ Shortener::ShortenedUrl.generate!(url)
59
+ expect{Shortener::ShortenedUrl.generate!(url)}.not_to change{Shortener::ShortenedUrl.count}
60
+ end
61
+
62
+ it "should create a fresh shortened urls" do
63
+ Shortener::ShortenedUrl.generate!(url)
64
+ expect{Shortener::ShortenedUrl.generate!(url, fresh: true)}.to change{Shortener::ShortenedUrl.count}.by(1)
65
+ end
66
+ end
67
+
55
68
  context "existing shortened URL" do
56
69
  let(:url) { Faker::Internet.url }
57
70
  let!(:existing_shortened_url) { Shortener::ShortenedUrl.generate!(url) }
@@ -46,6 +46,7 @@ describe Shortener::ShortenUrlInterceptor do
46
46
  "http://client.doorkeeper.jp/events/124-",
47
47
  "http://client.doorkeeper.jp/events/124-title?auth_token=xabagangs",
48
48
  "http://client.doorkeeper.jp/events/124-%E4%F6%A0",
49
+ "https://www.test-site.com/"
49
50
  ].each do |url|
50
51
  it_should_behave_like "shortens URL in text", url
51
52
  end
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.5.3
4
+ version: 0.5.4
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: 2015-09-19 00:00:00.000000000 Z
12
+ date: 2015-09-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails