tanshuku 0.0.10 → 0.0.12

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: a8126e6ece1367088566945159c232716b022d71d30dcf33ca6689c5433a9850
4
- data.tar.gz: b9cfd16613a396e9e2ab36811ca41c9a98a80b6f045f3bd3a3fb1938dc9b69e3
3
+ metadata.gz: 06377e7737ccb49d4080ce2a728f4ba5ca98d00bd14f85838e3e888c899327d1
4
+ data.tar.gz: 63065a557df15b3c6ca45b9af168a9562a8aae1f9a41bb7b3db7a2ad9adaa82f
5
5
  SHA512:
6
- metadata.gz: 372e5a161e2d4ece17641df2978c50defc764eb8db53114e10469da15ddce2a1b52d25d452299c240d384b39df50bd705153246cb5201a4ffb5d6988dcf077c6
7
- data.tar.gz: c6fe73ec40e14f78e9af4b2838f2b895970ea3ada9df65932125db326358eebfc52d62cace498027fb5cf2df589dbe34ca63852610c5dd15e486155129c92e90
6
+ metadata.gz: 5bab0a2c9c7b26ec1f34ef02d465ecea40435e890e6381a6e3140850e5738a803141906f5bb6ca2b2ff715935d5380eb3119b66f556ce231d8f584b21d5f66d3
7
+ data.tar.gz: 14d22e0bb7de15a6c37de42e314be5521f2e3ff77bd8511cacfd1cf0847791b4a234c98e459a01e1443f52990672b9be636828f90a3aaf10fb67bc6d4fb9e8f6
@@ -8,6 +8,8 @@ require "securerandom"
8
8
 
9
9
  module Tanshuku
10
10
  class Url < ActiveRecord::Base
11
+ DEFAULT_NAMESPACE = ""
12
+
11
13
  MAX_URL_LENGTH = 10_000
12
14
  URL_PATTERN = %r{\A(?:https?://\w+|/)}
13
15
  KEY_LENGTH = 20
@@ -20,7 +22,7 @@ module Tanshuku
20
22
  # duplicated. Then rescue the exception and try to retry.
21
23
  # validates :url, :hashed_url, :key, uniqueness: true
22
24
 
23
- def self.shorten(original_url)
25
+ def self.shorten(original_url, namespace: DEFAULT_NAMESPACE, url_options: {})
24
26
  raise ArgumentError, "original_url should be present" unless original_url
25
27
 
26
28
  url = normalize_url(original_url)
@@ -29,11 +31,11 @@ module Tanshuku
29
31
  begin
30
32
  transaction do
31
33
  record =
32
- create_or_find_by!(hashed_url: hash_url(url)) do |r|
34
+ create_or_find_by!(hashed_url: hash_url(url, namespace:)) do |r|
33
35
  r.attributes = { url:, key: generate_key }
34
36
  end
35
37
 
36
- record.shortened_url
38
+ record.shortened_url(url_options)
37
39
  end
38
40
  # ActiveRecord::RecordNotFound is raised when the key is duplicated.
39
41
  rescue ActiveRecord::RecordNotFound => e
@@ -50,9 +52,9 @@ module Tanshuku
50
52
  original_url
51
53
  end
52
54
 
53
- def self.find_by_url(url)
55
+ def self.find_by_url(url, namespace: DEFAULT_NAMESPACE)
54
56
  normalized_url = normalize_url(url)
55
- hashed_url = hash_url(normalized_url)
57
+ hashed_url = hash_url(normalized_url, namespace:)
56
58
 
57
59
  find_by(hashed_url:)
58
60
  end
@@ -64,8 +66,8 @@ module Tanshuku
64
66
  parsed_url.normalize.to_s
65
67
  end
66
68
 
67
- def self.hash_url(url)
68
- Digest::SHA512.hexdigest(url)
69
+ def self.hash_url(url, namespace: DEFAULT_NAMESPACE)
70
+ Digest::SHA512.hexdigest(namespace.to_s + url)
69
71
  end
70
72
 
71
73
  def self.generate_key
@@ -73,11 +75,16 @@ module Tanshuku
73
75
  end
74
76
 
75
77
  def self.report_exception(exception:, original_url:)
76
- logger.warn("Tanshuku - Failed to shorten a URL: #{exception.inspect} for #{original_url.inspect}")
78
+ Tanshuku.config.exception_reporter.call(exception:, original_url:)
77
79
  end
78
80
 
79
- def shortened_url
80
- Tanshuku::Engine.routes.url_for(controller: "tanshuku/urls", action: :show, key:)
81
+ def shortened_url(url_options = {})
82
+ url_options = url_options.symbolize_keys
83
+ url_options[:controller] = "tanshuku/urls"
84
+ url_options[:action] = :show
85
+ url_options[:key] = key
86
+
87
+ Tanshuku::Engine.routes.url_for(url_options)
81
88
  end
82
89
  end
83
90
  end
data/config/routes.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Tanshuku::Engine.routes.draw do
4
- default_url_options Tanshuku.default_url_options
4
+ default_url_options Tanshuku.config.default_url_options
5
5
 
6
6
  get "/:key", to: "urls#show"
7
7
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tanshuku
4
+ class Configuration
5
+ module DefaultExceptionReporter
6
+ def self.call(exception:, original_url:)
7
+ Rails.logger.warn("Tanshuku - Failed to shorten a URL: #{exception.inspect} for #{original_url.inspect}")
8
+ end
9
+ end
10
+
11
+ include ActiveModel::Attributes
12
+
13
+ attribute :default_url_options, default: {}
14
+ attribute :exception_reporter, default: DefaultExceptionReporter
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tanshuku
4
- VERSION = "0.0.10"
4
+ VERSION = "0.0.12"
5
5
  end
data/lib/tanshuku.rb CHANGED
@@ -1,8 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "tanshuku/configuration"
3
4
  require_relative "tanshuku/engine"
4
5
  require_relative "tanshuku/version"
5
6
 
6
7
  module Tanshuku
7
- mattr_accessor :default_url_options, default: {}
8
+ def self.config
9
+ Mutex.new.synchronize do
10
+ @config ||= Configuration.new
11
+ end
12
+ end
13
+
14
+ def self.configure
15
+ yield config
16
+ end
8
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tanshuku
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - kg8m
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-25 00:00:00.000000000 Z
11
+ date: 2023-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -56,6 +56,7 @@ files:
56
56
  - config/routes.rb
57
57
  - db/migrate/20230220123456_create_tanshuku_urls.rb
58
58
  - lib/tanshuku.rb
59
+ - lib/tanshuku/configuration.rb
59
60
  - lib/tanshuku/engine.rb
60
61
  - lib/tanshuku/version.rb
61
62
  homepage: https://github.com/kg8m/tanshuku
@@ -81,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  requirements: []
84
- rubygems_version: 3.4.1
85
+ rubygems_version: 3.4.6
85
86
  signing_key:
86
87
  specification_version: 4
87
88
  summary: Tanshuku is a simple and performance aware Rails engine for shortening URLs.