Class: Tanshuku::Configuration

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes
Defined in:
lib/tanshuku/configuration.rb

Overview

A class for managing Tanshuku configurations.

Defined Under Namespace

Modules: DefaultExceptionReporter, DefaultKeyGenerator, DefaultUrlHasher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



174
175
176
177
# File 'lib/tanshuku/configuration.rb', line 174

def initialize(...)
  super
  @mutex = Mutex.new
end

Instance Attribute Details

#default_url_optionsHash, void

Note:

The example below means that the configured host and protocol are used. Shortened URLs will be like https://example.com/t/abcdefghij0123456789.

Default URL options for Rails’ url_for. Defaults to {}.

Examples:

# config/initializers/tanshuku.rb
Tanshuku.configure do |config|
  config.default_url_options = { host: "example.com", protocol: :https }
end

Returns:

  • (Hash)
  • (void)

    If you set an invalid object.



62
# File 'lib/tanshuku/configuration.rb', line 62

attribute :default_url_options, default: {}

#exception_reporter#call, void

Note:

The example below means that an exception and a URL will be reported to Sentry (sentry.io).

A error-reporter class, module, or object used when shortening a URL fails. This should respond to #call with required keyword arguments exception: and original_url:. Defaults to DefaultExceptionReporter.

Examples:

# config/initializers/tanshuku.rb
Tanshuku.configure do |config|
  config.exception_reporter =
    lambda { |exception:, original_url:|
      Sentry.capture_exception(exception, tags: { original_url: })
    }
end

Returns:

  • (#call)

    A class, module, or object that responds to #call with required keyword arguments exception: and original_url:.

  • (void)

    If you set an invalid object.



172
# File 'lib/tanshuku/configuration.rb', line 172

attribute :exception_reporter, default: DefaultExceptionReporter

#key_generator#call, void

Note:

The example below means that unique keys for shortened URLs are generated by SecureRandom.uuid.

A class, module, or object to generate a unique key for shortened URLs. This should respond to #call without any arguments. Defaults to DefaultKeyGenerator.

Examples:

# config/initializers/tanshuku.rb
Tanshuku.configure do |config|
  config.key_generator = -> { SecureRandom.uuid }
end

Returns:

  • (#call)

    A class, module, or object that responds to #call without any arguments.

  • (void)

    If you set an invalid object.



150
# File 'lib/tanshuku/configuration.rb', line 150

attribute :key_generator, default: DefaultKeyGenerator

#key_lengthInteger, void

Note:

Don’t forget to fix the limit of the tanshuku_urls.key column if you change this value.

Note:

The example below means that Url#key has 10-char string.

Length of Url#key when #key_generator is DefaultKeyGenerator. Defaults to 20.

Examples:

# config/initializers/tanshuku.rb
Tanshuku.configure do |config|
  config.key_length = 10
end

Returns:

  • (Integer)
  • (void)

    If you set an invalid object.



115
# File 'lib/tanshuku/configuration.rb', line 115

attribute :key_length, :integer, default: 20

#max_url_lengthInteger, void

Note:

The example below means that Url#url can have a URL string with less than or equal to 20,000 characters.

Maximum length of Url#url. Defaults to 10,000.

Examples:

# config/initializers/tanshuku.rb
Tanshuku.configure do |config|
  config.max_url_length = 20_000
end

Returns:

  • (Integer)
  • (void)

    If you set an invalid object.



79
# File 'lib/tanshuku/configuration.rb', line 79

attribute :max_url_length, :integer, default: 10_000

#url_hasher#call, void

Note:

The example below means that URLs are hashed with Digest::SHA256.hexdigest.

A class, module, or object to hash a URL. This should respond to #call with a required positional argument url and a required keyword argument namespace:. Defaults to DefaultUrlHasher.

Examples:

# config/initializers/tanshuku.rb
Tanshuku.configure do |config|
  config.url_hasher = ->(url, namespace:) { Digest::SHA256.hexdigest("#{namespace}#{url}") }
end

Returns:

  • (#call)

    A class, module, or object that responds to #call with a required positional argument url and a required keyword argument namespace:.

  • (void)

    If you set an invalid object.



133
# File 'lib/tanshuku/configuration.rb', line 133

attribute :url_hasher, default: DefaultUrlHasher

#url_patternRegexp, void

Note:

The example below means that Url#url should start with a slash /.

Allowed pattern of Url#url. Defaults to %r{\A(?:https?://\w+|/)}. This default value forces a URL to start with “http://” or “https://”, or to be an absolute path without scheme.

Examples:

# config/initializers/tanshuku.rb
Tanshuku.configure do |config|
  config.url_pattern = /\A\//
end

Returns:

  • (Regexp)
  • (void)

    If you set an invalid object.



96
# File 'lib/tanshuku/configuration.rb', line 96

attribute :url_pattern, default: %r{\A(?:https?://\w+|/)}

Instance Method Details

#configure {|config| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Use Tanshuku.configure as a public API for configuration.

This method returns an undefined value.

Configures Tanshuku thread-safely.

Yield Parameters:

Yield Returns:

  • (void)


189
190
191
192
193
# File 'lib/tanshuku/configuration.rb', line 189

def configure
  @mutex.synchronize do
    yield self
  end
end