Class: Cliptic::Main::Fetch::Cache

Inherits:
Request
  • Object
show all
Defined in:
lib/cliptic/main.rb

Constant Summary collapse

Path =
"#{Dir.home}/.cache/cliptic"

Constants inherited from Request

Request::URL

Instance Attribute Summary

Attributes inherited from Request

#data

Instance Method Summary collapse

Methods inherited from Request

#raw, #send_request, #valid_input?

Constructor Details

#initialize(date: Date.today) ⇒ Cache

Returns a new instance of Cache.



154
155
156
157
# File 'lib/cliptic/main.rb', line 154

def initialize(date:Date.today)
  super(date:date)
  make_cache_dir
end

Instance Method Details

#date_cached?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/cliptic/main.rb', line 165

def date_cached?
  File.exist?(file_path)
end

#file_pathObject



168
169
170
# File 'lib/cliptic/main.rb', line 168

def file_path
  "#{Path}/#{data[:date]}"
end

#make_cache_dirObject



162
163
164
# File 'lib/cliptic/main.rb', line 162

def make_cache_dir
  FileUtils.mkdir_p(Path) unless Dir.exist?(Path)
end

#queryObject



158
159
160
161
# File 'lib/cliptic/main.rb', line 158

def query
  date_cached? ? read_cache : send_request
    .tap{|str| write_cache(str)}
end

#read_cacheObject



171
172
173
# File 'lib/cliptic/main.rb', line 171

def read_cache
  File.read(file_path)
end

#write_cache(str) ⇒ Object



174
175
176
# File 'lib/cliptic/main.rb', line 174

def write_cache(str)
  File.write(file_path, str)
end