class ARBookFinder::SearchResultsParser
Constants
- BOOK_DETAIL_XPATH
- BOOK_URL_XPATH
- BOOK_XPATH
- COLLECTION_PAGE_COUNT_XPATH
- COLLECTION_RESULTS_COUNT
- COLLECTION_RESULTS_XPATH
- COLLECTION_ROOT_XPATH
- SEARCH_PAGE_COUNT_XPATH
- SEARCH_RESULTS_COUNT
- SEARCH_RESULTS_XPATH
- SEARCH_ROOT_XPATH
Attributes
books[R]
current_page[R]
page_count[R]
total_books[R]
Public Class Methods
new(html, collection = false)
click to toggle source
# File lib/ar_book_finder/search_results_parser.rb, line 19 def initialize(html, collection = false) @doc = Nokogiri::HTML.parse(html) @xpath_const = collection ? :COLLECTION : :SEARCH @root = @doc.xpath(self.class.const_get(:"#{@xpath_const}_ROOT_XPATH")) @books = [] end
Public Instance Methods
parse()
click to toggle source
# File lib/ar_book_finder/search_results_parser.rb, line 26 def parse @current_page = parse_current_page.to_i @page_count = parse_page_count.to_i @total_books = parse_total_books.to_i @books = parse_results self end
Private Instance Methods
parse_current_page()
click to toggle source
# File lib/ar_book_finder/search_results_parser.rb, line 35 def parse_current_page @root.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page /, '').gsub(/ of \d+/, '') end
parse_page_count()
click to toggle source
# File lib/ar_book_finder/search_results_parser.rb, line 39 def parse_page_count @root.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page \d+ of /, '') end
parse_results()
click to toggle source
# File lib/ar_book_finder/search_results_parser.rb, line 47 def parse_results books = [] @root.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
parse_total_books()
click to toggle source
# File lib/ar_book_finder/search_results_parser.rb, line 43 def parse_total_books @root.xpath(self.class.const_get(:"#{@xpath_const}_RESULTS_COUNT")).text.gsub(/Titles \d+ - \d+ of /, '') end