Class: RuneterraCards::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/runeterra_cards/metadata.rb

Overview

Note:

This class cannot yet handle metadata for multiple locales at the same time. You will need multiple instances of this class, one for each locale, if you wish to handle multiple locales at this time.

Loads, stores, and retrieves data for cards from Legends of Runeterra Data Dragon set files. N.B. this does not ship with any metadata of its own, you must provide set files yourself and load them before this class will do anything.

Examples:

Load data and retrieve data for a card

m = RuneterraCards::Metadata.new
m.add_set_file('./set1-en_us.json')
m.lookup_card('01DE031') #=> CardMetadata

Load data from multiple sets

m = RuneterraCards::Metadata.new
m.add_set_file('./set1-en_us.json')
m.add_set_file('./set2-en_us.json')

Instance Method Summary collapse

Constructor Details

#initializeMetadata

Returns a new instance of Metadata.



23
24
25
# File 'lib/runeterra_cards/metadata.rb', line 23

def initialize
  @cards = {}
end

Instance Method Details

#add_set_file(file_path) ⇒ Object

TODO:

document and test exceptions that can be thrown here

Load card data from a Legends of Runeterra Data Dragon set file, e.g. “set1-en_us.json”

Parameters:

  • file_path (String)

    The path to the metadata file to read



30
31
32
33
34
35
36
37
# File 'lib/runeterra_cards/metadata.rb', line 30

def add_set_file(file_path)
  file = File.read(file_path)
  data = JSON.parse(file)
  data.each do |card_data|
    card = CardMetadata.new(card_data)
    @cards[card.card_code] = card
  end
end

#all_collectibleHash<String,CardMetadata>

Returns all cards in the metadata set that are collectible

Returns:

See Also:



50
51
52
# File 'lib/runeterra_cards/metadata.rb', line 50

def all_collectible
  @cards.select { |_, card| card.collectible? }
end

#full_setCardSet

Returns a [CardSet] that represents a complete card collection. That is: 3x of every card that is collectible.

Returns:



57
58
59
# File 'lib/runeterra_cards/metadata.rb', line 57

def full_set
  CardSet.new(all_collectible.keys.each_with_object({}) { |code, result| result[code] = 3 })
end

#lookup_card(card_code) ⇒ CardMetadata

TODO:

document errors if card_code doesn’t exist

Fetch card metadata for a card via its card code

Parameters:

  • card_code (String)

    card code, e.g. 01DE031

Returns:



43
44
45
# File 'lib/runeterra_cards/metadata.rb', line 43

def lookup_card(card_code)
  @cards.fetch(card_code)
end