-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks_apis.rb
executable file
·136 lines (127 loc) · 3.76 KB
/
stacks_apis.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
=begin
Title: Process Stacks Items
Author: Alex Cooper
Date: 09/01/2017
=end
require 'rubygems'
require 'rest_client'
require 'cgi'
require 'rexml/document'
include REXML
def getAnalytics(url,path,apikey,limit)
outcome = 1
response = 'N/A'
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)
mmsid = []
outcome = 1
mmsids = xml.elements.each("QueryResult/ResultXml/rowset/Row/Column2") { |element| element.text }
library = xml.elements.each("QueryResult/ResultXml/rowset/Row/Column7") { |element| element.text }
barcodes = xml.elements.each("QueryResult/ResultXml/rowset/Row/Column9") { |element| element.text }
return barcodes,outcome
end
# input variable from command line argument
input_arg = ARGV[0]
# open file with api information
begin
configuration = File.open("files/stacks.cfg", "r")
rescue
STDERR.puts 'Could not read configuration file'
end
# loops over api parameters
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] == "path_xmxl_bus"
path_xmxl_bus = line[1]
elsif line[0] == "path_xmxl_chem"
path_xmxl_chem = line[1]
elsif line[0] == "path_xmxl_univ"
path_xmxl_univ = line[1]
elsif line[0] == "path_xmxl_hlth"
path_xmxl_hlth = line[1]
elsif line[0] == "path_xmxl_lsc"
path_xmxl_lsc = line[1]
elsif line[0] == "path_xmxl_musme"
path_xmxl_musme = line[1]
elsif line[0] == "path_wd"
path_wd = line[1]
elsif line[0] == "analytics_apikey"
analytics_apikey = line[1]
elsif line[0] == "alma_bib_apikey"
alma_bib_apikey = line[1]
elsif line[0] == "alma_bib_url"
alma_bib_url = line[1]
end
rescue
STDERR.puts 'Could not parse configuration file'
end
end
# pass arguments to analyitcs api method
if input_arg == "univ"
path = path_xmxl_univ
elsif input_arg == "chem"
path = path_xmxl_chem
elsif input_arg == "bus"
path = path_xmxl_bus
elsif input_arg == "hlth"
path = path_xmxl_hlth
elsif input_arg == "lsc"
path = path_xmxl_lsc
elsif input_arg == "musme"
path = path_xmxl_musme
elsif input_arg == "wd"
path = path_wd
# puts path
end
xml,outcome = getAnalytics(analytics_url,path,analytics_apikey,limit)
#puts xml
# parses XML
if outcome == 0
begin
doc = Document.new xml
root = doc.root
rescue
STDERR.puts 'Could not read XML'
end
begin
finished = root.elements["QueryResult/IsFinished"].text
token = root.elements["QueryResult/ResumptionToken"].text
rescue
STDERR.puts 'Could not parse XML results'
end
if finished
parsedXml,outcome = parseAnalyticsXml(root)
string = ""
for line in parsedXml
# build set xml
####<set><members><member><id>010000690077</id></member></members></set>
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
else
workToDo = true
end
else
STDERR.puts 'getAnalytics failed ' + xml
end