11 @class_set = ClassSet.new()
12 @fast_set = FastSet.new()
15 def construct_url(params)
17 cmd = 'http://classify.oclc.org/classify2/Classify'
19 params += [ ['summary', 'false' ] ]
21 params.each do |tuple|
30 cmd += ERB::Util.url_encode(value)
37 return /[[:upper:]]/.match(c)
40 def lookup(author_grouping, pathname)
42 ['author', massage_author(author_grouping)],
43 ['title', massage_title(pathname)]
46 cmd = construct_url(params)
47 res = submit_request(cmd)
49 doc = Nokogiri::XML(res.body)
51 if "4" == response_code(doc)
52 # Multiple matches; pick the first one and re-query
53 owi = doc.css("works work")[0]["owi"]
58 cmd = construct_url(params)
59 res = submit_request(cmd)
63 doc = Nokogiri::XML(res.body)
66 if "2" != response_code(doc)
67 # Lookup failed. Let's try shortening the title, if it's in multiple parts
74 title = doc.css("classify editions edition")[0]["title"]
76 info = BookClass.new(author_grouping, title)
78 author = doc.css("classify editions edition")[0]["author"]
81 nodes = doc.css("classify recommendations ddc mostPopular")
82 if nil != nodes && nodes.length > 0
87 nodes = doc.css("classify recommendations lcc mostPopular")
88 if nil != nodes && nodes.length > 0
93 headings = doc.css("classify recommendations fast headings heading")
94 headings.each do |heading|
98 descr = heading.content
99 #puts 'DESCR: ' + descr
101 @fast_set.add(id, descr)
104 info.filename = title
106 @class_set.ensure_contains!(info)
111 def massage_author(input)
117 input.each_char do |c|
118 if isUpper?(c) and (reading_order.length > 0)
127 def massage_title(pathname)
128 basename = File.basename(pathname, '.*')
130 basename.gsub!('_', ' ')
131 basename.gsub!('--', ': ')
132 basename.gsub!('-s ', "'s ")
133 basename.gsub!('s- ', "s' ")
138 def response_code(doc)
139 return doc.css("classify response")[0]["code"]
143 @class_set.save_state()
144 @fast_set.save_state()
147 def submit_request(cmd)
151 req = Net::HTTP::Get.new(url.to_s)
152 res = Net::HTTP.start(url.host, url.port) {|http|