tanshuku 0.0.12 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +177 -0
- data/Rakefile +20 -0
- data/app/controllers/tanshuku/urls_controller.rb +6 -0
- data/app/models/tanshuku/url.rb +76 -1
- data/lib/generators/tanshuku/install_generator.rb +27 -0
- data/lib/generators/templates/initializer.rb +14 -0
- data/lib/tanshuku/configuration.rb +61 -0
- data/lib/tanshuku/engine.rb +13 -0
- data/lib/tanshuku/version.rb +1 -1
- data/lib/tanshuku.rb +29 -5
- data/tmp/.keep +0 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b280080f53f8fb1f57a0b05cd1f4549f81a8c5c6a893501eec056bf0ff49e894
|
4
|
+
data.tar.gz: c2dea1c5782731a81c2f961186211cddff5c9494be925f1d395a53cea17a11c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c96f4614d77af55d6a3889956967ab5244819071a0ec77d415df5af76fd288eefc0554cbb32b661ea7349fd5e8175201e02fe5309ec8dd53634eefa30508cc5
|
7
|
+
data.tar.gz: 9f8ed89f16d6531f60303f03feded46ff98a35f5319bd6ec7b77e5526664fd830dad0da8eec51e8fbd7432d87702d88d34df9b1db1fdc03a4a04a006d87af3d2
|
data/README.md
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
# Tanshuku
|
2
|
+
|
3
|
+
Tanshuku is a simple and small Rails engine that makes it easier to shorten URLs.
|
4
|
+
|
5
|
+
## Key Features
|
6
|
+
|
7
|
+
* Generates a unique key for a URL.
|
8
|
+
* The uniqueness is ensured at database level.
|
9
|
+
* Creates no additional shortened URL record if the given URL has already been shortened.
|
10
|
+
* You can create an additional record for the same URL with using a different namespace from existing records'.
|
11
|
+
* Checks its existence considering the performance.
|
12
|
+
* Provides a Rails controller and action for finding a shortened URL record and redirecting to its original, i.e., non-shortened, URL.
|
13
|
+
* The redirection is done with 301 HTTP status.
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
### 1. Mount `Tanshuku::Engine`
|
18
|
+
|
19
|
+
For example, the following code generates a routing `` GET `/t/:key` to `Tanshuku::UrlsController#show` ``. When your Rails app receives a request to `/t/abcdefghij0123456789`, `Tanshuku::UrlsController#show` will be called and a `Tanshuku::Url` record with a key `abcdefghij0123456789` will be found. Then the request will be redirected to the `Tanshuku::Url` record's original URL.
|
20
|
+
|
21
|
+
```rb
|
22
|
+
# config/routes.rb
|
23
|
+
Rails.application.routes.draw do
|
24
|
+
mount Tanshuku::Engine, at: "/t"
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
**Note**: You can customize the path `/t` as you like.
|
29
|
+
|
30
|
+
### 2. Configure Tanshuku (Optional)
|
31
|
+
|
32
|
+
**Note**: This step is optional.
|
33
|
+
|
34
|
+
**Note**: An initializer file for configuration can be generated by `bin/rails generate tanshuku:install`. See the "[Installation](#installation)" section below for more information.
|
35
|
+
|
36
|
+
**Note**: Mutating a `Tanshuku::Configuration` object is thread-***unsafe***. It is recommended to use `Tanshuku.configure` for configuration.
|
37
|
+
|
38
|
+
#### `config.default_url_options`
|
39
|
+
|
40
|
+
Tanshuku uses configured `config.default_url_options` when generating shortened URLs.
|
41
|
+
|
42
|
+
Default value is `{}`.
|
43
|
+
|
44
|
+
The following example means that the configured host and protocol are used. Shortened URLs will be like `https://example.com/t/abcdefghij0123456789`.
|
45
|
+
|
46
|
+
```rb
|
47
|
+
# config/initializers/tanshuku.rb
|
48
|
+
Tanshuku.configure do |config|
|
49
|
+
config.default_url_options = { host: "example.com", protocol: :https }
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
#### `config.exception_reporter`
|
54
|
+
|
55
|
+
If an exception occurs when shortening a URL, Tanshuku reports it with configured `config.exception_reporter` object.
|
56
|
+
|
57
|
+
Default value is [`Tanshuku::Configuration::DefaultExceptionReporter`](https://kg8m.github.io/tanshuku/Tanshuku/Configuration/DefaultExceptionReporter.html). It logs the exception and the original URL with `Rails.logger.warn`.
|
58
|
+
|
59
|
+
Value of `config.exception_reporter` should respond to `#call` with keyword arguments `exception:` and `original_url:`.
|
60
|
+
|
61
|
+
The following example means that an exception and a URL will be reported to [Sentry](https://sentry.io/).
|
62
|
+
|
63
|
+
```rb
|
64
|
+
# config/initializers/tanshuku.rb
|
65
|
+
Tanshuku.configure do |config|
|
66
|
+
config.exception_reporter =
|
67
|
+
lambda { |exception:, original_url:|
|
68
|
+
Sentry.capture_exception(exception, tags: { original_url: })
|
69
|
+
}
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
#### More information
|
74
|
+
|
75
|
+
cf. [`Tanshuku::Configuration`'s API documentation](https://kg8m.github.io/tanshuku/Tanshuku/Configuration.html)
|
76
|
+
|
77
|
+
### 3. Generate shortened URLs
|
78
|
+
|
79
|
+
#### Basic cases
|
80
|
+
|
81
|
+
You can generate a shortened URL with `Tanshuku::Url.shorten`. For example:
|
82
|
+
|
83
|
+
```rb
|
84
|
+
Tanshuku::Url.shorten("https://google.com/") #=> "https://example.com/t/abcdefghij0123456789"
|
85
|
+
```
|
86
|
+
|
87
|
+
[`config.default_url_options`](https://kg8m.github.io/tanshuku/Tanshuku/Configuration.html#default_url_options-instance_method) is used for the shortened URL.
|
88
|
+
|
89
|
+
**Note**: If a `Tanshuku::Url` record for the given URL already exists, no additional record will be created and always the existing record is used.
|
90
|
+
|
91
|
+
```rb
|
92
|
+
# When no record exists for "https://google.com/", a new record will be created.
|
93
|
+
Tanshuku::Url.shorten("https://google.com/") #=> "https://example.com/t/abcde0123456789fghij"
|
94
|
+
|
95
|
+
# When a record already exists for "https://google.com/", no additional record will be created.
|
96
|
+
Tanshuku::Url.shorten("https://google.com/") #=> "https://example.com/t/abcde0123456789fghij"
|
97
|
+
Tanshuku::Url.shorten("https://google.com/") #=> "https://example.com/t/abcde0123456789fghij"
|
98
|
+
Tanshuku::Url.shorten("https://google.com/") #=> "https://example.com/t/abcde0123456789fghij"
|
99
|
+
```
|
100
|
+
|
101
|
+
#### Shortening a URL with ad hoc URL options
|
102
|
+
|
103
|
+
You can specify URL options to `Tanshuku::Url.shorten`. For example:
|
104
|
+
|
105
|
+
```rb
|
106
|
+
Tanshuku::Url.shorten("https://google.com/", url_options: { host: "verycool.example.com" })
|
107
|
+
#=> "https://verycool.example.com/t/0123456789abcdefghij"
|
108
|
+
|
109
|
+
Tanshuku::Url.shorten("https://google.com/", url_options: { protocol: :http })
|
110
|
+
#=> "http://example.com/t/abcde01234fghij56789"
|
111
|
+
```
|
112
|
+
|
113
|
+
#### Shortening a URL with a namespace
|
114
|
+
|
115
|
+
You can create additional records for the same URL with specifying a namespace.
|
116
|
+
|
117
|
+
```rb
|
118
|
+
# When no record exists for "https://google.com/", a new record will be created.
|
119
|
+
Tanshuku::Url.shorten("https://google.com/") #=> "https://example.com/t/abc012def345ghi678j9"
|
120
|
+
|
121
|
+
# Even when a record already exists for "https://google.com/", an additional record will be created if namespace is
|
122
|
+
# specified.
|
123
|
+
Tanshuku::Url.shorten("https://google.com/", namespace: "a") #=> "https://example.com/t/ab01cd23ef45gh67ij89"
|
124
|
+
Tanshuku::Url.shorten("https://google.com/", namespace: "b") #=> "https://example.com/t/a0b1c2d3e4f5g6h7i8j9"
|
125
|
+
Tanshuku::Url.shorten("https://google.com/", namespace: "c") #=> "https://example.com/t/abcd0123efgh4567ij89"
|
126
|
+
|
127
|
+
# When the same URL and the same namespace is specified, no additional record will be created.
|
128
|
+
Tanshuku::Url.shorten("https://google.com/", namespace: "a") #=> "https://example.com/t/ab01cd23ef45gh67ij89"
|
129
|
+
Tanshuku::Url.shorten("https://google.com/", namespace: "a") #=> "https://example.com/t/ab01cd23ef45gh67ij89"
|
130
|
+
```
|
131
|
+
|
132
|
+
#### More information
|
133
|
+
|
134
|
+
cf. [`Tanshuku::Url.shorten`'s API documentation](https://kg8m.github.io/tanshuku/Tanshuku/Url.html#shorten-class_method)
|
135
|
+
|
136
|
+
### 4. Share the shortened URLs
|
137
|
+
|
138
|
+
You can share the shortened URLs, e.g., `https://example.com/t/abcdefghij0123456789`.
|
139
|
+
|
140
|
+
When a user clicks a link with a shortened URL, your Rails app redirects the user to its original URL.
|
141
|
+
|
142
|
+
## Installation
|
143
|
+
|
144
|
+
### 1. Enable Tanshuku
|
145
|
+
|
146
|
+
Add `gem "tanshuku"` to your application's `Gemfile`.
|
147
|
+
|
148
|
+
```rb
|
149
|
+
# Gemfile
|
150
|
+
gem "tanshuku"
|
151
|
+
```
|
152
|
+
|
153
|
+
### 2. Generate setup files
|
154
|
+
|
155
|
+
Execute a shell command as following:
|
156
|
+
|
157
|
+
```sh
|
158
|
+
bin/rails generate tanshuku:install
|
159
|
+
```
|
160
|
+
|
161
|
+
### 3. Apply generated migration files
|
162
|
+
|
163
|
+
Execute a shell command as following:
|
164
|
+
|
165
|
+
```sh
|
166
|
+
bin/rails db:migrate
|
167
|
+
```
|
168
|
+
|
169
|
+
## Q&A
|
170
|
+
|
171
|
+
### What does "tanshuku" mean?
|
172
|
+
|
173
|
+
"Tanshuku" is a Japanese word "短縮." It means "shortening." "短縮URL" in Japanese means "shortened URL" in English.
|
174
|
+
|
175
|
+
### \*\* (anything you want) isn't implemented?
|
176
|
+
|
177
|
+
Does Tanshuku have some missing features? Please [create an issue](https://github.com/kg8m/tanshuku/issues/new).
|
data/Rakefile
CHANGED
@@ -8,3 +8,23 @@ load "rails/tasks/engine.rake"
|
|
8
8
|
load "rails/tasks/statistics.rake"
|
9
9
|
|
10
10
|
require "bundler/gem_tasks"
|
11
|
+
require "rspec/core/rake_task"
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RSpec::Core::RakeTask.new(:spec)
|
15
|
+
RuboCop::RakeTask.new
|
16
|
+
|
17
|
+
namespace :yard do
|
18
|
+
desc "Start YARD server"
|
19
|
+
task :server do
|
20
|
+
puts "See http://localhost:8808/"
|
21
|
+
sh "yard server --reload"
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Generate YARD docs"
|
25
|
+
task :generate do
|
26
|
+
sh "yard --output-dir docs"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
task default: %i[rubocop spec]
|
@@ -1,7 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Tanshuku
|
4
|
+
# A Rails controller class for finding a {Tanshuku::Url} record and redirecting to its shortened URL.
|
4
5
|
class UrlsController < ActionController::API
|
6
|
+
# Finds a {Tanshuku::Url} record from the given +key+ parameter and redirects to its shortened URL.
|
7
|
+
#
|
8
|
+
# @return [void]
|
9
|
+
#
|
10
|
+
# @raise [ActiveRecord::NotFound] If no {Tanshuku::Url} record is found for the given +key+.
|
5
11
|
def show
|
6
12
|
url = Url.find_by!(key: params[:key])
|
7
13
|
redirect_to url.url, status: :moved_permanently, allow_other_host: true
|
data/app/models/tanshuku/url.rb
CHANGED
@@ -7,6 +7,7 @@ require "rack"
|
|
7
7
|
require "securerandom"
|
8
8
|
|
9
9
|
module Tanshuku
|
10
|
+
# An +ActiveRecord::Base+ inherited class for a shortened URL. This class also have some logics for shortening URLs.
|
10
11
|
class Url < ActiveRecord::Base
|
11
12
|
DEFAULT_NAMESPACE = ""
|
12
13
|
|
@@ -22,6 +23,46 @@ module Tanshuku
|
|
22
23
|
# duplicated. Then rescue the exception and try to retry.
|
23
24
|
# validates :url, :hashed_url, :key, uniqueness: true
|
24
25
|
|
26
|
+
# Shortens a URL. Builds and saves a {Tanshuku::Url} record with generating a unique key for the given URL and
|
27
|
+
# namespace. Then returns the record's shortened URL with the given URL options.
|
28
|
+
#
|
29
|
+
# @note
|
30
|
+
# If a {Tanshuku::Url} record already exists, no additional record will be created and the existing record will be
|
31
|
+
# used.
|
32
|
+
# @note
|
33
|
+
# The given URL will be normalized before shortening. So for example, +shorten("https://google.com/")+ and
|
34
|
+
# +shorten("https://google.com")+ have the same result.
|
35
|
+
#
|
36
|
+
# @param original_url [String] The original, i.e., non-shortened, URL.
|
37
|
+
# @param namespace: [String] A namespace for shorteting URL. Shortened URLs are unique in namespaces.
|
38
|
+
# @param url_options: [Hash] An option for Rails' +url_for+.
|
39
|
+
#
|
40
|
+
# @return [String] A shortened URL if succeeded to shorten the original URL.
|
41
|
+
# @return [String] The original URL if failed to shorten it.
|
42
|
+
#
|
43
|
+
# @example If succeeded to shorten a URL.
|
44
|
+
# Tanshuku::Url.shorten("https://google.com/") #=> "http://localhost/t/abcdefghij0123456789"
|
45
|
+
# @example If failed to shorten a URL.
|
46
|
+
# Tanshuku::Url.shorten("https://google.com/") #=> "https://google.com/"
|
47
|
+
# @example With ad hoc URL options.
|
48
|
+
# Tanshuku::Url.shorten("https://google.com/", url_options: { host: "verycool.example.com" })
|
49
|
+
# #=> "https://verycool.example.com/t/0123456789abcdefghij"
|
50
|
+
#
|
51
|
+
# Tanshuku::Url.shorten("https://google.com/", url_options: { protocol: :http })
|
52
|
+
# #=> "http://example.com/t/abcde01234fghij56789"
|
53
|
+
# @example With a namespace.
|
54
|
+
# # When no record exists for "https://google.com/", a new record will be created.
|
55
|
+
# Tanshuku::Url.shorten("https://google.com/") #=> "https://example.com/t/abc012def345ghi678j9"
|
56
|
+
#
|
57
|
+
# # Even when a record already exists for "https://google.com/", an additional record will be created if namespace
|
58
|
+
# # is specified.
|
59
|
+
# Tanshuku::Url.shorten("https://google.com/", namespace: "a") #=> "https://example.com/t/ab01cd23ef45gh67ij89"
|
60
|
+
# Tanshuku::Url.shorten("https://google.com/", namespace: "b") #=> "https://example.com/t/a0b1c2d3e4f5g6h7i8j9"
|
61
|
+
# Tanshuku::Url.shorten("https://google.com/", namespace: "c") #=> "https://example.com/t/abcd0123efgh4567ij89"
|
62
|
+
#
|
63
|
+
# # When the same URL and the same namespace is specified, no additional record will be created.
|
64
|
+
# Tanshuku::Url.shorten("https://google.com/", namespace: "a") #=> "https://example.com/t/ab01cd23ef45gh67ij89"
|
65
|
+
# Tanshuku::Url.shorten("https://google.com/", namespace: "a") #=> "https://example.com/t/ab01cd23ef45gh67ij89"
|
25
66
|
def self.shorten(original_url, namespace: DEFAULT_NAMESPACE, url_options: {})
|
26
67
|
raise ArgumentError, "original_url should be present" unless original_url
|
27
68
|
|
@@ -52,6 +93,13 @@ module Tanshuku
|
|
52
93
|
original_url
|
53
94
|
end
|
54
95
|
|
96
|
+
# Finds a {Tanshuku::Url} record by a non-shortened URL.
|
97
|
+
#
|
98
|
+
# @param url [String] A non-shortened URL.
|
99
|
+
# @param namespace: [String] A namespace for the URL.
|
100
|
+
#
|
101
|
+
# @return [Tanshuku::Url] A {Tanshuku::Url} instance if found.
|
102
|
+
# @reutnr [nil] +nil+ unless found.
|
55
103
|
def self.find_by_url(url, namespace: DEFAULT_NAMESPACE)
|
56
104
|
normalized_url = normalize_url(url)
|
57
105
|
hashed_url = hash_url(normalized_url, namespace:)
|
@@ -59,25 +107,52 @@ module Tanshuku
|
|
59
107
|
find_by(hashed_url:)
|
60
108
|
end
|
61
109
|
|
62
|
-
#
|
110
|
+
# Normalizes a URL. Adds or removes a trailing slash, removes +?+ for an empty query, and so on. And sorts query
|
111
|
+
# keys.
|
112
|
+
#
|
113
|
+
# @param url [String] A non-normalized URL.
|
114
|
+
#
|
115
|
+
# @return [String] A normalized URL.
|
63
116
|
def self.normalize_url(url)
|
64
117
|
parsed_url = Addressable::URI.parse(url)
|
65
118
|
parsed_url.query_values = Rack::Utils.parse_query(parsed_url.query)
|
66
119
|
parsed_url.normalize.to_s
|
67
120
|
end
|
68
121
|
|
122
|
+
# Hashes a URL with +Digest::SHA512.hexdigest+.
|
123
|
+
#
|
124
|
+
# @param url [String] A non-hashed URL.
|
125
|
+
# @param namespace: [String] A namespace for the URL.
|
126
|
+
#
|
127
|
+
# @return [String] A hashed 128-character string.
|
69
128
|
def self.hash_url(url, namespace: DEFAULT_NAMESPACE)
|
70
129
|
Digest::SHA512.hexdigest(namespace.to_s + url)
|
71
130
|
end
|
72
131
|
|
132
|
+
# Generates a key with +SecureRandom.alphanumeric+.
|
133
|
+
#
|
134
|
+
# @return [String] A 20-character alphanumeric string.
|
73
135
|
def self.generate_key
|
74
136
|
SecureRandom.alphanumeric(KEY_LENGTH)
|
75
137
|
end
|
76
138
|
|
139
|
+
# Reports an exception when failed to shorten a URL.
|
140
|
+
#
|
141
|
+
# @note This method calls {Tanshuku::Configuration#exception_reporter}'s +call+ and returns its return value.
|
142
|
+
#
|
143
|
+
# @param exception: [Exception] An error instance at shortening a URL.
|
144
|
+
# @param original_url: [String] The original URL failed to shorten.
|
145
|
+
#
|
146
|
+
# @return [void]
|
77
147
|
def self.report_exception(exception:, original_url:)
|
78
148
|
Tanshuku.config.exception_reporter.call(exception:, original_url:)
|
79
149
|
end
|
80
150
|
|
151
|
+
# The record's shortened URL.
|
152
|
+
#
|
153
|
+
# @param url_options [Hash] An option for Rails' +url_for+.
|
154
|
+
#
|
155
|
+
# @return [String] A shortened URL.
|
81
156
|
def shortened_url(url_options = {})
|
82
157
|
url_options = url_options.symbolize_keys
|
83
158
|
url_options[:controller] = "tanshuku/urls"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators"
|
4
|
+
|
5
|
+
module Tanshuku
|
6
|
+
# A generator class for Tanshuku configuration files.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class InstallGenerator < Rails::Generators::Base
|
10
|
+
source_root File.expand_path("../templates", __dir__)
|
11
|
+
|
12
|
+
# Generates a configuration file +config/initializers/tanshuku.rb+.
|
13
|
+
#
|
14
|
+
# @return [void]
|
15
|
+
def copy_initializer_file
|
16
|
+
copy_file "initializer.rb", "config/initializers/tanshuku.rb"
|
17
|
+
end
|
18
|
+
|
19
|
+
# Generates a migration file +db/migrate/20230220123456_create_tanshuku_urls.rb+.
|
20
|
+
#
|
21
|
+
# @return [void]
|
22
|
+
def copy_migration_file
|
23
|
+
filename = "20230220123456_create_tanshuku_urls.rb"
|
24
|
+
copy_file "../../../db/migrate/#{filename}", "db/migrate/#{filename}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Tanshuku.configure do |config|
|
4
|
+
# Your default URL options for `url_for`. Defaults to `{}`.
|
5
|
+
# config.default_url_options = { Your configurations here }
|
6
|
+
|
7
|
+
# Your default error reporter that is used when failed to shorten a URL. Defaults to
|
8
|
+
# `Tanshuku::Configuration::DefaultExceptionReporter`. It logs the exception and the original URL with
|
9
|
+
# `Rails.logger.warn`. This value should respond to `#call` with keyword arguments `exception:` and `original_url:`.
|
10
|
+
# config.exception_reporter =
|
11
|
+
# lambda { |exception:, original_url:|
|
12
|
+
# Your configurations here
|
13
|
+
# }
|
14
|
+
end
|
@@ -1,8 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Tanshuku
|
4
|
+
# A class for managing Tanshuku configurations.
|
4
5
|
class Configuration
|
6
|
+
# The default error-reporter. Calls +Rails.logger.warn+.
|
5
7
|
module DefaultExceptionReporter
|
8
|
+
# The default error-reporting procedure. Calls +Rails.logger.warn+.
|
9
|
+
#
|
10
|
+
# @param exception: [Exception] An error instance at shortening a URL.
|
11
|
+
# @param original_url: [String] The original URL failed to shorten.
|
12
|
+
#
|
13
|
+
# @return [void]
|
6
14
|
def self.call(exception:, original_url:)
|
7
15
|
Rails.logger.warn("Tanshuku - Failed to shorten a URL: #{exception.inspect} for #{original_url.inspect}")
|
8
16
|
end
|
@@ -10,7 +18,60 @@ module Tanshuku
|
|
10
18
|
|
11
19
|
include ActiveModel::Attributes
|
12
20
|
|
21
|
+
# @!attribute [rw] default_url_options
|
22
|
+
# Default URL options for Rails' +url_for+. Defaults to +{}+.
|
23
|
+
#
|
24
|
+
# @return [Hash]
|
25
|
+
# @return [void] If you set an invalid object.
|
26
|
+
#
|
27
|
+
# @note
|
28
|
+
# The example below means that the configured host and protocol are used. Shortened URLs will be like
|
29
|
+
# +https://example.com/t/abcdefghij0123456789+.
|
30
|
+
#
|
31
|
+
# @example
|
32
|
+
# # config/initializers/tanshuku.rb
|
33
|
+
# Tanshuku.configure do |config|
|
34
|
+
# config.default_url_options = { host: "example.com", protocol: :https }
|
35
|
+
# end
|
13
36
|
attribute :default_url_options, default: {}
|
37
|
+
|
38
|
+
# @!attribute [rw] exception_reporter
|
39
|
+
# A error-reporter class, module, or object used when shortening a URL fails. This should respond to +#call+ with
|
40
|
+
# keyword arguments +exception:+ and +original_url:+. Defaults to
|
41
|
+
# {Tanshuku::Configuration::DefaultExceptionReporter}.
|
42
|
+
#
|
43
|
+
# @return [#call] An object that responds to +#call+ with keyword arguments +exception:+ and +original_url:+.
|
44
|
+
# @return [void] If you set an invalid object.
|
45
|
+
#
|
46
|
+
# @note The example below means that an exception and a URL will be reported to Sentry (https://sentry.io).
|
47
|
+
#
|
48
|
+
# @example
|
49
|
+
# # config/initializers/tanshuku.rb
|
50
|
+
# Tanshuku.configure do |config|
|
51
|
+
# config.exception_reporter =
|
52
|
+
# lambda { |exception:, original_url:|
|
53
|
+
# Sentry.capture_exception(exception, tags: { original_url: })
|
54
|
+
# }
|
55
|
+
# end
|
14
56
|
attribute :exception_reporter, default: DefaultExceptionReporter
|
57
|
+
|
58
|
+
def initialize(...)
|
59
|
+
super
|
60
|
+
@mutex = Mutex.new
|
61
|
+
end
|
62
|
+
|
63
|
+
# Configures Tanshuku thread-safely.
|
64
|
+
#
|
65
|
+
# @yieldparam config [Tanshuku::Configuration] A configuration object that is yielded.
|
66
|
+
# @yieldreturn [void]
|
67
|
+
#
|
68
|
+
# @return [void]
|
69
|
+
#
|
70
|
+
# @api private
|
71
|
+
def configure
|
72
|
+
@mutex.synchronize do
|
73
|
+
yield self
|
74
|
+
end
|
75
|
+
end
|
15
76
|
end
|
16
77
|
end
|
data/lib/tanshuku/engine.rb
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Tanshuku
|
4
|
+
# Tanshuku's Rails engine.
|
5
|
+
#
|
6
|
+
# @note
|
7
|
+
# The example below generates a routing +GET `/t/:key` to `Tanshuku::UrlsController#show`+. When your Rails app
|
8
|
+
# receives a request to +/t/abcdefghij0123456789+, +Tanshuku::UrlsController#show+ will be called and a
|
9
|
+
# +Tanshuku::Url+ record with a key +abcdefghij0123456789+ will be found. Then the request will be redirected to the
|
10
|
+
# +Tanshuku::Url+ record's original URL.
|
11
|
+
#
|
12
|
+
# @example To mount Tanshuku to your Rails app
|
13
|
+
# # config/routes.rb
|
14
|
+
# Rails.application.routes.draw do
|
15
|
+
# mount Tanshuku::Engine, at: "/t"
|
16
|
+
# end
|
4
17
|
class Engine < ::Rails::Engine
|
5
18
|
isolate_namespace Tanshuku
|
6
19
|
|
data/lib/tanshuku/version.rb
CHANGED
data/lib/tanshuku.rb
CHANGED
@@ -4,14 +4,38 @@ require_relative "tanshuku/configuration"
|
|
4
4
|
require_relative "tanshuku/engine"
|
5
5
|
require_relative "tanshuku/version"
|
6
6
|
|
7
|
+
# Tanshuku's namespace.
|
7
8
|
module Tanshuku
|
9
|
+
# Returns a configuration object for Tanshuku.
|
10
|
+
#
|
11
|
+
# @return [Tanshuku::Configuration]
|
12
|
+
#
|
13
|
+
# @note
|
14
|
+
# Mutating a {Tanshuku::Configuration} object is thread-<em><b>unsafe</b></em>. It is recommended to use
|
15
|
+
# {Tanshuku.configure} for configuration.
|
8
16
|
def self.config
|
9
|
-
|
10
|
-
|
11
|
-
|
17
|
+
# Disable this cop but use `Tanshuku::Configuration#configure` for thread-safety.
|
18
|
+
# rubocop:disable ThreadSafety/InstanceVariableInClassMethod
|
19
|
+
@config ||= Configuration.new
|
20
|
+
# rubocop:enable ThreadSafety/InstanceVariableInClassMethod
|
12
21
|
end
|
13
22
|
|
14
|
-
|
15
|
-
|
23
|
+
# Configures Tanshuku.
|
24
|
+
#
|
25
|
+
# @yieldparam config [Tanshuku::Configuration] A configuration object that is yielded.
|
26
|
+
# @yieldreturn [void]
|
27
|
+
#
|
28
|
+
# @return [void]
|
29
|
+
#
|
30
|
+
# @note
|
31
|
+
# This method is thread-safe. When mutating a {Tanshuku::Configuration} object for configuration, it is recommended
|
32
|
+
# to use this method.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# Tanshuku.configure do |config|
|
36
|
+
# config.default_url_options = { host: "localhost", protocol: :https }
|
37
|
+
# end
|
38
|
+
def self.configure(&)
|
39
|
+
config.configure(&)
|
16
40
|
end
|
17
41
|
end
|
data/tmp/.keep
ADDED
File without changes
|
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.
|
4
|
+
version: 0.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kg8m
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-03-
|
11
|
+
date: 2023-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -48,6 +48,7 @@ extensions: []
|
|
48
48
|
extra_rdoc_files: []
|
49
49
|
files:
|
50
50
|
- LICENSE
|
51
|
+
- README.md
|
51
52
|
- Rakefile
|
52
53
|
- app/controllers/tanshuku/urls_controller.rb
|
53
54
|
- app/models/tanshuku/url.rb
|
@@ -55,10 +56,13 @@ files:
|
|
55
56
|
- config/locales/ja.yml
|
56
57
|
- config/routes.rb
|
57
58
|
- db/migrate/20230220123456_create_tanshuku_urls.rb
|
59
|
+
- lib/generators/tanshuku/install_generator.rb
|
60
|
+
- lib/generators/templates/initializer.rb
|
58
61
|
- lib/tanshuku.rb
|
59
62
|
- lib/tanshuku/configuration.rb
|
60
63
|
- lib/tanshuku/engine.rb
|
61
64
|
- lib/tanshuku/version.rb
|
65
|
+
- tmp/.keep
|
62
66
|
homepage: https://github.com/kg8m/tanshuku
|
63
67
|
licenses:
|
64
68
|
- MIT
|
@@ -66,6 +70,7 @@ metadata:
|
|
66
70
|
homepage_uri: https://github.com/kg8m/tanshuku
|
67
71
|
source_code_uri: https://github.com/kg8m/tanshuku
|
68
72
|
changelog_uri: https://github.com/kg8m/tanshuku/releases
|
73
|
+
documentation_uri: https://kg8m.github.io/tanshuku/
|
69
74
|
rubygems_mfa_required: 'true'
|
70
75
|
post_install_message:
|
71
76
|
rdoc_options: []
|