shortener 0.6.2 → 0.7.1

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: ae732fd2135a915ebf0565d0350279894ed29ddc
4
- data.tar.gz: 730ea8bc1f3c867ae3f3caa6ca2695399d61b8fc
3
+ metadata.gz: 7dcaac83914269aad38d35834f0a8550ce8ccb51
4
+ data.tar.gz: 31f10e8bd149d9a9ed5378e136571e6a0d5ce2e8
5
5
  SHA512:
6
- metadata.gz: 628ab6bb9d1bc641a3727e26643b617df6092aecb20ee0f9cdc4418b889418a85609b45231754fc462d813e203709ba48b61a7972520efbef1ef65f93e65abb9
7
- data.tar.gz: 3adb8897c37e618ce3fe27c999895b1519ae9e97d2d370ad2d16e2a27a6cca94ae646feb97617bcd370d607d5ce50244c244535c5c30c4f122e9d650ceae1865
6
+ metadata.gz: 6e87227bf0b46869caec0e518631104d93f245d79746d25606ae4790ee9f912ebdf5d1bb691c132be918a0736cfdf568137c8176ffe5ba3c056c122d3260411f
7
+ data.tar.gz: e1626bae89579ca2928e24d6f093432220464b34fb42beab57852f739200416576789216f82fac1b17ab0b0710ca10027029a5d6495262a7a2aa4184bd1cca55
data/.travis.yml CHANGED
@@ -1,5 +1,16 @@
1
1
  language: ruby
2
+
2
3
  rvm:
3
- - 2.0.0
4
- - 2.1.0
5
- - 2.2.3
4
+ - 2.2.10
5
+ - 2.3
6
+ - 2.4
7
+ - 2.5
8
+
9
+ matrix:
10
+ fast_finish: true
11
+
12
+ gemfile:
13
+ - gemfiles/rails_4.gemfile
14
+ - gemfiles/rails_5.0.gemfile
15
+ - gemfiles/rails_5.1.gemfile
16
+ - gemfiles/rails_5.2.gemfile
data/Appraisals ADDED
@@ -0,0 +1,15 @@
1
+ appraise "rails_4" do
2
+ gem "rails", "~> 4.2.10"
3
+ end
4
+
5
+ appraise "rails_5.0" do
6
+ gem "rails", "~> 5.0.7"
7
+ end
8
+
9
+ appraise "rails_5.1" do
10
+ gem "rails", "~> 5.1.6"
11
+ end
12
+
13
+ appraise "rails_5.2" do
14
+ gem "rails", "5.2.0"
15
+ end
data/README.rdoc CHANGED
@@ -33,21 +33,21 @@ v0.5.2 introduced the ability to set an expiration date for a shortened URL. The
33
33
  are stored in a expires_at column in the database, which can be added to your schema with the following
34
34
  migration:
35
35
 
36
- class AddExpiresAtToShortenedUrl < ActiveRecord::Migration
36
+ class AddExpiresAtToShortenedUrl < ActiveRecord::Migration[4.2]
37
37
  def change
38
38
  add_column :shortened_urls, :expires_at, :datetime
39
39
  end
40
40
  end
41
41
 
42
42
  === v0.5.6 to v0.6.1
43
- v0.6.1 introduced the ability to category a shortened URL. The category value
44
- is stored in a string column in the database, which can be added to your schema with the following
43
+ v0.6.1 introduced the ability to categorize a shortened URL. The category value
44
+ is stored in a string column in the database, which must be added to your schema with the following
45
45
  migration:
46
46
 
47
- bundle exec rails g migration add_category_to_shortened_url
47
+ bundle exec rails g migration add_category_to_shortened_url category:string:index
48
48
 
49
49
 
50
- class AddCategoryToShortenedUrl < ActiveRecord::Migration
50
+ class AddCategoryToShortenedUrl < ActiveRecord::Migration[4.2]
51
51
  def change
52
52
  add_column :shortened_urls, :category, :string
53
53
  add_index :shortened_urls, :category
@@ -1,5 +1,3 @@
1
- require 'voight_kampff'
2
-
3
1
  class Shortener::ShortenedUrlsController < ActionController::Base
4
2
  include Shortener
5
3
 
@@ -4,6 +4,8 @@ class Shortener::ShortenedUrl < ActiveRecord::Base
4
4
 
5
5
  validates :url, presence: true
6
6
 
7
+ before_create :generate_unique_key
8
+
7
9
  # allows the shortened link to be associated with a user
8
10
  belongs_to :owner, polymorphic: true
9
11
 
@@ -47,7 +49,6 @@ class Shortener::ShortenedUrl < ActiveRecord::Base
47
49
 
48
50
  scope.where(url: clean_url(destination_url), category: category).send(
49
51
  creation_method,
50
- unique_key: custom_key,
51
52
  custom_key: custom_key,
52
53
  expires_at: expires_at
53
54
  )
@@ -93,12 +94,14 @@ class Shortener::ShortenedUrl < ActiveRecord::Base
93
94
  end
94
95
 
95
96
  def self.merge_params_to_url(url: nil, params: {})
96
- params.try(:except!, *[:id, :action, :controller])
97
+ if params.respond_to?(:permit!)
98
+ params = params.permit!.to_h.with_indifferent_access.except!(:id, :action, :controller)
99
+ end
97
100
 
98
101
  if params.present?
99
102
  uri = URI.parse(url)
100
103
  existing_params = Rack::Utils.parse_nested_query(uri.query)
101
- uri.query = existing_params.symbolize_keys.merge(params).to_query
104
+ uri.query = existing_params.with_indifferent_access.merge(params).to_query
102
105
  url = uri.to_s
103
106
  end
104
107
 
@@ -106,11 +109,7 @@ class Shortener::ShortenedUrl < ActiveRecord::Base
106
109
  end
107
110
 
108
111
  def increment_usage_count
109
- Thread.new do
110
- ActiveRecord::Base.connection_pool.with_connection do |conn|
111
- increment!(:use_count)
112
- end
113
- end
112
+ self.class.increment_counter(:use_count, id)
114
113
  end
115
114
 
116
115
  def to_param
@@ -119,46 +118,15 @@ class Shortener::ShortenedUrl < ActiveRecord::Base
119
118
 
120
119
  private
121
120
 
122
- # the create method changed in rails 4...
123
- CREATE_METHOD_NAME =
124
- if Rails::VERSION::MAJOR >= 5
125
- "_create_record"
126
- elsif Rails::VERSION::MAJOR == 4
127
- # And again in 4.0.6/4.1.2
128
- if Rails::VERSION::MAJOR == 4 && (
129
- ((Rails::VERSION::MINOR == 0) && (Rails::VERSION::TINY < 6)) ||
130
- ((Rails::VERSION::MINOR == 1) && (Rails::VERSION::TINY < 2)))
131
- "create_record"
132
- else
133
- "_create_record"
134
- end
135
- else
136
- "create"
137
- end
138
-
139
- # we'll rely on the DB to make sure the unique key is really unique.
140
- # if it isn't unique, the unique index will catch this and raise an error
141
- define_method CREATE_METHOD_NAME do
142
- count = 0
121
+ def generate_unique_key
143
122
  begin
144
- self.unique_key = custom_key || generate_unique_key
145
- super()
146
- rescue ActiveRecord::RecordNotUnique, ActiveRecord::StatementInvalid => err
147
- logger.info("Failed to generate ShortenedUrl with unique_key: #{unique_key}")
148
- self.unique_key = nil
149
- if (count +=1) < 5
150
- logger.info("retrying with different unique key")
151
- retry
152
- else
153
- logger.info("too many retries, giving up")
154
- raise
155
- end
156
- end
123
+ self.unique_key = custom_key || self.class.unique_key_candidate
124
+ self.custom_key = nil
125
+ end while self.class.exists?(unique_key: unique_key) && custom_key.blank?
157
126
  end
158
127
 
159
- def generate_unique_key
128
+ def self.unique_key_candidate
160
129
  charset = ::Shortener.key_chars
161
130
  (0...::Shortener.unique_key_length).map{ charset[rand(charset.size)] }.join
162
131
  end
163
-
164
132
  end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 4.2.10"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 5.0.7"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 5.1.6"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "5.2.0"
6
+
7
+ gemspec path: "../"
@@ -1,4 +1,4 @@
1
- class CreateShortenedUrlsTable < ActiveRecord::Migration
1
+ class CreateShortenedUrlsTable < ActiveRecord::Migration[4.2]
2
2
  def change
3
3
  create_table :shortened_urls do |t|
4
4
  # we can link this to a user for interesting things
@@ -11,8 +11,8 @@ class CreateShortenedUrlsTable < ActiveRecord::Migration
11
11
  # the unique key
12
12
  t.string :unique_key, limit: 10, null: false
13
13
 
14
- # a label to help categorize shortened urls
15
- t.string :label, :string
14
+ # a category to help categorize shortened urls
15
+ t.string :category
16
16
 
17
17
  # how many times the link has been clicked
18
18
  t.integer :use_count, null: false, default: 0
@@ -28,6 +28,6 @@ class CreateShortenedUrlsTable < ActiveRecord::Migration
28
28
  add_index :shortened_urls, :unique_key, unique: true
29
29
  add_index :shortened_urls, :url
30
30
  add_index :shortened_urls, [:owner_id, :owner_type]
31
- add_index :shortened_urls, :label
31
+ add_index :shortened_urls, :category
32
32
  end
33
33
  end
@@ -3,3 +3,12 @@ module Shortener::ActiveRecordExtension
3
3
  has_many :shortened_urls, class_name: "::Shortener::ShortenedUrl", as: :owner
4
4
  end
5
5
  end
6
+
7
+ if Rails::VERSION::MAJOR < 5 && !ActiveRecord::Migration.respond_to?(:[])
8
+ module VersionedMigrationPatch
9
+ def [](*args)
10
+ ActiveRecord::Migration
11
+ end
12
+ end
13
+ ActiveRecord::Migration.extend VersionedMigrationPatch
14
+ end
@@ -1,4 +1,5 @@
1
1
  require "rails/engine"
2
+ require "voight_kampff"
2
3
  require "shortener"
3
4
 
4
5
  class Shortener::Engine < ::Rails::Engine #:nodoc:
@@ -1,3 +1,3 @@
1
1
  module Shortener
2
- VERSION = '0.6.2'
2
+ VERSION = '0.7.1'
3
3
  end
data/shortener.gemspec CHANGED
@@ -13,14 +13,18 @@ Gem::Specification.new do |s|
13
13
  s.email = [ "gems@jamespmcgrath.com", "michael@mobalean.com" ]
14
14
  s.homepage = "http://jamespmcgrath.com/projects/shortener"
15
15
  s.rubyforge_project = "shortener"
16
- s.required_rubygems_version = "> 1.3.6"
17
- s.add_dependency "rails", ">= 3.0.7"
18
- s.add_dependency "voight_kampff", '~>1.0.2'
16
+ s.required_rubygems_version = "> 2.1.0"
17
+
18
+ s.add_dependency "voight_kampff", '~> 1.1.2'
19
+
20
+ s.add_development_dependency "rails", '>= 3'
19
21
  s.add_development_dependency "sqlite3"
20
- s.add_development_dependency "rspec-rails", '~> 3.3.0'
21
- s.add_development_dependency "shoulda-matchers", '~> 3'
22
+ s.add_development_dependency "rspec-rails", '~> 3.6.0'
23
+ s.add_development_dependency "shoulda-matchers"
22
24
  s.add_development_dependency "faker"
23
25
  s.add_development_dependency "byebug"
26
+ s.add_development_dependency "appraisal"
27
+
24
28
  s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
25
29
  s.require_path = 'lib'
26
30
  end
@@ -8,7 +8,11 @@ describe Shortener::ShortenedUrlsController, type: :controller do
8
8
  describe '#show' do
9
9
  let(:params) { {} }
10
10
  before do
11
- get :show, { id: key }.merge(params)
11
+ if Rails::VERSION::MAJOR < 5
12
+ get :show, { id: key }.merge(params)
13
+ else
14
+ get :show, params: { id: key }.merge(params)
15
+ end
12
16
  end
13
17
 
14
18
  context 'valid keys' do
@@ -20,21 +24,28 @@ describe Shortener::ShortenedUrlsController, type: :controller do
20
24
  end
21
25
 
22
26
  context "when request is not from an human" do
27
+ let(:run_action!) do
28
+ if Rails::VERSION::MAJOR < 5
29
+ get :show, { id: key }
30
+ else
31
+ get :show, params: { id: key }
32
+ end
33
+ end
23
34
  before do
24
- allow_any_instance_of(Rack::Request).to receive(:human?).and_return(false)
35
+ allow_any_instance_of(ActionController::TestRequest).to receive(:human?).and_return(false)
25
36
  end
26
37
  context 'Shortener.ignore_robots == true' do
27
38
  it "calls fetch with token with track argument as false" do
28
39
  Shortener.ignore_robots = true
29
40
  expect(Shortener::ShortenedUrl).to receive(:fetch_with_token).with(hash_including(track: false)).and_call_original
30
- get :show, { id: key }
41
+ run_action!
31
42
  end
32
43
  end
33
44
  context 'Shortener.ignore_robots == false (default)' do
34
45
  it "calls fetch with token with track argument as true" do
35
46
  Shortener.ignore_robots = false
36
47
  expect(Shortener::ShortenedUrl).to receive(:fetch_with_token).with(hash_including(track: true)).and_call_original
37
- get :show, { id: key }
48
+ run_action!
38
49
  end
39
50
  end
40
51
  end
@@ -97,7 +108,11 @@ describe Shortener::ShortenedUrlsController, type: :controller do
97
108
  before do
98
109
  Shortener.default_redirect = 'http://www.default_redirect.com'
99
110
  # call again for the get is done with the setting
100
- get :show, id: key
111
+ if Rails::VERSION::MAJOR < 5
112
+ get :show, id: key
113
+ else
114
+ get :show, params: { id: key }
115
+ end
101
116
  end
102
117
 
103
118
  context 'non existant key' do
@@ -1,9 +1,9 @@
1
- class CreateShortenedUrlsTable < ActiveRecord::Migration
1
+ class CreateShortenedUrlsTable < ActiveRecord::Migration[4.2]
2
2
  def change
3
3
  create_table :shortened_urls do |t|
4
4
  # we can link this to a user for interesting things
5
5
  t.integer :owner_id
6
- t.string :owner_type, limit: 40
6
+ t.string :owner_type, limit: 20
7
7
 
8
8
  # the real url that we will redirect to
9
9
  t.text :url, null: false
@@ -1,4 +1,4 @@
1
- class CreateUsers < ActiveRecord::Migration
1
+ class CreateUsers < ActiveRecord::Migration[4.2]
2
2
  def change
3
3
  create_table :users do |t|
4
4
 
@@ -1,4 +1,4 @@
1
- class ChangeUrlFieldToTextOnShortenedUrl < ActiveRecord::Migration
1
+ class ChangeUrlFieldToTextOnShortenedUrl < ActiveRecord::Migration[4.2]
2
2
  # def up
3
3
  # change_column :shortened_urls, :url, :text
4
4
  # end
@@ -1,4 +1,4 @@
1
- class AddExpiresAtToShortenedUrl < ActiveRecord::Migration
1
+ class AddExpiresAtToShortenedUrl < ActiveRecord::Migration[4.2]
2
2
  # def change
3
3
  # add_column :shortened_urls, :expires_at, :datetime
4
4
  # end
@@ -1,4 +1,4 @@
1
- class AddCategoryToShortenedUrl < ActiveRecord::Migration
1
+ class AddCategoryToShortenedUrl < ActiveRecord::Migration[4.2]
2
2
  # def change
3
3
  # add_column :shortened_urls, :category, :string
4
4
  # add_index :shortened_urls, :category
@@ -122,15 +122,14 @@ describe Shortener::ShortenedUrl, type: :model do
122
122
  end
123
123
 
124
124
  context "duplicate unique key" do
125
- before do
126
- expect_any_instance_of(Shortener::ShortenedUrl).to receive(:generate_unique_key).
127
- and_return(existing_shortened_url.unique_key, 'ABCDEF')
128
- Shortener::ShortenedUrl.where(unique_key: 'ABCDEF').delete_all
129
- end
125
+ let(:duplicate_key) { 'ABCDEF' }
130
126
  it 'should try until it finds a non-dup key' do
131
- short_url = Shortener::ShortenedUrl.generate!(Faker::Internet.url)
127
+ Shortener::ShortenedUrl.where(unique_key: duplicate_key).delete_all
128
+ Shortener::ShortenedUrl.create!(url: Faker::Internet.url, custom_key: duplicate_key)
129
+ short_url = Shortener::ShortenedUrl.create!(url: Faker::Internet.url, unique_key: duplicate_key)
132
130
  expect(short_url).not_to be_nil
133
- expect(short_url.unique_key).to eq "ABCDEF"
131
+ expect(short_url.unique_key).not_to be_nil
132
+ expect(short_url.unique_key).not_to eq duplicate_key
134
133
  end
135
134
  end
136
135
  end
@@ -180,8 +179,7 @@ describe Shortener::ShortenedUrl, type: :model do
180
179
  it 'increments the use_count on the shortenedLink' do
181
180
  original_count = short_url.use_count
182
181
  short_url.increment_usage_count
183
- sleep 0.001
184
- expect(short_url.use_count).to eq (original_count + 1)
182
+ expect(short_url.reload.use_count).to eq (original_count + 1)
185
183
  end
186
184
  end
187
185
 
data/spec/spec_helper.rb CHANGED
@@ -18,4 +18,8 @@ Shoulda::Matchers.configure do |config|
18
18
  end
19
19
 
20
20
  # Run any available migration
21
- ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
21
+ if ActiveRecord::Migrator.respond_to?(:migrate)
22
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
23
+ else
24
+ ActiveRecord::MigrationContext.new(File.expand_path("../dummy/db/migrate/", __FILE__)).migrate
25
+ 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.6.2
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James P. McGrath
@@ -9,36 +9,36 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-04 00:00:00.000000000 Z
12
+ date: 2018-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rails
15
+ name: voight_kampff
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: 3.0.7
20
+ version: 1.1.2
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: 3.0.7
27
+ version: 1.1.2
28
28
  - !ruby/object:Gem::Dependency
29
- name: voight_kampff
29
+ name: rails
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 1.0.2
35
- type: :runtime
34
+ version: '3'
35
+ type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: 1.0.2
41
+ version: '3'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: sqlite3
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -59,28 +59,28 @@ dependencies:
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: 3.3.0
62
+ version: 3.6.0
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: 3.3.0
69
+ version: 3.6.0
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: shoulda-matchers
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - "~>"
74
+ - - ">="
75
75
  - !ruby/object:Gem::Version
76
- version: '3'
76
+ version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - "~>"
81
+ - - ">="
82
82
  - !ruby/object:Gem::Version
83
- version: '3'
83
+ version: '0'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: faker
86
86
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +109,20 @@ dependencies:
109
109
  - - ">="
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: appraisal
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
112
126
  description: Shortener is a Rails Engine Gem that makes it easy to create and interpret
113
127
  shortened URLs on your own domain from within your Rails application. Once installed
114
128
  Shortener will generate, store URLS and "unshorten" shortened URLs for your applications
@@ -122,6 +136,7 @@ extra_rdoc_files: []
122
136
  files:
123
137
  - ".gitignore"
124
138
  - ".travis.yml"
139
+ - Appraisals
125
140
  - Gemfile
126
141
  - MIT-LICENSE
127
142
  - README.rdoc
@@ -129,6 +144,10 @@ files:
129
144
  - app/controllers/shortener/shortened_urls_controller.rb
130
145
  - app/helpers/shortener/shortener_helper.rb
131
146
  - app/models/shortener/shortened_url.rb
147
+ - gemfiles/rails_4.gemfile
148
+ - gemfiles/rails_5.0.gemfile
149
+ - gemfiles/rails_5.1.gemfile
150
+ - gemfiles/rails_5.2.gemfile
132
151
  - lib/generators/shortener/shortener_generator.rb
133
152
  - lib/generators/shortener/templates/migration.rb
134
153
  - lib/shortener.rb
@@ -189,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
208
  requirements:
190
209
  - - ">"
191
210
  - !ruby/object:Gem::Version
192
- version: 1.3.6
211
+ version: 2.1.0
193
212
  requirements: []
194
213
  rubyforge_project: shortener
195
214
  rubygems_version: 2.5.1