Class: EAAL::Cache::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/eaal/cache/file.rb

Overview

EAAL::Cache::FileCache File based xml cache which respects the cachedUntil of the Eve API Usage:

EAAL.cache = EAAL::Cache::FileCache.new

Or

EAAL.cache = EAAL::Cache::FileCache.new("/path/to/place/to/store/xml/data")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basepath = "#{ENV['HOME']}/.eaal/cache") ⇒ FileCache

constructor, takes one argument which is the path where files should be written

  • basepath (String) path which should be used to store cached data. defaults to $HOME/.eaal/cache/



13
14
15
16
17
18
# File 'lib/eaal/cache/file.rb', line 13

def initialize(basepath = "#{ENV['HOME']}/.eaal/cache")
  if basepath[(basepath.length) -1, basepath.length] != "/"
    basepath += "/"
  end
  @basepath = basepath
end

Instance Attribute Details

#basepathObject

Returns the value of attribute basepath.



8
9
10
# File 'lib/eaal/cache/file.rb', line 8

def basepath
  @basepath
end

Instance Method Details

#filename(userid, apikey, scope, name, args) ⇒ Object

create the path/filename for the cache file



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/eaal/cache/file.rb', line 21

def filename(userid, apikey, scope, name, args)
  ret =""
  args.delete_if { |k,v| (v || "").to_s.length == 0 }
#    h = args.stringify_keys
  args.keys.each do |key|
    args[key.to_s] = args.delete(key)
  end
  ret += args.sort.flatten.collect{ |e| e.to_s }.join('_')
  hash = ret.gsub(/_$/,'')
  "#{@basepath}#{userid}/#{apikey}/#{scope}/#{name}/Request_#{hash}.xml"
end

#load(userid, apikey, scope, name, args) ⇒ Object

load xml if available, return false if not available, or cachedUntil ran out



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/eaal/cache/file.rb', line 34

def load(userid, apikey, scope, name, args)
  filename = self.filename(userid, apikey,scope,name,args)
  if not File.exist?(filename)
    ret = false
  else
    xml = File.open(filename).read
    if self.validate_cache(xml, name)
      ret = xml
    else
      ret = false
    end
  end
  ret
end

#save(userid, apikey, scope, name, args, xml) ⇒ Object

save xml data to file



64
65
66
67
68
# File 'lib/eaal/cache/file.rb', line 64

def save(userid, apikey, scope, name, args, xml)
  filename = self.filename(userid, apikey,scope,name,args)
  FileUtils.mkdir_p(File.dirname(filename))
  File.open(filename,'wb') { |f| f.print xml }
end

#validate_cache(xml, name) ⇒ Object

validate cached datas cachedUntil



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/eaal/cache/file.rb', line 50

def validate_cache(xml, name)
  doc = Hpricot.XML(xml)
  cached_until_in_utc = (doc/"/eveapi/cachedUntil").inner_html
  cached_until_in_utc += ' UTC' unless cached_until_in_utc.end_with?('UTC')
  cached_until = Time.parse(cached_until_in_utc)
  if name == "WalletJournal"
    result = Time.at(cached_until.to_i + 3600) > Time.now.utc
  else
    result = cached_until > Time.now.utc
  end
  result
end