-
Notifications
You must be signed in to change notification settings - Fork 3
/
overdrive.rb
67 lines (51 loc) · 1.46 KB
/
overdrive.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'uri'
class LookupStrategies::Overdrive
include LookupStrategies::Browser
attr_accessor :host, :title, :author, :location_title
def initialize(host, location_title, title, author)
self.title = title.gsub(/\([^)]+\)/, '')
self.location_title = location_title
self.author = author
self.host = host
end
def find
session.visit host
search_for_book
puts "#{title} / #{author} - #{result_links.length} results"
copies_from_links
end
private
def copies_from_links
# there's some JS going on with each click, so we can't just get all the urls
# and open them serialially
[].tap do |copies|
(0..result_links.length-1).each do |i|
result_links[i].click
copies << extract_copy
session.go_back
end
end
end
def extract_copy
ScrapedBook.new(title: copy_title,
location: location_title,
call_number: "",
url: session.current_url,
status: copy_status)
end
def copy_title
session.find("#detailsTitle h3").text
end
def copy_status
session.all('#copiesExpand .row').map do |row|
row.all('.columns').map(&:text).join(' ')
end.join(" / ")
end
def search_for_book
session.fill_in "FullTextCriteria", with: "#{title} #{author}"
session.find("input[title='Submit search']").click
end
def result_links
session.all('li.title-element-li a.tc-title')
end
end