darjeelink 0.13.3 → 0.14.0

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
  SHA256:
3
- metadata.gz: 33acf43d8f9a2344bb3e9ea0d802232776c616f41461351fdf6ca1853acc46ca
4
- data.tar.gz: ff59ffc23185354d3b1e83f819f1700d888e8100409dce52437cc3ceb1aefd59
3
+ metadata.gz: 96f614f236d3e7d9d03fe066719066ce2b4e1d3e5673fd0c78756bcdbfe0bbe6
4
+ data.tar.gz: b698bd03bf5d648650aa72b8056e47ee952211732dda6257837e1e2b87f21853
5
5
  SHA512:
6
- metadata.gz: 0e2fbc92bf253a980681d2614d74c3c5eb86f02e8f6078d50eefce580866906e9e803c9a83f215a1d2329b1ae5af74c7a194dcb45cbf69dadd0a5c14d61fd393
7
- data.tar.gz: 41bf0de36f7baec2b7864f64522ee7ddcf3ebb0c9cb6f1bd997a58098feda5b54c2ae67b2d98b7b42faa16028455cb3e86814d494733a82a6a6465cac9810f21
6
+ metadata.gz: 76068de4b9ac74d9bce9e2255cc9b28637dc3d32dcd8409d1b12598f14cb92508abf730b8f2a693873b24b485e7202708e9b8ad8639e88f921054f8841559421
7
+ data.tar.gz: 7f56e2b13c2f9041205e65e792a9b9afb72779d516bed82dc652379a1ff482c94f515627681e065a4116c5a8c51355029bf4b2b07faa71fcd92f3becdc2fdc55
data/README.md CHANGED
@@ -101,7 +101,10 @@ Each key is a hyphenated source-medium. If you just want the source then omit t
101
101
  Each value is a slightly more readable version for display.
102
102
 
103
103
  ## Development
104
- The recommended approach is to use Vagrant. `vagrant up` will create an isolated darjeelink instance.
104
+
105
+ Docker can be used to run tests easily - 'docker-compose up' should run the unit tests.
106
+
107
+ Another approach is to use Vagrant. `vagrant up` will create an isolated darjeelink instance.
105
108
  Before you run `vagrant up`, make sure to create `.env.development` & `.env.test` files as detailed below.
106
109
 
107
110
  ### Setup development environment
@@ -60,7 +60,7 @@ module Darjeelink
60
60
  begin
61
61
  yield
62
62
  rescue ActiveRecord::RecordNotUnique
63
- flash[:error] = "#{params[:shortened_path]} already used! Choose a different custom path"
63
+ flash[:error] = "#{short_link_params[:shortened_path]} already used! Choose a different custom path"
64
64
  return redirect_to(error_redirect_path)
65
65
  rescue ActiveRecord::RecordInvalid => e
66
66
  flash[:error] = e.message.to_s
@@ -1,8 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'securerandom'
4
+
3
5
  module Darjeelink
4
6
  class ShortLink < ApplicationRecord
5
- after_save :generate_short_link
7
+ # ActiveRecord::RecordNotUnique error unable to be raised in a transaction that is not committed
8
+ after_commit :generate_short_link
9
+
10
+ def self.auto_generate_shortened_path
11
+ # our current db has a case insensitive constraint so we might as well downcase here before we get to db level
12
+ pp SecureRandom.urlsafe_base64(3).downcase
13
+ end
6
14
 
7
15
  validates_presence_of :url
8
16
  validates :url, :shortened_path, format: {
@@ -31,26 +39,14 @@ module Darjeelink
31
39
  def generate_short_link
32
40
  return if shortened_path.present?
33
41
 
34
- update!(shortened_path: auto_generate_shortened_path)
35
- end
36
-
37
- ALPHABET = (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + ['-', '_']).freeze
38
-
39
- def auto_generate_shortened_path
40
- # from http://refactormycode.com/codes/125-base-62-encoding
41
- # with only minor modification
42
- i = id
43
- return ALPHABET[0] if i.zero?
44
-
45
- generated_path = []
46
- base = ALPHABET.length
47
-
48
- while i.positive?
49
- generated_path << ALPHABET[i.modulo(base)]
50
- i /= base
42
+ begin
43
+ attempt ||= 0
44
+ update!(shortened_path: self.class.auto_generate_shortened_path)
45
+ rescue ActiveRecord::RecordNotUnique
46
+ # we only want to try 5 times to prevent infinite loop
47
+ attempt += 1
48
+ attempt <= 5 ? retry : raise
51
49
  end
52
-
53
- generated_path.join.reverse
54
50
  end
55
51
  end
56
52
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Darjeelink
4
- VERSION = '0.13.3'
4
+ VERSION = '0.14.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darjeelink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.3
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Hulme
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-24 00:00:00.000000000 Z
11
+ date: 2023-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth
@@ -253,7 +253,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
253
253
  requirements:
254
254
  - - ">="
255
255
  - !ruby/object:Gem::Version
256
- version: '3.1'
256
+ version: 3.1.2
257
257
  required_rubygems_version: !ruby/object:Gem::Requirement
258
258
  requirements:
259
259
  - - ">="