Class: CoinSync::PriceLoaders::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/coinsync/price_loaders/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Cache

Returns a new instance of Cache.



7
8
9
10
11
12
13
14
15
16
# File 'lib/coinsync/price_loaders/cache.rb', line 7

def initialize(name)
  @name = name
  @filename = "data/prices/#{name}.json"

  if File.exist?(@filename)
    @prices = JSON.parse(File.read(@filename))
  else
    @prices = {}
  end
end

Instance Method Details

#[](coin, time) ⇒ Object



18
19
20
21
# File 'lib/coinsync/price_loaders/cache.rb', line 18

def [](coin, time)
  @prices[coin.code] ||= {}
  @prices[coin.code][time.to_i.to_s]
end

#[]=(coin, time, price) ⇒ Object



23
24
25
26
# File 'lib/coinsync/price_loaders/cache.rb', line 23

def []=(coin, time, price)
  @prices[coin.code] ||= {}
  @prices[coin.code][time.to_i.to_s] = price
end

#saveObject



28
29
30
31
# File 'lib/coinsync/price_loaders/cache.rb', line 28

def save
  FileUtils.mkdir_p(File.dirname(@filename))
  File.write(@filename, JSON.generate(@prices))
end