class Cards

Attributes

no_cache[R]

Public Class Methods

get(no_cache = nil) click to toggle source
# File lib/cards.rb, line 7
def self.get(no_cache = nil)
  new(no_cache).get
end
new(no_cache) click to toggle source
# File lib/cards.rb, line 19
def initialize(no_cache)
  @no_cache = no_cache
end

Public Instance Methods

get() click to toggle source
# File lib/cards.rb, line 11
def get
  JSON.parse(cards)
end

Private Instance Methods

cache_empty?() click to toggle source
# File lib/cards.rb, line 33
def cache_empty?
  File.size(cache_file_path).zero?
end
cache_file_path() click to toggle source
# File lib/cards.rb, line 43
def cache_file_path
  File.expand_path('../cache/cards.json', __dir__)
end
cards() click to toggle source
# File lib/cards.rb, line 23
def cards
  fetch_and_save_cards_from_api if no_cache || cache_empty?

  read_cache
end
fetch_and_save_cards_from_api() click to toggle source
# File lib/cards.rb, line 37
def fetch_and_save_cards_from_api
  File.open(cache_file_path, 'w') do |file|
    file.write(MtgApiResponse.results)
  end
end
read_cache() click to toggle source
# File lib/cards.rb, line 29
def read_cache
  File.read(cache_file_path)
end