Class: SolarTerms24::Cache

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

Overview

:nodoc:

Constant Summary collapse

TIME_FORMAT =
'%Y-%m-%d %H:%M:%S.%L%:z'
CACHE_DIR =
File.expand_path(File.join(__dir__, 'db'))

Class Method Summary collapse

Class Method Details

.has?(year) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/solar_terms_24/cache.rb', line 11

def self.has?(year)
  File.exist?("#{CACHE_DIR}/#{year}.json")
end

.load(year) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/solar_terms_24/cache.rb', line 15

def self.load(year)
  file = File.read("#{CACHE_DIR}/#{year}.json")
  JSON.parse(file, symbolize_names: true).tap do |json|
    json.each_key do |key|
      json[key] = DateTime.parse(json[key])
    end
  end
end

.save(year, data) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/solar_terms_24/cache.rb', line 24

def self.save(year, data)
  File.open("#{CACHE_DIR}/#{year}.json", 'w') do |f|
    json = data.dup
    json.each_key do |key|
      json[key] = json[key].strftime(TIME_FORMAT)
    end
    f.write(JSON.pretty_generate(json))
  end
end