Sha256: ff571c01f6fbb9cdf99e07da08fa944e6d603a869676e9766c04c5d2f728bb65

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require_relative 'representers/set_representer'
require_relative 'card'
require_relative 'resource'

module MTG
  class Set < Resource
    include Roar::JSON
    include SetRepresenter
    include RestClient
  
    attr_accessor :code, :name, :type, :border, :mkm_id, :mkm_name,
                  :release_date, :gatherer_code, :magic_cards_info_code, :booster,
                  :old_code, :block, :online_only

    # Find a single set by the set code
    #
    # @param code [String] the Set code
    # @return [Set] a Set object
    def self.find(code)
      response = RestClient.get("sets/#{code}")
      
      if response.body['set'].nil?
        raise ArgumentError, 'Set not found'
      end
      
      new.from_json(response.body['set'].to_json)
    end
    
    # Get all sets from a query
    #
    # @return [Array<Set>] Array of Set objects
    def self.all
      get
    end
    
    # Generate a booster pack for a specific set
    #
    # @param code [String] the Set code
    # @return [Array<Card>] Array of Card objects
    def self.generate_booster(code)
      cards = []
      response = RestClient.get("sets/#{code}/booster")
      data = response.body['cards']
      
      data.each do |card|
        cards << Card.new.from_json(card.to_json)
      end
      
      @query = nil
      cards
    end
    
    # Execute a query and convert the response
    # into a list of Set objects
    #
    # @return [Array<Set>] Array of Set objects
    def self.get
      sets = []
      response = RestClient.get('sets', query[:parameters])
      data = response.body['sets']
      
      data.each do |set|
        sets << new.from_json(set.to_json)
      end
      
      @query = nil
      sets
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mtg_sdk-1.1.1 lib/mtg_sdk/set.rb
mtg_sdk-1.1.0 lib/mtg_sdk/set.rb