public
Last active — forked from roadhouse/main.rb

  • Download Gist
card_data.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
class CardData
def initialize(node)
@card_data = node
end
 
def name
@card_data.search("name").text
end
def image
@card_data.search("set").attribute("picURL").text
end
 
def set
@card_data.search("set").text
end
 
def color
@card_data.search("color").text
end
 
def manacost
@card_data.search("manacost").text
end
 
def card_type
@card_data.search("type").text
end
 
def text
@card_data.search("text").text
end
def loyalty
@card_data.search("loyalty").text
end
 
def power
@card_data.search("pt").split("/").first
end
def toughness
@card_data.search("pt").split("/").last
end
 
// se não quiser ficar chamando um por um
// isso também pode ser dinâmico, usando uma constante ou pegando os métodos locais exceto o construtor
def attributes
{
:name => name,
:image => image,
:set => set,
:color => color,
:manacost => manacost,
:card_type => card_type,
:text => text,
:loyalty => loyalty,
:power => power,
:toughness => toughness
}
end
end
card_xml_parser.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class CardXmlParser
def initialize(path, engine = Nokogiri::XML)
@path = path
@engine = engine
parse!
end
 
def each_card(&block)
@parsed_xml.map do |card|
yield CardData.new(@parsed_xml.new(card))
end
end
private
def parse!
@parsed_xml = @engine(File.open(@path)).search("card")
end
end
main.rb
Ruby
1 2 3 4 5
parser = CardXmlParser.new("path")
parser.each_card { |card_data| Card.create!(card.<attribute>,..., :without_protection => true) }
ou
parser.each_card { |card_data| Card.create!(card.attributes, :without_protection => true) }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.