Sha256: 640c5ec64cdc8e0dcc16532db203bc617b1edbe429ae88fe5d8d020a9e3c40aa

Contents?: true

Size: 888 Bytes

Versions: 1

Compression:

Stored size: 888 Bytes

Contents

module Opener
  class PropertyTagger
    ##
    # Thread-safe cache for storing the contents of remote aspects.
    #
    class RemoteAspectsCache

      include MonitorMixin

      def initialize
        super

        @url   = ENV['PROPERTY_TAGGER_LEXICONS_URL']
        @cache = {}
      end

      def [] lang
        synchronize do
          @cache[lang] ||= load_aspects lang
        end
      end
      alias_method :get, :[]

      def load_aspects lang
        mapping  = Hash.new{ |hash, key| hash[key] = [] }
        url      = "#{@url}&language_code=#{lang}"
        lexicons = JSON.parse HTTPClient.new.get(url).body
        lexicons = lexicons['data'].map{ |l| Hashie::Mash.new l }
        puts "#{lang}: loaded aspects from #{url}"

        lexicons.each do |l|
          mapping[l.lemma.to_sym] << l.aspect
        end

        return mapping
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opener-property-tagger-3.2.1 lib/opener/property_tagger/remote_aspects_cache.rb