darjeelink 0.13.2 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eeb3578591867e81f4e376b4e63bb2a76b2d332df30e6118d6beb8b56cd4224f
4
- data.tar.gz: 551f675eebe854975e8272c935de6e4e819a7c368d85ce652c03f6507bb8cc5d
3
+ metadata.gz: 96f614f236d3e7d9d03fe066719066ce2b4e1d3e5673fd0c78756bcdbfe0bbe6
4
+ data.tar.gz: b698bd03bf5d648650aa72b8056e47ee952211732dda6257837e1e2b87f21853
5
5
  SHA512:
6
- metadata.gz: '087f6e5ac60d715f64deb722f22fdb8f5aac81dec3f0f98547d6baee4fe9e83dbca59d4670577200f4dff6baff789d84f46492cd6a2187f0bc15b0bbd2023a29'
7
- data.tar.gz: cf1c9231dcef1503fb5279ff973854809ca3871f0598f124f9823f5ff9536569a3cc77d3fc72cb10c222690cba0ce154583d353c426eb29a54e81dab9c0509f2
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
@@ -4,6 +4,7 @@ module Darjeelink
4
4
  class ApiController < Darjeelink::ApplicationController
5
5
  skip_before_action :check_ip_whitelist
6
6
  skip_before_action :authenticate
7
+ skip_before_action :verify_authenticity_token
7
8
 
8
9
  before_action :authenticate_token
9
10
 
@@ -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.2'
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.2
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-10-10 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,14 +253,14 @@ 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
  - - ">="
260
260
  - !ruby/object:Gem::Version
261
261
  version: '0'
262
262
  requirements: []
263
- rubygems_version: 3.3.21
263
+ rubygems_version: 3.3.7
264
264
  signing_key:
265
265
  specification_version: 4
266
266
  summary: URL Shortener