Class: Kotoshu::SourceRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/source_registry.rb

Overview

Single source of truth for where each remote resource lives.

Every URL the cache layer fetches is built here. Caches do not construct URL strings inline. Per-repo pins honor that kotoshu/dictionaries ships on v1 while the other repos are on main, which previously caused silent 404s on first-use.

Examples:

registry = Kotoshu::SourceRegistry.new
registry.url_for(:spelling, lang: "en", ext: "aff")
# => "https://raw.githubusercontent.com/kotoshu/dictionaries/v1/en/spelling/index.aff"

Defined Under Namespace

Classes: Source

Constant Summary collapse

DEFAULT_BASE_URL =
"https://raw.githubusercontent.com/kotoshu"
SOURCES =

Returns:

{
  spelling: Source.new(repo: "dictionaries", default_pin: "v1",
                       template: "dictionaries/%<pin>s/%<lang>s/spelling/index.%<ext>s"),
  grammar: Source.new(repo: "dictionaries", default_pin: "v1",
                      template: "dictionaries/%<pin>s/%<lang>s/grammar/rules.yaml"),
  dict_manifest: Source.new(repo: "dictionaries", default_pin: "v1",
                            template: "dictionaries/%<pin>s/manifest.json"),
  frequency: Source.new(repo: "frequency-list-kelly", default_pin: "main",
                        template: "frequency-list-kelly/%<pin>s/data/%<lang>s.json"),
  freq_manifest: Source.new(repo: "frequency-list-kelly", default_pin: "main",
                            template: "frequency-list-kelly/%<pin>s/manifest.json"),
  model: Source.new(repo: "models-fasttext-onnx", default_pin: "main",
                    template: "models-fasttext-onnx/%<pin>s/models/%<lang>s/fasttext.%<lang>s.onnx"),
  model_vocab: Source.new(repo: "models-fasttext-onnx", default_pin: "main",
                          template: "models-fasttext-onnx/%<pin>s/models/%<lang>s/fasttext.%<lang>s.vocab.json"),
  model_manifest: Source.new(repo: "models-fasttext-onnx", default_pin: "main",
                             template: "models-fasttext-onnx/%<pin>s/manifest.json")
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: DEFAULT_BASE_URL, pins: {}) ⇒ SourceRegistry

Returns a new instance of SourceRegistry.

Parameters:

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    GitHub raw root, no trailing slash.

  • pins (Hash<String, String>) (defaults to: {})

    Optional per-repo pin overrides keyed by repo name (e.g. { "dictionaries" => "v2" }).



43
44
45
46
# File 'lib/kotoshu/source_registry.rb', line 43

def initialize(base_url: DEFAULT_BASE_URL, pins: {})
  @base_url = base_url.to_s.chomp("/")
  @pins = pins.transform_keys(&:to_s).freeze
end

Instance Attribute Details

#base_urlString (readonly)

Returns Configured GitHub raw root (no trailing slash).

Returns:

  • (String)

    Configured GitHub raw root (no trailing slash).



49
50
51
# File 'lib/kotoshu/source_registry.rb', line 49

def base_url
  @base_url
end

Instance Method Details

#pin_for_source(source_key) ⇒ String

Returns Resolved pin (override or default).

Parameters:

  • source_key (Symbol)

Returns:

  • (String)

    Resolved pin (override or default).



65
66
67
68
# File 'lib/kotoshu/source_registry.rb', line 65

def pin_for_source(source_key)
  source = SOURCES.fetch(source_key)
  pin_for(source)
end

#repo_for(source_key) ⇒ String

Returns Repo name (e.g. "dictionaries").

Parameters:

  • source_key (Symbol)

Returns:

  • (String)

    Repo name (e.g. "dictionaries").



72
73
74
# File 'lib/kotoshu/source_registry.rb', line 72

def repo_for(source_key)
  SOURCES.fetch(source_key).repo
end

#url_for(source_key, lang: nil, ext: nil) ⇒ String

Returns Fully-qualified URL.

Parameters:

  • source_key (Symbol)

    One of SOURCES.keys.

  • lang (String, nil) (defaults to: nil)

    Language code, interpolated into template.

  • ext (String, nil) (defaults to: nil)

    File extension, interpolated into template.

Returns:

  • (String)

    Fully-qualified URL.



55
56
57
58
59
60
61
# File 'lib/kotoshu/source_registry.rb', line 55

def url_for(source_key, lang: nil, ext: nil)
  source = SOURCES.fetch(source_key) do
    raise ArgumentError, "unknown source: #{source_key.inspect}"
  end
  path = source.template % { pin: pin_for(source), lang: lang, ext: ext }
  "#{@base_url}/#{path}"
end