Class: GathererSetParser::CardParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gatherer_set_parser/card_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card) ⇒ CardParser

Returns a new instance of CardParser.



6
7
8
# File 'lib/gatherer_set_parser/card_parser.rb', line 6

def initialize card
  @body = card
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/gatherer_set_parser/card_parser.rb', line 4

def body
  @body
end

Instance Method Details

#attributesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gatherer_set_parser/card_parser.rb', line 10

def attributes
  { title: title,
    card_type: type,
    sub_type: sub_type,
    power: power,
    toughness: toughness,
    cmc: cmc,
    mana_cost: mana_cost,
    loyalty: loyalty,
    image_url: image_url,
    text: text,
    gatherer_url: gatherer_url,
    rarity: rarity }
end

#cmcObject



53
54
55
# File 'lib/gatherer_set_parser/card_parser.rb', line 53

def cmc
  body.css('.convertedManaCost').text.to_i
end

#gatherer_urlObject



85
86
87
88
89
# File 'lib/gatherer_set_parser/card_parser.rb', line 85

def gatherer_url
  'http://gatherer.wizards.com/Pages/' +
  body.css('.cardTitle > a').attribute('href').
    text.gsub(/\.\.\//, '')
end

#image_urlObject



79
80
81
82
83
# File 'lib/gatherer_set_parser/card_parser.rb', line 79

def image_url
  'http://gatherer.wizards.com/' +
  body.css('.leftCol > a > img').attribute('src').
    text.gsub(/\.\.\//, '')
end

#loyaltyObject



57
58
59
# File 'lib/gatherer_set_parser/card_parser.rb', line 57

def loyalty
  type_box.match(/\(([0-9]*)\)/)[1].to_i rescue nil
end

#mana_costObject



71
72
73
74
75
76
77
# File 'lib/gatherer_set_parser/card_parser.rb', line 71

def mana_cost
  array = []
  body.css('.manaCost >img').each do |img|
    array << img.attribute('alt').text.parse_mana_cost
  end
  array
end

#powerObject



45
46
47
# File 'lib/gatherer_set_parser/card_parser.rb', line 45

def power
  type_box.match(/\(([0-9]*)\//)[1].to_i rescue nil
end

#rarityObject



61
62
63
64
# File 'lib/gatherer_set_parser/card_parser.rb', line 61

def rarity
  body.css('.rightCol > a').first.children.attribute('alt').text.
    match(/\((.*)\)/)[1].to_s
end

#sub_typeObject



38
39
40
41
42
43
# File 'lib/gatherer_set_parser/card_parser.rb', line 38

def sub_type
  type_box.gsub(/\(.*\)/, '').
    match(/ ([a-z0-9 ]*)\z/i)[1].to_s.lstrip.strip
rescue
  nil
end

#textObject



66
67
68
69
# File 'lib/gatherer_set_parser/card_parser.rb', line 66

def text
  body.xpath('//div[@class="rulesText"]//img[@alt]').each{ |i| i.swap( i['alt'].parse_mana_cost ) }
  body.css('.rulesText').text.gsub(/[ ]{2,}/, ' ')
end

#titleObject



25
26
27
# File 'lib/gatherer_set_parser/card_parser.rb', line 25

def title
  body.css('.cardTitle > a').text
end

#toughnessObject



49
50
51
# File 'lib/gatherer_set_parser/card_parser.rb', line 49

def toughness
  type_box.match(/\/([0-9]*)\)/)[1].to_i rescue nil
end

#typeObject



34
35
36
# File 'lib/gatherer_set_parser/card_parser.rb', line 34

def type
  type_box.match(/[a-z0-9 ]*/i).to_s.strip
end

#type_boxObject



29
30
31
32
# File 'lib/gatherer_set_parser/card_parser.rb', line 29

def type_box
  body.css('span.typeLine').text.
    gsub(/\n/, '').gsub(/[ ]{2,}/, ' ').strip.lstrip
end