Class: EAAL::Cache::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/eaal/eaal_cache.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/



28
29
30
31
32
33
# File 'lib/eaal/eaal_cache.rb', line 28

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.



23
24
25
# File 'lib/eaal/eaal_cache.rb', line 23

def basepath
  @basepath
end

Instance Method Details

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

create the path/filename for the cache file



36
37
38
39
40
41
42
43
# File 'lib/eaal/eaal_cache.rb', line 36

def filename(userid, apikey, scope, name, args)
  ret =""
  args.delete_if { |k,v| (v || "").to_s.length == 0 }
  h = args.stringify_keys
  ret += h.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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/eaal/eaal_cache.rb', line 46

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



72
73
74
75
76
# File 'lib/eaal/eaal_cache.rb', line 72

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,'w') { |f| f.print xml }        
end

#validate_cache(xml, name) ⇒ Object

validate cached datas cachedUntil



62
63
64
65
66
67
68
69
# File 'lib/eaal/eaal_cache.rb', line 62

def validate_cache(xml, name)
  doc = Hpricot.XML(xml)
	if name == "WalletJournal"
    Time.at((doc/"/eveapi/cachedUntil").inner_html.to_time.to_i + 3600) > Time.now
  else 
	  (doc/"/eveapi/cachedUntil").inner_html.to_time > Time.now
	end
end