class ARBookFinder::BookDetailParser

Constants

BOOK_XPATH_NODES
PUBLISHER_XPATH_NODES
ROOT_PUBLISHER_XPATH
ROOT_XPATH

Public Class Methods

new(html) click to toggle source
# File lib/ar_book_finder/book_detail_parser.rb, line 32
def initialize(html)
  @doc = Nokogiri::HTML.parse(html)
  @root = @doc.xpath(ROOT_XPATH)
end

Public Instance Methods

parse() click to toggle source
# File lib/ar_book_finder/book_detail_parser.rb, line 37
def parse
  hash = parse_book_nodes
  hash[:publishers] = parse_publisher_nodes
  hash
end

Private Instance Methods

parse_book_nodes() click to toggle source
# File lib/ar_book_finder/book_detail_parser.rb, line 44
def parse_book_nodes
  hash = {}
  BOOK_XPATH_NODES.keys.each { |k| hash[k] = @root.xpath(BOOK_XPATH_NODES[k]).text.strip }
  hash
end
parse_publisher_nodes() click to toggle source
# File lib/ar_book_finder/book_detail_parser.rb, line 50
def parse_publisher_nodes
  root = @root.xpath(ROOT_PUBLISHER_XPATH)
  publishers = []
  root.each_with_index do |node, i|
    next if i == 0
    hash = {}
    PUBLISHER_XPATH_NODES.keys.each do |key|
      value = node.xpath(PUBLISHER_XPATH_NODES[key]).text
      value = '' if value == 'Not Available'
      hash[key] = value.strip
    end
    publishers << hash
  end
  publishers
end