Sha256: 44d2440aca64193665d66503847e6c6927532cec82dc5595f7f292b042c56d45

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module Opener
  class ChainedDaemon
    class LanguagesCache

      include MonitorMixin

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

      def initialize
        super #MonitorMixin

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

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

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

        languages     = SymMash.new JSON.parse http.get(@url).body
        @last_updated = Time.now
        @cache        = languages.data.each.with_object({}){ |l,h| h[l.code] = l }
        @cache
      end

      def http
        return @http if @http

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

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opener-chained-daemon-3.3.5 lib/opener/chained_daemon/languages_cache.rb
opener-chained-daemon-3.3.4 lib/opener/chained_daemon/languages_cache.rb
opener-chained-daemon-3.3.3 lib/opener/chained_daemon/languages_cache.rb