gatherer 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,34 +26,38 @@ module Gatherer
26
26
  raise CardNotFound if document.css(SELECTORS[:illustrator]).empty?
27
27
  end
28
28
 
29
+ def find_row(css)
30
+ document.css(SELECTORS[css])
31
+ end
32
+
29
33
  def title(parsed_text = extract_title)
30
34
  parsed_text.strip
31
35
  end
32
36
 
33
37
  def extract_title
34
- row = document.css(SELECTORS[:title])
35
- return '' unless row
36
- title_line = row.css('div.value').text
38
+ row = find_row(:title)
39
+ title_line = row.css('div.value').text if row
37
40
  end
38
41
 
39
42
  def types(parsed_text = extract_types)
40
- [parsed_text.strip.split("\u2014").first.split].flatten
43
+ if parsed_text
44
+ types = parsed_text.strip.split("\u2014").first
45
+ types.split.flatten
46
+ end
41
47
  end
42
48
 
43
49
  def extract_types
44
- row = document.css(SELECTORS[:types])
45
- return '' unless row
46
- type_line = row.css('div.value').text
50
+ row = find_row(:types)
51
+ row.css('div.value').text unless row.empty?
47
52
  end
48
53
 
49
54
  def converted_mana_cost(parsed_text = extract_converted_mana_cost)
50
- parsed_text.strip.to_i
55
+ parsed_text.strip.to_i if parsed_text
51
56
  end
52
57
 
53
58
  def extract_converted_mana_cost
54
- row = document.css(SELECTORS[:cmc])
55
- return '' unless row
56
- cmc_line = row.css('div.value').text
59
+ row = find_row(:cmc)
60
+ cmc_line = row.css('div.value').text unless row.empty?
57
61
  end
58
62
 
59
63
  def mana_cost(parsed_text = extract_mana_cost)
@@ -62,13 +66,15 @@ module Gatherer
62
66
  end
63
67
 
64
68
  def extract_mana_cost
65
- row = document.css(SELECTORS[:mana])
66
- return '' unless row
67
- mana_cost_line = row.css('div.value').css('img').map { |img| img['alt'] }
69
+ row = find_row(:mana)
70
+
71
+ if row
72
+ mana_cost_line = row.css('div.value').css('img').map { |img| img['alt'] }
73
+ end
68
74
  end
69
75
 
70
76
  def subtypes(parsed_text = extract_subtypes)
71
- if parsed_text.include?("\u2014")
77
+ if parsed_text && parsed_text.include?("\u2014")
72
78
  parsed_text.split("\u2014").last.split(' ').map { |type| type.strip }
73
79
  else
74
80
  []
@@ -76,20 +82,22 @@ module Gatherer
76
82
  end
77
83
 
78
84
  def extract_subtypes
79
- row = document.css(SELECTORS[:subtypes])
80
- return '' unless row
81
- row.css('div.value').text
85
+ row = find_row(:subtypes)
86
+ row.css('div.value').text unless row.empty?
82
87
  end
83
88
 
84
89
  def text(parsed_text = extract_text)
85
- parsed_text.map { |line| line.strip }.compact.join("\n")
90
+ parsed_text.map { |line| line.strip }.compact.join("\n") if parsed_text
86
91
  end
87
92
 
88
93
  def extract_text
89
- row = document.css(SELECTORS[:text]).first
90
- return '' unless row
91
- row.inner_html = replace_mana_symbols(row.inner_html)
92
- row.css('div.value div.cardtextbox').map(&:text)
94
+ row = find_row(:text)
95
+
96
+ unless row.empty?
97
+ text_element = row.first
98
+ text_element.inner_html = replace_mana_symbols(text_element.inner_html)
99
+ text_element.css('div.value div.cardtextbox').map(&:text)
100
+ end
93
101
  end
94
102
 
95
103
  def replace_mana_symbols(html)
@@ -101,22 +109,23 @@ module Gatherer
101
109
  end
102
110
 
103
111
  def flavor_text(parsed_text = extract_flavor_text)
104
- parsed_text.strip
112
+ parsed_text.strip if parsed_text
105
113
  end
106
114
 
107
115
  def extract_flavor_text
108
- row = document.css(SELECTORS[:flavor_text])
109
- return '' unless row
110
- row.css('div.cardtextbox').text
116
+ row = find_row(:flavor_text)
117
+ row.css('div.cardtextbox').text unless row.empty?
111
118
  end
112
119
 
113
120
  def parse_printing(printing)
114
- title = printing.split(' (').first
115
- Printing.new(
116
- expansion: Expansion.new(title: title, abbreviation: abbreviation(title)),
117
- rarity: printing.split(' (').last.chop,
118
- number: (number if current_printing?(title))
119
- )
121
+ if printing
122
+ title = printing.split(' (').first
123
+ Printing.new(
124
+ expansion: Expansion.new(title: title, abbreviation: abbreviation(title)),
125
+ rarity: printing.split(' (').last.chop,
126
+ number: (number if current_printing?(title))
127
+ )
128
+ end
120
129
  end
121
130
 
122
131
  def printings(parsed_text = extract_printings)
@@ -130,7 +139,7 @@ module Gatherer
130
139
  end
131
140
 
132
141
  def current_printing(parsed_text = extract_current_printing)
133
- parse_printing(parsed_text)
142
+ parse_printing(parsed_text) if parsed_text
134
143
  end
135
144
 
136
145
  def current_printing?(title)
@@ -140,9 +149,8 @@ module Gatherer
140
149
  end
141
150
 
142
151
  def extract_current_printing
143
- row = document.css(SELECTORS[:set])
144
- return '' unless row
145
- row.css('img').map { |img| img['title'] }.first
152
+ row = find_row(:set)
153
+ row.css('img').map { |img| img['title'] }.first unless row.empty?
146
154
  end
147
155
 
148
156
  def other_printings(parsed_text = extract_other_printings)
@@ -155,9 +163,8 @@ module Gatherer
155
163
  end
156
164
 
157
165
  def extract_other_printings
158
- row = document.css(SELECTORS[:other_sets])
159
- return '' unless row
160
- row.css('img').map { |img| img['title'] }
166
+ row = find_row(:other_sets)
167
+ row.css('img').map { |img| img['title'] } if row
161
168
  end
162
169
 
163
170
  def abbreviation(title, parsed_text = nil)
@@ -166,32 +173,33 @@ module Gatherer
166
173
  end
167
174
 
168
175
  def extract_abbreviation(title)
169
- images = document.css(SELECTORS[:set]).css('img')
170
- return '' unless images
171
- images += document.css(SELECTORS[:other_sets]).css('img')
176
+ images = find_row(:set).css('img')
172
177
 
173
- images.map do |image|
174
- image['src'] if image['title'].include?(title)
175
- end.compact.uniq.first
178
+ unless images.empty?
179
+ images += document.css(SELECTORS[:other_sets]).css('img')
180
+
181
+ images.map do |image|
182
+ image['src'] if image['title'].include?(title)
183
+ end.compact.uniq.first
184
+ end
176
185
  end
177
186
 
178
187
  def power(parsed_text = extract_power_toughness)
179
- parsed_text.split('/').first if parsed_text.include?('/')
188
+ parsed_text.split('/').first if parsed_text && parsed_text.include?('/')
180
189
  end
181
190
 
182
191
  def toughness(parsed_text = extract_power_toughness)
183
- parsed_text.split('/').last if parsed_text.include?('/')
192
+ parsed_text.split('/').last if parsed_text && parsed_text.include?('/')
184
193
  end
185
194
 
186
195
  def extract_power_toughness
187
- row = document.css(SELECTORS[:pt])
188
- return '' unless row
189
- row.css('div.value').text
196
+ row = find_row(:pt)
197
+ row.css('div.value').text unless row.empty?
190
198
  end
191
199
 
192
200
  # gatherer uses the pt row to display loyalty
193
201
  def loyalty(parsed_text = extract_power_toughness)
194
- unless parsed_text.include?('/')
202
+ if parsed_text && !parsed_text.include?('/')
195
203
  parsed_text.to_i if parsed_text.to_i > 0
196
204
  end
197
205
  end
@@ -201,19 +209,17 @@ module Gatherer
201
209
  end
202
210
 
203
211
  def extract_number
204
- row = document.css(SELECTORS[:number])
205
- return '' unless row
206
- row.css('div.value').text
212
+ row = find_row(:number)
213
+ row.css('div.value').text unless row.empty?
207
214
  end
208
215
 
209
216
  def illustrator(parsed_text = extract_illustrator)
210
- parsed_text.strip
217
+ parsed_text.strip if parsed_text
211
218
  end
212
219
 
213
220
  def extract_illustrator
214
- row = document.css(SELECTORS[:illustrator])
215
- return '' unless row
216
- row.css('div.value').text
221
+ row = find_row(:illustrator)
222
+ row.css('div.value').text unless row.empty?
217
223
  end
218
224
 
219
225
  def to_hash
@@ -1,3 +1,3 @@
1
1
  module Gatherer
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gatherer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-16 00:00:00.000000000 Z
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty