Class: Clowk::Subdomain

Inherits:
Object
  • Object
show all
Defined in:
lib/clowk/subdomain.rb

Constant Summary collapse

CACHE_TTL =
60
DEFAULT_SUBDOMAIN_BASE =
"clowk.dev"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Subdomain

Returns a new instance of Subdomain.



49
50
51
52
# File 'lib/clowk/subdomain.rb', line 49

def initialize(options = {})
  @publishable_key = options.fetch(:publishable_key, Clowk.config.publishable_key)
  @subdomain_url = options.fetch(:subdomain_url, Clowk.config.subdomain_url)
end

Class Method Details

.clear_cache!Object



17
18
19
# File 'lib/clowk/subdomain.rb', line 17

def clear_cache!
  @cache_mutex.synchronize { @cache = {} }
end

.read_cache(key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clowk/subdomain.rb', line 21

def read_cache(key)
  @cache_mutex.synchronize do
    entry = cache[key]

    return unless entry
    return entry[:value] if entry[:expires_at] > Time.now

    cache.delete(key)
    nil
  end
end

.resolve_url!Object



13
14
15
# File 'lib/clowk/subdomain.rb', line 13

def resolve_url!(...)
  new(...).resolve_url!
end

.write_cache(key, value, ttl:) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/clowk/subdomain.rb', line 33

def write_cache(key, value, ttl:)
  @cache_mutex.synchronize do
    cache[key] = {
      value: value,
      expires_at: Time.now + ttl
    }
  end
end

Instance Method Details

#resolve_url!Object

Raises:



54
55
56
57
58
59
# File 'lib/clowk/subdomain.rb', line 54

def resolve_url!
  return resolve_from_key if publishable_key.present?
  return normalize_url(subdomain_url) if subdomain_url.present?

  raise ConfigurationError, "set publishable_key or subdomain_url to build Clowk URLs"
end