class Tamiyo::ScrapeParser

Public Instance Methods

adjust_for_bottom_flip_card() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 86
def adjust_for_bottom_flip_card
  bottom_card_name = bottom_card_of @name
  if bottom_card_name
    bottom_card = @collection.delete bottom_card_name
    new_card.with_flip_bottom bottom_card
  end
end
adjust_for_split_card(card, part_name) click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 68
def adjust_for_split_card(card, part_name)
  card = if @name.start_with? part_name
    card.with_left_split @cost, @text
  else
    card.with_right_split @cost, @text
  end
  card.with_extra_colors colors
end
adjust_for_top_flip_card(card, bottom_name) click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 77
def adjust_for_top_flip_card(card, bottom_name)
  if card
    card.with_flip_bottom new_card.with_name bottom_name
  else
    remember_top_bottom_link @name, bottom_name
    new_card.with_name bottom_name
  end
end
adjust_for_transform_card() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 94
def adjust_for_transform_card
  back_name = fetch_back_card_of @name
  new_card.with_transform_hint back_name if back_name
end
back_card_of(first)
Alias for: bottom_card_of
beautify(name) click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 147
def beautify(name)
  name.gsub(for_linked_card, '')
    .gsub('XX', '')
    .gsub('Æ', 'AE')
    .tr('’', "'")
end
bottom_card_of(first) click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 124
def bottom_card_of(first)
  @linked_cards[first]
end
Also aliased as: back_card_of
colors() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 166
def colors
  @color ||= ''
  {'W' => [' is white.', 'White'],
   'U' => [' is blue.', 'Blue'],
   'B' => [' is black.', 'Black'],
   'R' => [' is red.', 'Red'],
   'G' => [' is green.', 'Green']
  }.each_with_object [] do |(color, (text, identity)), colors|
    if @cost.include?(color) ||
       @text.include?(@name + text) ||
       @color.include?(identity)
      colors << color
    end
  end
end
fetch_back_card_of(card_name) click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 99
def fetch_back_card_of(card_name)
  unless remembers_front_back_link card_name
    parts = pull_card_details
    return false if parts == :single
    front_name, back_name = parts
    remember_front_back_link front_name, back_name
    remember_front_back_link back_name, false
  end
  back_card_of card_name
end
flip_card() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 158
def flip_card
  @text[match_flip_text]
end
main_type_based_on(type) click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 197
def main_type_based_on(type)
  # The last word before a dash or $
  type.match(/(\w+)\s*(-|$)/)[1]
end
new_card() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 134
def new_card
  Card.with name: @name,
            cost: @cost,
            type: @type,
            text: @text,
            pt: @pt,
            loyalty: @loyalty,
            colors: colors,
            table_row: table_row,
            played_tapped: played_tapped,
            picture_url: picture_url
end
parse_card() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 48
def parse_card
  linked_card = @name.match for_linked_card
  @name = beautify @name

  adjusted_card = if linked_card
    part_name = beautify linked_card[:part_name]
    card = @collection[@name]
    if split_card
      adjust_for_split_card card, part_name if card
    else
      adjust_for_top_flip_card card, part_name
    end
  elsif flip_card
    adjust_for_bottom_flip_card
  elsif transform_in_card_text
    adjust_for_transform_card
  end
  adjusted_card || new_card
end
parse_cards_from(xml) click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 12
def parse_cards_from(xml)
  @collection = {}
  @linked_cards = {}
  xml.each do |tr|
    tds = tr.css 'td'
    if tds.length == 2
      left, right = tds
      right_text = right.content.tr '—', '-'
      case fold left.content
      when 'Name'
        href = right.at_css('a')['href']
        @id = href.match(/multiverseid=(.*)/)[1]
        @name = fold right_text
      when 'Cost:'
        @cost = fold right_text
      when 'Color:'
        @color = fold right_text
      when 'Type:'
        @type = fold right_text
      when 'Pow/Tgh:'
        @pt = fold(right_text).tr '()', ''
        @pt = nil if @pt.empty?
      when 'Loyalty:'
        @loyalty = right_text.strip.tr '()', ''
      when 'Rules Text:'
        @text = right_text.strip.each_line.map(&:strip).join "\n"
      end
    else
      card = parse_card
      @collection[card.name] = card
      @color = @pt = @loyalty = nil
    end
  end
  @collection.values
end
picture_url() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 206
def picture_url
  host = 'http://gatherer.wizards.com/'
  path = 'Handlers/Image.ashx'
  query = '?multiverseid=%s&amp;type=card' % @id
  "#{host}#{path}#{query}"
end
played_tapped() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 202
def played_tapped
  @text.include?(@name + ' enters the battlefield tapped.')
end
pull() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 8
def pull
  parse_cards_from @chain.xml_spoiler_rows
end
pull_card_details() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 110
def pull_card_details
  xml = @chain.xml_card_name_details @id
  return :single if xml.one?
  xml.map do |name_row|
    beautify fold name_row.at_css('.value').content
  end
end
split_card() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 154
def split_card
  @name.include? '//'
end
table_row() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 182
def table_row
  mana_source = @type.end_with?('Artifact') &&
    @text.include?('{T}') && @text.include?('to your mana pool')
  case main_type_based_on @type
  when /Land/, mana_source
    :first
  when /Sorcery|Instant/
    :stack
  when /Creature/
    :third
  else
    :second
  end
end
transform_in_card_text() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 162
def transform_in_card_text
  @text[match_transform_text]
end

Private Instance Methods

fold(text) click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 215
def fold(text)
  text.strip.gsub /\s+/, ' '
end
for_linked_card()
Alias for: match_linked_card
match_flip_text() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 225
def match_flip_text
  %r{flip (it|#{@name}).|flipped.}
end
match_linked_card() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 219
def match_linked_card
  / \((?<part_name>.*)\)/
end
Also aliased as: for_linked_card
match_transform_text() click to toggle source
# File lib/tamiyo/scrape_parser.rb, line 229
def match_transform_text
  /transform/i
end