tanshuku 0.0.10 → 0.0.11

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: 58228d834716ae9f1a9415212f4976816aeaf61bd6ff2a1e92672a62bd202a93
4
+ data.tar.gz: e8ecd16d19d73cdff428f08cd5a3b801e59934fa86d69fa1b182e0da40da1feb
5
5
  SHA512:
6
- metadata.gz: 372e5a161e2d4ece17641df2978c50defc764eb8db53114e10469da15ddce2a1b52d25d452299c240d384b39df50bd705153246cb5201a4ffb5d6988dcf077c6
7
- data.tar.gz: c6fe73ec40e14f78e9af4b2838f2b895970ea3ada9df65932125db326358eebfc52d62cace498027fb5cf2df589dbe34ca63852610c5dd15e486155129c92e90
6
+ metadata.gz: f786879cc78304a288487079e4b2e218be8df2d7c6df4edc8f629ca672369fa583998e2b44bc4d62876b1f242dd24ae0f67201a1a85f391d20fb26dd07d80ca1
7
+ data.tar.gz: 2b7af5d5181be59774b1d6ae13e12df32347c862cec776139a4ba31da153a38fb63959380e8bebcc931fa707b658799b33eb5a248a7bf765ba77dc9de3397bf9
@@ -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)
24
26
  raise ArgumentError, "original_url should be present" unless original_url
25
27
 
26
28
  url = normalize_url(original_url)
@@ -29,7 +31,7 @@ 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
 
@@ -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,7 +75,7 @@ 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
81
  def shortened_url
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.11"
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.11
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-10 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.