Class: EveApi::Caches::File

Inherits:
Default
  • Object
show all
Defined in:
lib/eve_api/caches/file.rb

Instance Method Summary collapse

Methods inherited from Default

#path

Constructor Details

#initialize(cache_path) ⇒ File

Returns a new instance of File.



4
5
6
# File 'lib/eve_api/caches/file.rb', line 4

def initialize(cache_path)
  @root = cache_path
end

Instance Method Details

#cached?(keyID, scope, canonic_name, characterID) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/eve_api/caches/file.rb', line 8

def cached?(keyID, scope, canonic_name, characterID)
  file_path = render_path(keyID, scope, canonic_name, characterID)
  
  return false if !::File.exists?(file_path)
  
  cached = ::File.open(file_path) do |f|
    parser = EveApi::Parsers::Cache.new(f.read)
    
    cached_until, _ = parser.parse
    
    Time.now <= cached_until
  end
  
  return cached
end

#retrieve(keyID, scope, canonic_name, characterID) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/eve_api/caches/file.rb', line 24

def retrieve(keyID, scope, canonic_name, characterID)
  rendered_path = render_path(keyID, scope, canonic_name, characterID)
  
  if cached?(keyID, scope, canonic_name, characterID)
    return ::File.open(rendered_path) do |f|
      f.read
    end
  else
    return nil
  end
end

#store!(keyID, scope, canonic_name, characterID, content) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/eve_api/caches/file.rb', line 36

def store!(keyID, scope, canonic_name, characterID, content)
  rendered_path = render_path(keyID, scope, canonic_name, characterID)
  
  FileUtils.mkdir_p(::File.dirname(rendered_path))
  
  ::File.open(rendered_path, "w+") do |f|
    f.write(content.to_s)
  end
  
  return true
end