Skip to content

Instantly share code, notes, and snippets.

@adback03
Last active March 29, 2016 02:04
Show Gist options
  • Save adback03/4faf7f34ad2c519475ea to your computer and use it in GitHub Desktop.
Save adback03/4faf7f34ad2c519475ea to your computer and use it in GitHub Desktop.
Sample usage and an Example application for the MTG SDK for Ruby
# card.rb
require 'sequel'
module SampleApplication
class Card < Sequel::Model
end
end
CREATE TABLE cards
(
id serial NOT NULL,
name text,
set text,
multiverse_id integer,
CONSTRAINT cards_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE cards
OWNER TO guest;
# configuration.rb
require 'yaml'
module SampleApplication
class Configuration
attr_accessor :host, :port, :database, :user, :password
def initialize
config = YAML.load_file("database.yml")
@host = config['default']['host']
@port = config['default']['port']
@database = config['default']['database']
@user = config['default']['user']
@password = config['default']['password']
end
end
end
development: &development
host: 127.0.0.1
port: 5432
database: sample_application
username: guest
password: password1234
default:
<<: *development
require 'mtg_sdk'
# Configuration
MTG.configure do |config|
# Target a different api_version. The default is version 1
config.api_version = 2
end
# Get all Cards (long process, but handles paging through the data for you)
cards = MTG::Card.all
# Get a specific card
card = MTG::Card.find(88803)
# Filter Cards
# You can chain 'where' clauses together. The key of the hash
# should be the URL parameter you are trying to filter on
cards = MTG::Card.where(supertypes: 'legendary')
.where(types: 'creature')
.where(colors: 'red,white')
.all
# Get cards on a specific page / pageSize
cards = MTG::Card.where(page: 50).where(pageSize: 500).all
# Get all Sets
sets = MTG::Sets.all
# Get a specific Set
set = MTG::Set.find('ktk')
# Get all Sets by filtering on set name
sets = MTG::Set.where(name: 'khans').all
# Rakefile
require_relative 'sample_application'
task :run do
SampleApplication.load_legendary_cards
end
# sample_application.rb
require_relative 'configuration'
require 'mtg_sdk'
require 'sequel'
module SampleApplication
class << self
attr_writer :configuration
end
def self.configuration
@configuration ||= Configuration.new
end
def self.load_legendary_cards
Sequel::Model.db = Sequel.connect("postgres://#{configuration.user}:#{configuration.password}@#{configuration.host}:#{configuration.port}/#{configuration.database}")
require_relative 'card'
puts 'Getting all legendary cards...'
cards = MTG::Card.where(supertypes: 'legendary')
.where(types: 'creature')
.all
cards.each {|card|
puts card.name
Card.insert(name: card.name, set: card.set, multiverse_id: card.multiverse_id)
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment