Class: Wabi::RegistryClient

Inherits:
Object
  • Object
show all
Defined in:
lib/wabi/registry_client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://wabikit.dev/r"
DEFAULT_TTL =
24 * 60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: DEFAULT_BASE_URL) ⇒ RegistryClient

Returns a new instance of RegistryClient.



16
17
18
19
# File 'lib/wabi/registry_client.rb', line 16

def initialize(base_url: DEFAULT_BASE_URL)
  @base_url = base_url
  @ttl = Integer(ENV.fetch("WABI_CACHE_TTL", DEFAULT_TTL))
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



14
15
16
# File 'lib/wabi/registry_client.rb', line 14

def base_url
  @base_url
end

Instance Method Details

#cache_dirObject



21
22
23
# File 'lib/wabi/registry_client.rb', line 21

def cache_dir
  File.expand_path("~/.cache/wabi/#{Wabi::VERSION}")
end

#fetch(component_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wabi/registry_client.rb', line 25

def fetch(component_name)
  unless dev_context?
    cached = read_cache(component_name)
    return JSON.parse(cached) if cached
  end

  body =
    if @base_url.start_with?("file://")
      fetch_local(component_name)
    else
      fetch_http(component_name)
    end

  write_cache(component_name, body) unless dev_context?
  JSON.parse(body)
end