Class: LCBO::ProductPage

Inherits:
Object
  • Object
show all
Includes:
CrawlKit::Page
Defined in:
lib/lcbo/pages/product_page.rb

Instance Method Summary collapse

Methods included from CrawlKit::Page

#[], #as_hash, #fields, #http_method, included, #initialize, #is_parsed?, #parse, #process, #request, #request_prototype

Instance Method Details

#find_info_line(regexp) ⇒ Object


290
291
292
# File 'lib/lcbo/pages/product_page.rb', line 290

def find_info_line(regexp)
  info_cell_lines.select { |l| l =~ regexp }.first
end

#get_info_lines_at_offset(offset) ⇒ Object


279
280
281
282
283
284
# File 'lib/lcbo/pages/product_page.rb', line 279

def get_info_lines_at_offset(offset)
  raw_info_cell_lines.select do |line|
    match = line.scan(/\A[\s]+/)[0]
    match ? offset == match.size : false
  end
end

#has_package?Boolean

Returns:

  • (Boolean)

257
258
259
# File 'lib/lcbo/pages/product_page.rb', line 257

def has_package?
  !info_cell_lines[2].include?('Price:')
end

#info_cell_elementObject


312
313
314
# File 'lib/lcbo/pages/product_page.rb', line 312

def info_cell_element
  doc.css('table[width="478"] td[height="271"] td[colspan="2"].main_font')[0]
end

#info_cell_htmlObject


308
309
310
# File 'lib/lcbo/pages/product_page.rb', line 308

def info_cell_html
  @info_cell_html ||= info_cell_element.inner_html
end

#info_cell_line_after(item) ⇒ Object


304
305
306
# File 'lib/lcbo/pages/product_page.rb', line 304

def info_cell_line_after(item)
  (i = info_cell_lines.index(item)) ? info_cell_lines[i + 1] : nil
end

#info_cell_linesObject


298
299
300
301
302
# File 'lib/lcbo/pages/product_page.rb', line 298

def info_cell_lines
  @info_cell_lines ||= begin
    raw_info_cell_lines.map { |l| l.strip }.reject { |l| l == '' }
  end
end

#info_cell_textObject


286
287
288
# File 'lib/lcbo/pages/product_page.rb', line 286

def info_cell_text
  @info_cell_text ||= info_cell_lines.join("\n")
end

#normalize_image_url(url) ⇒ Object


316
317
318
319
320
# File 'lib/lcbo/pages/product_page.rb', line 316

def normalize_image_url(url)
  return unless url
  return if url.include?('default')
  url.include?('http://') ? url : File.join('http://lcbo.com', url)
end

#product_details_form(name) ⇒ Object


274
275
276
277
# File 'lib/lcbo/pages/product_page.rb', line 274

def product_details_form(name)
  doc.css("form[name=\"productdetails\"] input[name=\"#{name}\"]")[0].
    attributes['value'].to_s
end

#raw_info_cell_linesObject


294
295
296
# File 'lib/lcbo/pages/product_page.rb', line 294

def raw_info_cell_lines
  @raw_info_cell_lines ||= info_cell_element.content.split(/\n/)
end

#stock_categoryObject


261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/lcbo/pages/product_page.rb', line 261

def stock_category
  cat = get_info_lines_at_offset(12).reject do |line|
    l = line.strip
    l == '' ||
    l.include?('Price:') ||
    l.include?('Bonus Reward Miles Offer') ||
    l.include?('Value Added Promotion') ||
    l.include?('Limited Time Offer') ||
    l.include?('NOTE:')
  end.first
  cat ? cat.strip : nil
end

#verify_product_details_formObject


341
342
343
344
345
# File 'lib/lcbo/pages/product_page.rb', line 341

def verify_product_details_form
  return unless doc.css('form[name="productdetails"]').empty?
  raise CrawlKit::MalformedError,
    "productdetails form not found in doc for product #{id}"
end

#verify_product_nameObject


335
336
337
338
339
# File 'lib/lcbo/pages/product_page.rb', line 335

def verify_product_name
  return unless product_details_form('itemName').strip == ''
  raise CrawlKit::NotFoundError,
    "can not locate name for product #{id}"
end

#verify_response_not_blankObject


329
330
331
332
333
# File 'lib/lcbo/pages/product_page.rb', line 329

def verify_response_not_blank
  return unless html.strip == ''
  raise CrawlKit::NotFoundError,
    "product #{id} does not appear to exist"
end

#verify_third_info_cellObject


322
323
324
325
326
327
# File 'lib/lcbo/pages/product_page.rb', line 322

def verify_third_info_cell
  return unless has_package? && info_cell_lines[2][0,1] != '|'
  raise CrawlKit::MalformedError,
    "Expected third line in info cell to begin with bar. LCBO No: " \
    "#{id}, Dump: #{info_cell_lines[2].inspect}"
end

#volume_helperObject


253
254
255
# File 'lib/lcbo/pages/product_page.rb', line 253

def volume_helper
  @volume_helper ||= CrawlKit::VolumeHelper.new(package)
end