class Opener::PropertyTagger::RemoteAspectsCache
Thread-safe cache for storing the contents of remote aspects.
Constants
- UPDATE_INTERVAL
Public Class Methods
new()
click to toggle source
Calls superclass method
# File lib/opener/property_tagger/remote_aspects_cache.rb, line 12 def initialize super @url = ENV['PROPERTY_TAGGER_LEXICONS_URL'] @cache = {} end
Public Instance Methods
[](**params)
click to toggle source
# File lib/opener/property_tagger/remote_aspects_cache.rb, line 19 def [] **params existing = @cache[params] return existing if existing and existing.from > UPDATE_INTERVAL.ago synchronize do @cache[params] = cache_update @cache[params], **params end end
Also aliased as: get
cache_update(existing = nil, **params)
click to toggle source
# File lib/opener/property_tagger/remote_aspects_cache.rb, line 29 def cache_update existing = nil, **params from = Time.now lexicons = load_aspects cache: existing, **params if existing and lexicons.blank? existing.from = from return existing end Hashie::Mash.new( aspects: lexicons, from: from, ) end
load_aspects(lang:, cache:, **params)
click to toggle source
# File lib/opener/property_tagger/remote_aspects_cache.rb, line 44 def load_aspects lang:, cache:, **params url = "#{@url}&language_code=#{lang}&#{params.to_query}" url += "&if_updated_since=#{cache.from.utc.iso8601}" if cache puts "#{lang}: loading aspects from #{url}" lexicons = JSON.parse HTTPClient.new.get(url).body lexicons = lexicons['data'].map{ |l| Hashie::Mash.new l } mapping = Hash.new{ |hash, key| hash[key] = [] } lexicons.each do |l| mapping[l.lemma.to_sym] << l l.variants&.each do |v| mapping[v.lemma.to_sym] << l end end mapping end