class Valvat::Lookup
Public Class Methods
cache()
click to toggle source
# File lib/valvat_cache.rb, line 8 def cache @@cache ||= Hash.new end
cache=(cache_hash)
click to toggle source
# File lib/valvat_cache.rb, line 12 def cache=(cache_hash) @@cache = cache_hash.is_a?(Hash) ? cache_hash : Hash.new end
cache_path()
click to toggle source
# File lib/valvat_cache.rb, line 16 def cache_path @@cache_path ||= nil end
cache_path=(path)
click to toggle source
# File lib/valvat_cache.rb, line 20 def cache_path=(path) @@lock ||= Mutex.new changed = false if !path.nil? if File.file?(path) self.cache = JSON.parse(File.open(path, 'r') { |f| f.read }, symbolize_names: true) self.cache.each do |k, v| v[:request_date] = Date.parse v[:request_date] if (Date.today - v[:request_date]) > self.expiration_days changed = true self.cache.delete(k) end end File.open(path, 'w') { |f| f.write(cache.to_json) } if changed else self.cache = Hash.new.to_json File.open(path, 'w') { |f| f.write(cache) } end end @@cache_path = path end
expiration_days()
click to toggle source
# File lib/valvat_cache.rb, line 46 def expiration_days @@expiration_date ||= 7 end
expiration_days=(days)
click to toggle source
# File lib/valvat_cache.rb, line 50 def expiration_days=(days) @@expiration_date = days end
semaphore()
click to toggle source
# File lib/valvat_cache.rb, line 54 def semaphore @@lock end
Private Instance Methods
response()
click to toggle source
# File lib/valvat_cache.rb, line 61 def response if !options[:requester_vat].nil? || self.class.cache[vat.raw.to_sym].nil? || (Date.today - self.class.cache[vat.raw.to_sym][:request_date]) > self.class.expiration_days self.class.cache[vat.raw.to_sym] = request.perform(self.class.client) begin if self.class.cache_path self.class.semaphore.synchronize do File.open(self.class.cache_path, 'w') { |f| f.write self.class.cache.to_json } end end rescue Exception end end @response ||= self.class.cache[vat.raw.to_sym] end