tanshuku 0.0.13 → 0.0.14

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: 1748ebeb12ae7c094ef0cd34386f6db676b8363c50423b39bf571e92093a1217
4
- data.tar.gz: c9d4513adbf7bd8e87d1d218801b108c7a72e4681185d4b7f60705eefff722ce
3
+ metadata.gz: b280080f53f8fb1f57a0b05cd1f4549f81a8c5c6a893501eec056bf0ff49e894
4
+ data.tar.gz: c2dea1c5782731a81c2f961186211cddff5c9494be925f1d395a53cea17a11c4
5
5
  SHA512:
6
- metadata.gz: b18fa0edf40682988bf7c51055f2a59cc100af34ea50ccf52bd9ca320b34a95dd76f9bd864f57e98e4909dbec904da954e02d0b1e4cadd2ff42119f1956cee47
7
- data.tar.gz: 0fa9fe03d277660ba8d440429f8f58686d3587dc09db02788ffc79ddeff2e34ef995500b0fde39e19cb841f026e7ce4b918c06ff504120fbf4203bae54cfbe5f
6
+ metadata.gz: 8c96f4614d77af55d6a3889956967ab5244819071a0ec77d415df5af76fd288eefc0554cbb32b661ea7349fd5e8175201e02fe5309ec8dd53634eefa30508cc5
7
+ data.tar.gz: 9f8ed89f16d6531f60303f03feded46ff98a35f5319bd6ec7b77e5526664fd830dad0da8eec51e8fbd7432d87702d88d34df9b1db1fdc03a4a04a006d87af3d2
data/README.md CHANGED
@@ -31,7 +31,9 @@ end
31
31
 
32
32
  **Note**: This step is optional.
33
33
 
34
- **Note**: The initializer file can be generated by `bin/rails generate tanshuku:install`. See the "[Installation](#installation)" section for more information.
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.
35
37
 
36
38
  #### `config.default_url_options`
37
39
 
@@ -52,7 +54,7 @@ end
52
54
 
53
55
  If an exception occurs when shortening a URL, Tanshuku reports it with configured `config.exception_reporter` object.
54
56
 
55
- Default value is `Tanshuku::Configuration::DefaultExceptionReporter`. It logs the exception and the original URL with `Rails.logger.warn`.
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`.
56
58
 
57
59
  Value of `config.exception_reporter` should respond to `#call` with keyword arguments `exception:` and `original_url:`.
58
60
 
@@ -68,6 +70,10 @@ Tanshuku.configure do |config|
68
70
  end
69
71
  ```
70
72
 
73
+ #### More information
74
+
75
+ cf. [`Tanshuku::Configuration`'s API documentation](https://kg8m.github.io/tanshuku/Tanshuku/Configuration.html)
76
+
71
77
  ### 3. Generate shortened URLs
72
78
 
73
79
  #### Basic cases
@@ -78,7 +84,7 @@ You can generate a shortened URL with `Tanshuku::Url.shorten`. For example:
78
84
  Tanshuku::Url.shorten("https://google.com/") #=> "https://example.com/t/abcdefghij0123456789"
79
85
  ```
80
86
 
81
- [`config.default_url_options`](#configdefault_url_options) is used for the shortened URL.
87
+ [`config.default_url_options`](https://kg8m.github.io/tanshuku/Tanshuku/Configuration.html#default_url_options-instance_method) is used for the shortened URL.
82
88
 
83
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.
84
90
 
@@ -123,6 +129,10 @@ Tanshuku::Url.shorten("https://google.com/", namespace: "a") #=> "https://examp
123
129
  Tanshuku::Url.shorten("https://google.com/", namespace: "a") #=> "https://example.com/t/ab01cd23ef45gh67ij89"
124
130
  ```
125
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
+
126
136
  ### 4. Share the shortened URLs
127
137
 
128
138
  You can share the shortened URLs, e.g., `https://example.com/t/abcdefghij0123456789`.
data/Rakefile CHANGED
@@ -20,6 +20,11 @@ namespace :yard do
20
20
  puts "See http://localhost:8808/"
21
21
  sh "yard server --reload"
22
22
  end
23
+
24
+ desc "Generate YARD docs"
25
+ task :generate do
26
+ sh "yard --output-dir docs"
27
+ end
23
28
  end
24
29
 
25
30
  task default: %i[rubocop spec]
@@ -44,6 +44,25 @@ module Tanshuku
44
44
  # Tanshuku::Url.shorten("https://google.com/") #=> "http://localhost/t/abcdefghij0123456789"
45
45
  # @example If failed to shorten a URL.
46
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"
47
66
  def self.shorten(original_url, namespace: DEFAULT_NAMESPACE, url_options: {})
48
67
  raise ArgumentError, "original_url should be present" unless original_url
49
68
 
@@ -23,15 +23,55 @@ module Tanshuku
23
23
  #
24
24
  # @return [Hash]
25
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
26
36
  attribute :default_url_options, default: {}
27
37
 
28
38
  # @!attribute [rw] exception_reporter
29
- # A error-reporter class, module, or object used when shortening a URL fails. This should respond to +#call+.
30
- # Defaults to {Tanshuku::Configuration::DefaultExceptionReporter}.
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}.
31
42
  #
32
- # @return [Tanshuku::Configuration::DefaultExceptionReporter, #call]
33
- # A +Tanshuku::Configuration::DefaultExceptionReporter+ instance or an object that responds to +#call+.
43
+ # @return [#call] An object that responds to +#call+ with keyword arguments +exception:+ and +original_url:+.
34
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
35
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
36
76
  end
37
77
  end
@@ -2,6 +2,18 @@
2
2
 
3
3
  module Tanshuku
4
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
5
17
  class Engine < ::Rails::Engine
6
18
  isolate_namespace Tanshuku
7
19
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tanshuku
4
- VERSION = "0.0.13"
4
+ VERSION = "0.0.14"
5
5
  end
data/lib/tanshuku.rb CHANGED
@@ -9,10 +9,15 @@ module Tanshuku
9
9
  # Returns a configuration object for Tanshuku.
10
10
  #
11
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.
12
16
  def self.config
13
- Mutex.new.synchronize do
14
- @config ||= Configuration.new
15
- end
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
16
21
  end
17
22
 
18
23
  # Configures Tanshuku.
@@ -22,11 +27,15 @@ module Tanshuku
22
27
  #
23
28
  # @return [void]
24
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
+ #
25
34
  # @example
26
35
  # Tanshuku.configure do |config|
27
36
  # config.default_url_options = { host: "localhost", protocol: :https }
28
37
  # end
29
- def self.configure
30
- yield config
38
+ def self.configure(&)
39
+ config.configure(&)
31
40
  end
32
41
  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.13
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-22 00:00:00.000000000 Z
11
+ date: 2023-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -70,6 +70,7 @@ metadata:
70
70
  homepage_uri: https://github.com/kg8m/tanshuku
71
71
  source_code_uri: https://github.com/kg8m/tanshuku
72
72
  changelog_uri: https://github.com/kg8m/tanshuku/releases
73
+ documentation_uri: https://kg8m.github.io/tanshuku/
73
74
  rubygems_mfa_required: 'true'
74
75
  post_install_message:
75
76
  rdoc_options: []