Class: Opener::ChainedDaemon::LanguagesCache

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/opener/chained_daemon/languages_cache.rb

Constant Summary collapse

UPDATE_INTERVAL =
(ENV['CACHE_EXPIRE_MINS']&.to_i || 5).minutes

Instance Method Summary collapse

Constructor Details

#initializeLanguagesCache

Returns a new instance of LanguagesCache.



9
10
11
12
13
14
15
# File 'lib/opener/chained_daemon/languages_cache.rb', line 9

def initialize
  super #MonitorMixin

  @url          = ENV['SUPPORTED_LANGUAGES_URL']
  @cache        = []
  @last_updated = nil
end

Instance Method Details

#cache_updateObject



25
26
27
28
29
30
31
# File 'lib/opener/chained_daemon/languages_cache.rb', line 25

def cache_update
  puts "loading supported languages from url #{@url}" if ENV['DEBUG']

  languages     = JSON.parse http.get(@url).body
  @cache        = languages['data'].map { |l| l['code'] }
  @last_updated = Time.now
end

#getObject



17
18
19
20
21
22
23
# File 'lib/opener/chained_daemon/languages_cache.rb', line 17

def get
  synchronize do
    break @cache if @last_updated and @last_updated > UPDATE_INTERVAL.ago
    cache_update
  end
  @cache
end

#httpObject



33
34
35
36
37
38
39
40
41
# File 'lib/opener/chained_daemon/languages_cache.rb', line 33

def http
  return @http if @http

  @http = HTTPClient.new
  @http.send_timeout    = 120
  @http.receive_timeout = 120
  @http.connect_timeout = 120
  @http
end