Skip to content

Commit

Permalink
Parsing out total results.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Smith committed Oct 29, 2013
1 parent 250e8b9 commit 212a365
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/ar_book_finder/search_results_parser.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
module ARBookFinder
class SearchResultsParser
SEARCH_ROOT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_pnlSearchFormPanel"]'
SEARCH_PAGE_COUNT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucSeachResults_lblResultsSummaryTop"]'
SEARCH_RESULTS_COUNT = '//*[@id="ctl00_ContentPlaceHolder1_ucSearchResultsHeader_lblResultSummary"]'
SEARCH_RESULTS_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucSeachResults_lblQuizzes"]/table'

COLLECTION_ROOT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_pnlSearchFormPanel"]'
COLLECTION_PAGE_COUNT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_ucSeachResults_lblResultsSummaryTop"]'
COLLECTION_RESULTS_COUNT = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_ucSearchResultsHeader_lblResultSummary"]'
COLLECTION_RESULTS_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_ucSeachResults_lblQuizzes"]/table'

BOOK_XPATH = 'tbody/tr/td[2]'
Expand All @@ -15,28 +19,34 @@ class SearchResultsParser
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

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
def parse_current_page
@doc.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page /, '').gsub(/ of \d+/, '')
@root.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 /, '')
@root.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page \d+ of /, '')
end

def parse_total_books
@root.xpath(self.class.const_get(:"#{@xpath_const}_RESULTS_COUNT")).text.gsub(/Titles \d+ - \d+ of /, '')
end

def parse_results
books = []
@doc.xpath(self.class.const_get(:"#{@xpath_const}_RESULTS_XPATH")).each_with_index do |result, i|
@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)
Expand Down
5 changes: 5 additions & 0 deletions spec/ar_book_finder/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
results.current_page.should == 2
end

it 'should return total book count' do
results = ARBookFinder.collection({ 'Awards' => 'ALA Notable/Best Books' })
results.total_books.should > 0
end

it 'should fetch book data' do
results = ARBookFinder.collection('Awards' => 'ALA Notable/Best Books')
book = results.books[0]
Expand Down
5 changes: 5 additions & 0 deletions spec/ar_book_finder/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
results.current_page.should == 2
end

it 'should return total book count' do
results = ARBookFinder.search('harry potter')
results.total_books.should > 0
end

it 'should fetch book data' do
results = ARBookFinder.search('harry potter')
book = results.books[0]
Expand Down

0 comments on commit 212a365

Please sign in to comment.