ar_book_finder 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -88,7 +88,7 @@ book.author
|
|
88
88
|
Return publisher data...
|
89
89
|
```ruby
|
90
90
|
...
|
91
|
-
publisher = book.publishers
|
91
|
+
publisher = book.publishers[0]
|
92
92
|
publisher.name
|
93
93
|
publisher.isbn
|
94
94
|
...
|
@@ -110,8 +110,9 @@ results = ARBookFinder.collection({ 'State Lists' => { 'Indiana' => 'IN Young Ho
|
|
110
110
|
### Pagination
|
111
111
|
It's also possible to paginate search results.
|
112
112
|
```ruby
|
113
|
-
# Retrieve results for page 2
|
113
|
+
# Retrieve results for page 2
|
114
114
|
results = ARBookFinder.search('harry potter', 2)
|
115
|
+
results.current_page # should == 2
|
115
116
|
```
|
116
117
|
|
117
118
|
## Contributing
|
@@ -1,9 +1,13 @@
|
|
1
1
|
module ARBookFinder
|
2
2
|
class SearchResultsParser
|
3
|
+
SEARCH_ROOT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_pnlSearchFormPanel"]'
|
3
4
|
SEARCH_PAGE_COUNT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucSeachResults_lblResultsSummaryTop"]'
|
5
|
+
SEARCH_RESULTS_COUNT = '//*[@id="ctl00_ContentPlaceHolder1_ucSearchResultsHeader_lblResultSummary"]'
|
4
6
|
SEARCH_RESULTS_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucSeachResults_lblQuizzes"]/table'
|
5
7
|
|
8
|
+
COLLECTION_ROOT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_pnlSearchFormPanel"]'
|
6
9
|
COLLECTION_PAGE_COUNT_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_ucSeachResults_lblResultsSummaryTop"]'
|
10
|
+
COLLECTION_RESULTS_COUNT = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_ucSearchResultsHeader_lblResultSummary"]'
|
7
11
|
COLLECTION_RESULTS_XPATH = '//*[@id="ctl00_ContentPlaceHolder1_ucCollection_ucSeachResults_lblQuizzes"]/table'
|
8
12
|
|
9
13
|
BOOK_XPATH = 'tbody/tr/td[2]'
|
@@ -15,28 +19,34 @@ module ARBookFinder
|
|
15
19
|
def initialize(html, collection = false)
|
16
20
|
@doc = Nokogiri::HTML.parse(html)
|
17
21
|
@xpath_const = collection ? :COLLECTION : :SEARCH
|
22
|
+
@root = @doc.xpath(self.class.const_get(:"#{@xpath_const}_ROOT_XPATH"))
|
18
23
|
@books = []
|
19
24
|
end
|
20
25
|
|
21
26
|
def parse
|
22
27
|
@current_page = parse_current_page.to_i
|
23
28
|
@page_count = parse_page_count.to_i
|
29
|
+
@total_books = parse_total_books.to_i
|
24
30
|
@books = parse_results
|
25
31
|
self
|
26
32
|
end
|
27
33
|
|
28
34
|
private
|
29
35
|
def parse_current_page
|
30
|
-
@
|
36
|
+
@root.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page /, '').gsub(/ of \d+/, '')
|
31
37
|
end
|
32
38
|
|
33
39
|
def parse_page_count
|
34
|
-
@
|
40
|
+
@root.xpath(self.class.const_get(:"#{@xpath_const}_PAGE_COUNT_XPATH")).text.gsub(/Page \d+ of /, '')
|
41
|
+
end
|
42
|
+
|
43
|
+
def parse_total_books
|
44
|
+
@root.xpath(self.class.const_get(:"#{@xpath_const}_RESULTS_COUNT")).text.gsub(/Titles \d+ - \d+ of /, '')
|
35
45
|
end
|
36
46
|
|
37
47
|
def parse_results
|
38
48
|
books = []
|
39
|
-
@
|
49
|
+
@root.xpath(self.class.const_get(:"#{@xpath_const}_RESULTS_XPATH")).each_with_index do |result, i|
|
40
50
|
next if i.odd?
|
41
51
|
book = result.xpath(BOOK_XPATH)
|
42
52
|
book_detail = book.xpath(BOOK_DETAIL_XPATH)
|
@@ -16,6 +16,11 @@ describe 'Collection' do
|
|
16
16
|
results.current_page.should == 2
|
17
17
|
end
|
18
18
|
|
19
|
+
it 'should return total book count' do
|
20
|
+
results = ARBookFinder.collection({ 'Awards' => 'ALA Notable/Best Books' })
|
21
|
+
results.total_books.should > 0
|
22
|
+
end
|
23
|
+
|
19
24
|
it 'should fetch book data' do
|
20
25
|
results = ARBookFinder.collection('Awards' => 'ALA Notable/Best Books')
|
21
26
|
book = results.books[0]
|
@@ -11,6 +11,11 @@ describe 'Search' do
|
|
11
11
|
results.current_page.should == 2
|
12
12
|
end
|
13
13
|
|
14
|
+
it 'should return total book count' do
|
15
|
+
results = ARBookFinder.search('harry potter')
|
16
|
+
results.total_books.should > 0
|
17
|
+
end
|
18
|
+
|
14
19
|
it 'should fetch book data' do
|
15
20
|
results = ARBookFinder.search('harry potter')
|
16
21
|
book = results.books[0]
|