CwCardUtils

A Ruby gem for analyzing Magic: The Gathering decklists and calculating various metrics.

Installation

Add this line to your application’s Gemfile:

gem 'cw_card_utils'

And then execute:

bundle install

Configuration

You can configure the card data source used by the library:

# In a Rails initializer (config/initializers/cw_card_utils.rb)
CwCardUtils.configure do |config|
  config.card_data_source = MyCustomDataSource.new
end

# Or set it directly
CwCardUtils.card_data_source = MyCustomDataSource.new

The default data source is CwCardUtils::ScryfallCmcData.instance, which loads card data from a local JSON file.

Usage

Basic Deck Parsing

require 'cw_card_utils'

# Parse a decklist
decklist = <<~DECK
  4 Lightning Bolt
  4 Mountain
  2 Shock
DECK

deck = CwCardUtils::DecklistParser::Parser.new(decklist).parse

# Access deck information
puts deck.mainboard_size  # => 10
puts deck.color_identity  # => ["R"]
puts deck.archetype      # => :aggro

Custom Data Sources

You can implement your own card data source by inheriting from CwCardUtils::CardDataSource:

class MyCustomDataSource < CwCardUtils::CardDataSource
  def find_card(name)
    # Your implementation here
    # Should return a hash with card data or nil
  end
end

# Configure the library to use your data source
CwCardUtils.card_data_source = MyCustomDataSource.new

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cracklingwit/cw_card_utils.