Class: Tamiyo::ScrapeParser

Inherits:
Object
  • Object
show all
Includes:
Process::Node
Defined in:
lib/tamiyo/scrape_parser.rb

Instance Method Summary collapse

Methods included from Process::Node

#affix

Instance Method Details

#adjust_for_bottom_flip_cardObject



86
87
88
89
90
91
92
# 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) ⇒ Object



68
69
70
71
72
73
74
75
# 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) ⇒ Object



77
78
79
80
81
82
83
84
# 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_cardObject



94
95
96
97
# 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

#beautify(name) ⇒ Object



147
148
149
150
151
152
# 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) ⇒ Object Also known as: back_card_of



124
125
126
# File 'lib/tamiyo/scrape_parser.rb', line 124

def bottom_card_of(first)
  @linked_cards[first]
end

#colorsObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# 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_cardObject



158
159
160
# File 'lib/tamiyo/scrape_parser.rb', line 158

def flip_card
  @text[match_flip_text]
end

#main_type_based_on(type) ⇒ Object



197
198
199
200
# 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_cardObject



134
135
136
137
138
139
140
141
142
143
144
145
# 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_cardObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



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
# 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_urlObject



206
207
208
209
210
211
# 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_tappedObject



202
203
204
# File 'lib/tamiyo/scrape_parser.rb', line 202

def played_tapped
  @text.include?(@name + ' enters the battlefield tapped.')
end

#pullObject



8
9
10
# File 'lib/tamiyo/scrape_parser.rb', line 8

def pull
  parse_cards_from @chain.xml_spoiler_rows
end

#pull_card_detailsObject



110
111
112
113
114
115
116
# 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


118
119
120
# File 'lib/tamiyo/scrape_parser.rb', line 118

def remember_top_bottom_link(first, second)
  @linked_cards.store first, second
end


130
131
132
# File 'lib/tamiyo/scrape_parser.rb', line 130

def remembers_front_back_link(first)
  @linked_cards.include? first
end

#split_cardObject



154
155
156
# File 'lib/tamiyo/scrape_parser.rb', line 154

def split_card
  @name.include? '//'
end

#table_rowObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# 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_textObject



162
163
164
# File 'lib/tamiyo/scrape_parser.rb', line 162

def transform_in_card_text
  @text[match_transform_text]
end