tanshuku 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/tanshuku/url.rb +1 -19
- data/db/migrate/20230220123456_create_tanshuku_urls.rb +1 -1
- data/lib/tanshuku/configuration.rb +1 -78
- data/lib/tanshuku/version.rb +1 -1
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12ed0c4f9a99b9b6e2c988a8fe327cc491420b9586c3128dd6e2493f46819b0a
|
4
|
+
data.tar.gz: a566c0f82f98f54301b3ab91ce23dc09629ba2e5de79970b06b62ac13d54cf10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28d39d2e039b8916441f4bca51c130374002c416d9afd08836166ea354370bbe341be9df7b552e023e5f03c6abc08071493434b3bbb62df862391c55d7f82349
|
7
|
+
data.tar.gz: 0c7e3337b95a5e647e8cd44d2a61df7070ac9897537d047dd10aaff243cd227f3622e68f549152bfc94eaa9f6e34bd6732185fc4812c749e477550bc0ca6303b
|
data/app/models/tanshuku/url.rb
CHANGED
@@ -26,11 +26,10 @@ module Tanshuku
|
|
26
26
|
DEFAULT_NAMESPACE = ""
|
27
27
|
|
28
28
|
validates :url, :hashed_url, :key, presence: true
|
29
|
+
validates :url, length: { maximum: proc { Tanshuku.config.max_url_length } }
|
29
30
|
validates :url, format: { with: proc { Tanshuku.config.url_pattern } }, allow_blank: true
|
30
31
|
|
31
32
|
# Don’t validate uniqueness of unique attributes. Raise ActiveRecord::RecordNotUnique instead if the attributes get
|
32
|
-
validate :validates_length_of_url
|
33
|
-
|
34
33
|
# duplicated. Then rescue the exception and try to retry.
|
35
34
|
# validates :url, :hashed_url, :key, uniqueness: true
|
36
35
|
|
@@ -104,15 +103,6 @@ module Tanshuku
|
|
104
103
|
original_url
|
105
104
|
end
|
106
105
|
|
107
|
-
unless respond_to?(:create_or_find_by!)
|
108
|
-
# https://github.com/rails/rails/blob/v6.0.0/activerecord/lib/active_record/relation.rb#L218-L222
|
109
|
-
def self.create_or_find_by!(attributes, &block)
|
110
|
-
transaction(requires_new: true) { create!(attributes, &block) }
|
111
|
-
rescue ActiveRecord::RecordNotUnique
|
112
|
-
find_by!(attributes)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
106
|
# Finds a {Tanshuku::Url} record by a non-shortened URL.
|
117
107
|
#
|
118
108
|
# @param url [String] A non-shortened URL.
|
@@ -185,14 +175,6 @@ module Tanshuku
|
|
185
175
|
|
186
176
|
Tanshuku::Engine.routes.url_for(url_options)
|
187
177
|
end
|
188
|
-
|
189
|
-
private
|
190
|
-
|
191
|
-
def validates_length_of_url
|
192
|
-
if url && url.length > Tanshuku.config.max_url_length
|
193
|
-
errors.add(:url, :too_long, count: Tanshuku.config.max_url_length)
|
194
|
-
end
|
195
|
-
end
|
196
178
|
end
|
197
179
|
# rubocop:enable Rails/ApplicationRecord
|
198
180
|
end
|
@@ -42,84 +42,7 @@ module Tanshuku
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
|
46
|
-
include ActiveModel::Attributes
|
47
|
-
else
|
48
|
-
# https://github.com/rails/rails/blob/v5.2.8.1/activemodel/lib/active_model/attributes.rb
|
49
|
-
# https://github.com/rails/rails/blob/v5.2.8.1/activemodel/lib/active_model/type/integer.rb
|
50
|
-
concerning :PetitModelAttributes do
|
51
|
-
const_set :NO_DEFAULT_PROVIDED, Object.new
|
52
|
-
|
53
|
-
class_methods do
|
54
|
-
# rubocop:disable Metrics/MethodLength
|
55
|
-
|
56
|
-
# A petit and limited implementation of `ActiveModel::Attributes`'s `attribute` method for backward
|
57
|
-
# compatibility.
|
58
|
-
#
|
59
|
-
# @param name [Symbol] An attribute name.
|
60
|
-
# @param cast_type [Symbol,nil] A type to cast the attribute's value.
|
61
|
-
# @param default [Object] A default value of the attribute.
|
62
|
-
#
|
63
|
-
# @return [void]
|
64
|
-
def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED)
|
65
|
-
attr_reader name
|
66
|
-
|
67
|
-
case cast_type
|
68
|
-
when nil
|
69
|
-
attr_writer name
|
70
|
-
when :integer
|
71
|
-
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
72
|
-
def #{name}=(value) # def max_url_length=(value)
|
73
|
-
@#{name} = # @max_url_length =
|
74
|
-
case value # case value
|
75
|
-
when true # when true
|
76
|
-
1 # 1
|
77
|
-
when false # when false
|
78
|
-
0 # 0
|
79
|
-
when nil # when nil
|
80
|
-
nil # nil
|
81
|
-
else # else
|
82
|
-
value.to_i rescue nil # value.to_i rescue nil
|
83
|
-
end # end
|
84
|
-
end # end
|
85
|
-
RUBY
|
86
|
-
else
|
87
|
-
raise ArgumentError, "Non-supported cast type: #{cast_type.inspect}"
|
88
|
-
end
|
89
|
-
|
90
|
-
return if default == NO_DEFAULT_PROVIDED
|
91
|
-
|
92
|
-
mod =
|
93
|
-
Module.new do
|
94
|
-
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
95
|
-
def initialize(*args) # def initialize(*args)
|
96
|
-
super # super
|
97
|
-
self.#{name} = #{default.inspect} # self.max_url_length = 10_000
|
98
|
-
end # end
|
99
|
-
RUBY
|
100
|
-
end
|
101
|
-
prepend mod
|
102
|
-
end
|
103
|
-
# rubocop:enable Metrics/MethodLength
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
# @!attribute [rw] default_url_options
|
109
|
-
# Default URL options for Rails’ +url_for+. Defaults to +{}+.
|
110
|
-
#
|
111
|
-
# @return [Hash]
|
112
|
-
# @return [void] If you set an invalid object.
|
113
|
-
#
|
114
|
-
# @note
|
115
|
-
# The example below means that the configured host and protocol are used. Shortened URLs will be like
|
116
|
-
# +https://example.com/t/abcdefghij0123456789+.
|
117
|
-
#
|
118
|
-
# @example
|
119
|
-
# # config/initializers/tanshuku.rb
|
120
|
-
# Tanshuku.configure do |config|
|
121
|
-
# config.default_url_options = { host: "example.com", protocol: :https }
|
122
|
-
# end
|
45
|
+
include ActiveModel::Attributes
|
123
46
|
attribute :default_url_options, default: {}
|
124
47
|
|
125
48
|
# @!attribute [rw] max_url_length
|
data/lib/tanshuku/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tanshuku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kg8m
|
@@ -30,20 +30,20 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '6.0'
|
34
34
|
- - "<"
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: '
|
36
|
+
version: '7.0'
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
41
|
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: '
|
43
|
+
version: '6.0'
|
44
44
|
- - "<"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
46
|
+
version: '7.0'
|
47
47
|
description: Tanshuku is a simple and performance aware Rails engine for shortening
|
48
48
|
URLs. Tanshuku generates a shortened URL per a normalized original URL. Tanshuku
|
49
49
|
redirects from a shortened URL to its corresponding original URL.
|
@@ -85,17 +85,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
85
|
requirements:
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version: '
|
88
|
+
version: '3.0'
|
89
89
|
- - "<"
|
90
90
|
- !ruby/object:Gem::Version
|
91
|
-
version: '3.
|
91
|
+
version: '3.1'
|
92
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
|
-
rubygems_version: 3.
|
98
|
+
rubygems_version: 3.2.33
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: Tanshuku is a simple and performance aware Rails engine for shortening URLs.
|