Skip to content

Commit

Permalink
Implemented collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Smith committed Oct 29, 2013
1 parent 232e08a commit 4c68b6d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/ar_book_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
require 'capybara/poltergeist'
require 'nokogiri'

ARBookFinder::BASE_URL = 'http://www.arbookfind.com'

require 'ar_book_finder/constants'
require 'ar_book_finder/user_type_processor'
require 'ar_book_finder/pagination_processor'
require 'ar_book_finder/quick_search_processor'
require 'ar_book_finder/collection_processor'
require 'ar_book_finder/book_detail_processor'
require 'ar_book_finder/search_results_parser'
require 'ar_book_finder/book_detail_parser'
Expand Down Expand Up @@ -34,7 +34,7 @@ def self.advanced_search(user_type, search_type, params)
raise 'Not yet implemented'
end

def self.collection(topic, sub_topic)
raise 'Not yet implemented'
def self.collection(collection)
scraper(options[:user_type]).collection(collection)
end
end
30 changes: 30 additions & 0 deletions lib/ar_book_finder/collection_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module ARBookFinder
class CollectionProcessor
include Capybara::DSL

COLLECTIONS_URL = "#{ARBookFinder::BASE_URL}/collections.aspx"

def initialize(collections)
@collections = collections
end

def process
unless current_url.downcase == COLLECTIONS_URL
visit(COLLECTIONS_URL)
end
navigate_collection(@collections)
end

private
def navigate_collection(collection)
collection.each do |key, value|
click_on(key)
if value.kind_of?(Hash)
navigate_collection(value)
else
click_on(value)
end
end
end
end
end
8 changes: 6 additions & 2 deletions lib/ar_book_finder/scraper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
module ARBookFinder
class Scraper
def initialize(user_type)
@user_type = user_type
UserTypeProcessor.new(user_type).process
end

def search(query, page = 1, sort_by = 'Relevance')
UserTypeProcessor.new(@user_type).process
QuickSearchProcessor.new(query, page, sort_by).process
SearchResultsParser.new(Capybara.page.html).parse
end

def collection(collection)
CollectionProcessor.new(collection).process
SearchResultsParser.new(Capybara.page.html, true).parse
end
end
end
16 changes: 10 additions & 6 deletions lib/ar_book_finder/search_results_parser.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
module ARBookFinder
class SearchResultsParser
PAGE_COUNT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucSeachResults_lblResultsSummaryTop"]'
RESULTS_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucSeachResults_lblQuizzes"]/table'
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 :page_count, :books

def initialize(html)
def initialize(html, collection = false)
@doc = Nokogiri::HTML.parse(html)
@xpath_const = collection ? :COLLECTION : :SEARCH
@books = []
end

Expand All @@ -21,16 +25,16 @@ def parse

private
def parse_page_count
@doc.xpath(PAGE_COUNT_XPATH).text.gsub(/Page \d+ of /, '')
@doc.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page \d+ of /, '')
end

def parse_results
books = []
@doc.xpath(RESULTS_XPATH).each_with_index do |result, i|
@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('http://www.arbookfind.com/' + book_detail.xpath(BOOK_URL_XPATH).attribute('href').content)
books << Book.new("#{ARBookFinder::BASE_URL}/#{book_detail.xpath(BOOK_URL_XPATH).attribute('href').content}")
end
books
end
Expand Down
20 changes: 20 additions & 0 deletions spec/ar_book_finder/collection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe 'Collection' do
it 'should return results for a single child hash' do
results = ARBookFinder.collection('Awards' => 'ALA Notable/Best Books')
results.books.size.should > 0
end

it 'should return results for a multi child hash' do
results = ARBookFinder.collection('State Lists' => { 'Indiana' => 'IN Young Hoosier Middle Grades Book Award Nominees 2013-2014' })
results.books.size.should > 0
end

it 'should fetch book data' do
results = ARBookFinder.collection('Awards' => 'ALA Notable/Best Books')
book = results.books[0]
book.fetch
book.title.should_not be ''
end
end

0 comments on commit 4c68b6d

Please sign in to comment.