-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_books_api.rb
83 lines (77 loc) · 2.04 KB
/
new_books_api.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/ruby
=begin
Title: Process Used New Books
Author: Alex Cooper
Date: 09/26/2017
=end
require 'rubygems'
require 'rest_client'
require 'cgi'
require 'rexml/document'
include REXML
def getAnalytics(url,apikey,path,limit)
outcome = 1
headers = { :params => { CGI::escape('path') => path, CGI::escape('apikey') => apikey, CGI::escape('limit') => limit }}
begin
response = RestClient::Request.execute :method => 'GET', :url => url, :headers => headers
outcome = 0
rescue RestClient::ExceptionWithResponse => e
return e.response, outcome
end
return response,outcome
end
def parseAnalyticsXML(xml)
outcome = 1
begin
doc = Document.new xml
root = doc.root
barcodes = root.elements.each("QueryResult/ResultXml/rowset/Row/Column6") { |element| element.text }
# puts barcodes
outcome = 0
rescue
STDERR.puts 'Could not parse xml'
end
return barcodes,outcome
end
#### Process configuration file
begin
configuration = File.open("/sirsi/webserver/bin/stacks_jobs/files/stacks.cfg")
rescue
STDERR.puts 'Could not open configuration file'
end
for line in configuration
line = line.chomp
line = line.split("=")
begin
if line[0] == "limit"
limit = line[1]
elsif line[0] == "analytics_url"
analytics_url = line[1]
elsif line[0] == "analytics_apikey"
analytics_apikey = line[1]
elsif line[0] == "path_new"
path = line[1]
end
rescue
STDERR.puts 'Could not parse configuration file'
end
end
#### Place API call
xml,outcome = getAnalytics(analytics_url,analytics_apikey,path,limit)
if outcome == 1
puts xml
end
#### Parse Analytics XML
barcodes,outcome = parseAnalyticsXML(xml)
string = ""
for line in barcodes
barcode = line.text
barcode = barcode.to_s
string = string << "<member><id>#{barcode}</id></member>"
end
if not string.empty?
xml = "<set><members>#{string}</members></set>"
STDOUT.puts xml
else
STDOUT.puts 'end'
end