Skip to content

Instantly share code, notes, and snippets.

@rinaldifonseca
Created November 8, 2012 14:59
Show Gist options
  • Save rinaldifonseca/4039292 to your computer and use it in GitHub Desktop.
Save rinaldifonseca/4039292 to your computer and use it in GitHub Desktop.
c = XmlImport.new("path", CockatriceXmlParser).process!
c.each { |card_attributes| Card.create!(card_attributes, :without_protection => true) }
class CockatriceXmlParser
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
end
class XmlImport
def initialize(path, parser)
@path = path
@parser = parser
end
def process!
repository.map do |card|
card_data = @parser.new(card)
{
:name => card_data.name,
:image => card_data.image,
:set => card_data.set,
:color => card_data.color,
:manacost => card_data.manacost,
:card_type => card_data.card_type,
:text => card_data.text,
:loyalty => card_data.loyalty,
:power => card_data.power,
:toughness => card_data.toughness
}
end
end
def repository
Nokogiri::XML(File.open(@path)).search("card")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment