Class: Gatherer::CardParser

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

Constant Summary collapse

SELECTORS =
{
  title: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_nameRow',
  types: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_typeRow',
  cmc: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_cmcRow',
  mana: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_manaRow',
  subtypes: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_typeRow',
  text: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_textRow',
  flavor_text: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_flavorRow',
  set: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_setRow',
  other_sets: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_otherSetsRow',
  pt: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_ptRow',
  number: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_numberRow',
  illustrator: 'div#ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_artistRow'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html, validate_card_markup = true) ⇒ CardParser

Returns a new instance of CardParser.



20
21
22
23
# File 'lib/gatherer/card_parser.rb', line 20

def initialize(html, validate_card_markup = true)
  @document = Nokogiri::HTML(html)
  validate if validate_card_markup
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



3
4
5
# File 'lib/gatherer/card_parser.rb', line 3

def document
  @document
end

Instance Method Details

#abbreviation(title, parsed_text = nil) ⇒ Object



170
171
172
173
# File 'lib/gatherer/card_parser.rb', line 170

def abbreviation(title, parsed_text = nil)
  parsed_text ||= extract_abbreviation(title)
  parsed_text.split('&set=').last.split('&').first if parsed_text
end

#converted_mana_cost(parsed_text = extract_converted_mana_cost) ⇒ Object



54
55
56
# File 'lib/gatherer/card_parser.rb', line 54

def converted_mana_cost(parsed_text = extract_converted_mana_cost)
  parsed_text.strip.to_i if parsed_text
end

#current_printing(parsed_text = extract_current_printing) ⇒ Object



141
142
143
# File 'lib/gatherer/card_parser.rb', line 141

def current_printing(parsed_text = extract_current_printing)
  parse_printing(parsed_text) if parsed_text
end

#current_printing?(title) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
149
# File 'lib/gatherer/card_parser.rb', line 145

def current_printing?(title)
  current_printing_text = extract_current_printing
  current_title = current_printing_text.split(' (').first if current_printing_text
  title == current_title
end

#extract_abbreviation(title) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gatherer/card_parser.rb', line 175

def extract_abbreviation(title)
  images = find_row(:set).css('img')

  unless images.empty?
    images += document.css(SELECTORS[:other_sets]).css('img')

    images.map do |image|
      image['src'] if image['title'].include?(title)
    end.compact.uniq.first
  end
end

#extract_converted_mana_costObject



58
59
60
61
# File 'lib/gatherer/card_parser.rb', line 58

def extract_converted_mana_cost
  row = find_row(:cmc)
  cmc_line = row.css('div.value').text unless row.empty?
end

#extract_current_printingObject



151
152
153
154
# File 'lib/gatherer/card_parser.rb', line 151

def extract_current_printing
  row = find_row(:set)
  row.css('img').map { |img| img['title'] }.first unless row.empty?
end

#extract_flavor_textObject



115
116
117
118
# File 'lib/gatherer/card_parser.rb', line 115

def extract_flavor_text
  row = find_row(:flavor_text)
  row.css('div.cardtextbox').text unless row.empty?
end

#extract_illustratorObject



220
221
222
223
# File 'lib/gatherer/card_parser.rb', line 220

def extract_illustrator
  row = find_row(:illustrator)
  row.css('div.value').text unless row.empty?
end

#extract_mana_costObject



68
69
70
71
72
73
74
# File 'lib/gatherer/card_parser.rb', line 68

def extract_mana_cost
  row = find_row(:mana)

  if row
    mana_cost_line = row.css('div.value').css('img').map { |img| img['alt'] }
  end
end

#extract_numberObject



211
212
213
214
# File 'lib/gatherer/card_parser.rb', line 211

def extract_number
  row = find_row(:number)
  row.css('div.value').text unless row.empty?
end

#extract_other_printingsObject



165
166
167
168
# File 'lib/gatherer/card_parser.rb', line 165

def extract_other_printings
  row = find_row(:other_sets)
  row.css('img').map { |img| img['title'] } if row
end

#extract_power_toughnessObject



195
196
197
198
# File 'lib/gatherer/card_parser.rb', line 195

def extract_power_toughness
  row = find_row(:pt)
  row.css('div.value').text unless row.empty?
end

#extract_printingsObject



137
138
139
# File 'lib/gatherer/card_parser.rb', line 137

def extract_printings
  ([extract_current_printing] + extract_other_printings).uniq
end

#extract_subtypesObject



84
85
86
87
# File 'lib/gatherer/card_parser.rb', line 84

def extract_subtypes
  row = find_row(:subtypes)
  row.css('div.value').text unless row.empty?
end

#extract_textObject



93
94
95
96
97
98
99
100
101
# File 'lib/gatherer/card_parser.rb', line 93

def extract_text
  row = find_row(:text)

  unless row.empty?
    text_element = row.first
    text_element.inner_html = replace_mana_symbols(text_element.inner_html)
    text_element.css('div.value div.cardtextbox').map(&:text)
  end
end

#extract_titleObject



37
38
39
40
# File 'lib/gatherer/card_parser.rb', line 37

def extract_title
  row = find_row(:title)
  title_line = row.css('div.value').text if row
end

#extract_typesObject



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

def extract_types
  row = find_row(:types)
  row.css('div.value').text unless row.empty?
end

#find_row(css) ⇒ Object



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

def find_row(css)
  document.css(SELECTORS[css])
end

#flavor_text(parsed_text = extract_flavor_text) ⇒ Object



111
112
113
# File 'lib/gatherer/card_parser.rb', line 111

def flavor_text(parsed_text = extract_flavor_text)
  parsed_text.strip if parsed_text
end

#illustrator(parsed_text = extract_illustrator) ⇒ Object



216
217
218
# File 'lib/gatherer/card_parser.rb', line 216

def illustrator(parsed_text = extract_illustrator)
  parsed_text.strip if parsed_text
end

#loyalty(parsed_text = extract_power_toughness) ⇒ Object

gatherer uses the pt row to display loyalty



201
202
203
204
205
# File 'lib/gatherer/card_parser.rb', line 201

def loyalty(parsed_text = extract_power_toughness)
  if parsed_text && !parsed_text.include?('/')
    parsed_text.to_i if parsed_text.to_i > 0
  end
end

#mana_cost(parsed_text = extract_mana_cost) ⇒ Object



63
64
65
66
# File 'lib/gatherer/card_parser.rb', line 63

def mana_cost(parsed_text = extract_mana_cost)
  color_map = Gatherer::Card::COLOR_SYMBOLS
  parsed_text.map { |mana| color_map[mana] ? color_map[mana] : mana }.join
end

#number(parsed_text = extract_number) ⇒ Object



207
208
209
# File 'lib/gatherer/card_parser.rb', line 207

def number(parsed_text = extract_number)
  parsed_text.to_i
end

#other_printings(parsed_text = extract_other_printings) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/gatherer/card_parser.rb', line 156

def other_printings(parsed_text = extract_other_printings)
  parsed_text.map do |printing|
    Printing.new(
      expansion: Expansion.new(:title => parsed_text.split(' (').first),
      rarity: parsed_text.split(' (').last.chop
    )
  end
end

#parse_printing(printing) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/gatherer/card_parser.rb', line 120

def parse_printing(printing)
  if printing
    title = printing.split(' (').first
    Printing.new(
      expansion: Expansion.new(title: title, abbreviation: abbreviation(title)),
      rarity: printing.split(' (').last.chop,
      number: (number if current_printing?(title))
    )
  end
end

#power(parsed_text = extract_power_toughness) ⇒ Object



187
188
189
# File 'lib/gatherer/card_parser.rb', line 187

def power(parsed_text = extract_power_toughness)
  parsed_text.split('/').first if parsed_text && parsed_text.include?('/')
end

#printings(parsed_text = extract_printings) ⇒ Object



131
132
133
134
135
# File 'lib/gatherer/card_parser.rb', line 131

def printings(parsed_text = extract_printings)
  parsed_text.map do |printing|
    parse_printing(printing)
  end
end

#replace_mana_symbols(html) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/gatherer/card_parser.rb', line 103

def replace_mana_symbols(html)
  while image = html.match(/<img.*?alt="([^"]*)"[^>]*>/)
    html.gsub!(image.to_s, "{{#{image.captures.first}}}")
  end

  html
end

#subtypes(parsed_text = extract_subtypes) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/gatherer/card_parser.rb', line 76

def subtypes(parsed_text = extract_subtypes)
  if parsed_text && parsed_text.include?("\u2014")
    parsed_text.split("\u2014").last.split(' ').map { |type| type.strip }
  else
    []
  end
end

#text(parsed_text = extract_text) ⇒ Object



89
90
91
# File 'lib/gatherer/card_parser.rb', line 89

def text(parsed_text = extract_text)
  parsed_text.map { |line| line.strip }.compact.join("\n") if parsed_text
end

#title(parsed_text = extract_title) ⇒ Object



33
34
35
# File 'lib/gatherer/card_parser.rb', line 33

def title(parsed_text = extract_title)
  parsed_text.strip
end

#to_hashObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/gatherer/card_parser.rb', line 225

def to_hash
  {
    title: title,
    types: types,
    mana_cost: mana_cost,
    converted_mana_cost: converted_mana_cost,
    subtypes: subtypes,
    text: text,
    flavor_text: flavor_text,
    printings: printings.map(&:to_hash),
    power: power,
    toughness: toughness,
    loyalty: loyalty,
    number: number,
    illustrator: illustrator
  }
end

#toughness(parsed_text = extract_power_toughness) ⇒ Object



191
192
193
# File 'lib/gatherer/card_parser.rb', line 191

def toughness(parsed_text = extract_power_toughness)
  parsed_text.split('/').last if parsed_text && parsed_text.include?('/')
end

#types(parsed_text = extract_types) ⇒ Object



42
43
44
45
46
47
# File 'lib/gatherer/card_parser.rb', line 42

def types(parsed_text = extract_types)
  if parsed_text
    types = parsed_text.strip.split("\u2014").first
    types.split.flatten
  end
end

#validateObject

Raises:



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

def validate
  raise CardNotFound if document.css(SELECTORS[:illustrator]).empty?
end