Sha256: d613a68a4740f46dbc7baade6a7dc6d479e6dd02d2f305fc2dcb2039647feb57
Contents?: true
Size: 1.68 KB
Versions: 1
Compression:
Stored size: 1.68 KB
Contents
module ARBookFinder class SearchResultsParser SEARCH_PAGE_COUNT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucSeachResults_lblResultsSummaryTop"]' SEARCH_RESULTS_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucSeachResults_lblQuizzes"]/table' COLLECTION_PAGE_COUNT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_ucSeachResults_lblResultsSummaryTop"]' COLLECTION_RESULTS_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_ucSeachResults_lblQuizzes"]/table' BOOK_XPATH = 'tbody/tr/td[2]' BOOK_DETAIL_XPATH = 'table/tbody/tr/td[2]' BOOK_URL_XPATH = 'a' attr_reader :current_page, :page_count, :total_books, :books def initialize(html, collection = false) @doc = Nokogiri::HTML.parse(html) @xpath_const = collection ? :COLLECTION : :SEARCH @books = [] end def parse @current_page = parse_current_page.to_i @page_count = parse_page_count.to_i @books = parse_results self end private def parse_current_page @doc.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page /, '').gsub(/ of \d+/, '') end def parse_page_count @doc.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page \d+ of /, '') end def parse_results books = [] @doc.xpath(self.class.const_get(:"#{@xpath_const}_RESULTS_XPATH")).each_with_index do |result, i| next if i.odd? book = result.xpath(BOOK_XPATH) book_detail = book.xpath(BOOK_DETAIL_XPATH) books << Book.new("#{ARBookFinder::BASE_URL}/#{book_detail.xpath(BOOK_URL_XPATH).attribute('href').content}") end books end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ar_book_finder-1.1.0 | lib/ar_book_finder/search_results_parser.rb |