ebsco-discovery-service-api 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ebsco-discovery-service-api.rb +250 -0
  2. metadata +46 -0
@@ -0,0 +1,250 @@
1
+ require 'net/http'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ # This is the first, barely tested pass at creating a set of Ruby functions to authenticate to, search, and retrieve from the EDS API.
6
+ # Once this gem is installed, you can find demo code that makes use of the gem here:
7
+ # http://www.lh2cc.net/dse/efrierson/ruby/eds-alpha-demo.zip
8
+
9
+ module EDSApi
10
+ API_URL = "http://eds-api.ebscohost.com/"
11
+ API_URL_S = "https://eds-api.ebscohost.com/"
12
+
13
+ # Connection object. Does what it says. ConnectionHandler is what is usually desired and wraps auto-reonnect features, etc.
14
+ class Connection
15
+
16
+ attr_accessor :auth_token, :session_token
17
+
18
+ attr_writer :userid, :password
19
+
20
+ # Init the object with userid and pass.
21
+ def uid_init(userid, password, profile)
22
+ @userid = userid
23
+ @password = password
24
+ @profile = profile
25
+ return self
26
+ end
27
+ def ip_init(profile)
28
+ @profile = profile
29
+ return self
30
+ end
31
+ # Auth with the server. Currently only uid auth is supported.
32
+
33
+ ###
34
+ def uid_authenticate(format = :xml)
35
+ # DO NOT SEND CALL IF YOU HAVE A VALID AUTH TOKEN
36
+ xml = "<UIDAuthRequestMessage xmlns='http://www.ebscohost.com/services/public/AuthService/Response/2012/06/01'><UserId>#{@userid}</UserId><Password>#{@password}</Password></UIDAuthRequestMessage>"
37
+ uri = URI "#{API_URL_S}authservice/rest/uidauth"
38
+ req = Net::HTTP::Post.new(uri.request_uri)
39
+ req["Content-Type"] = "application/xml"
40
+ req["Accept"] = "application/json" #if format == :json
41
+ req.body = xml
42
+ https = Net::HTTP.new(uri.hostname, uri.port)
43
+ https.use_ssl = true
44
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
45
+ doc = JSON.parse(https.request(req).body)
46
+ if doc.has_key?('ErrorNumber')
47
+ abort "Bad response from server - error code #{result['ErrorNumber']}"
48
+ else
49
+ @auth_token = doc['AuthToken']
50
+ end
51
+ end
52
+ def ip_authenticate(format = :xml)
53
+ uri = URI "#{API_URL_S}authservice/rest/ipauth"
54
+ req = Net::Http:Post.new(uri.request_uri)
55
+ req["Accept"] = "application/json" #if format == :json
56
+ https = Net::HTTP.new(uri.hostname, uri.port)
57
+ https.use_ssl = true
58
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
59
+ doc = JSON.parse(https.request(req).body)
60
+ @auth_token = doc['AuthToken']
61
+ end
62
+ # Create the session
63
+ def create_session
64
+ uri = URI "#{API_URL}edsapi/rest/createsession?profile=#{@profile}"
65
+ req = Net::HTTP::Get.new(uri.request_uri)
66
+ req['x-authenticationToken'] = @auth_token
67
+ req['Accept'] = "application/json"
68
+ # Net::HTTP.start(uri.hostname, uri.port) { |http|
69
+ # doc = JSON.parse(http.request(req).body)
70
+ # return doc['SessionToken']
71
+ # }
72
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
73
+ return http.request(req).body
74
+ }
75
+ end
76
+ # End the session
77
+ def end_session(session_token)
78
+ uri = URI "#{API_URL}edsapi/rest/endsession?sessiontoken=#{CGI::escape(session_token)}"
79
+ req = Net::HTTP::Get.new(uri.request_uri)
80
+ req['x-authenticationToken'] = @auth_token
81
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
82
+ http.request(req)
83
+ }
84
+ return true
85
+ end
86
+ # Run a search query, XML results are returned
87
+ def search(options, format = :xml)
88
+ uri = URI "#{API_URL}edsapi/rest/Search?#{options}"
89
+ #return uri.request_uri
90
+ req = Net::HTTP::Get.new(uri.request_uri)
91
+ req['x-authenticationToken'] = @auth_token
92
+ req['x-sessionToken'] = @session_token
93
+ req['Accept'] = 'application/json' #if format == :json
94
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
95
+ return http.request(req).body
96
+ }
97
+ end
98
+ # Retrieve specific information
99
+ def retrieve(dbid, an, format = :xml)
100
+ uri = URI "#{API_URL}edsapi/rest/retrieve?dbid=#{dbid}&an=#{an}"
101
+ req = Net::HTTP::Get.new(uri.request_uri)
102
+ req['x-authenticationToken'] = @auth_token
103
+ req['x-sessionToken'] = @session_token
104
+ req['Accept'] = 'application/json' #if format == :json
105
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
106
+ return http.request(req).body
107
+ }
108
+ end
109
+ # Info method
110
+ def info(format = :xml)
111
+ uri = URI "#{API_URL}edsapi/rest/Info"
112
+ req = Net::HTTP::Get.new(uri.request_uri)
113
+ req['x-authenticationToken'] = @auth_token
114
+ req['x-sessionToken'] = @session_token
115
+ req['Accept'] = 'application/json' #if format == :json
116
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
117
+ return http.request(req).body
118
+ }
119
+ end
120
+ end
121
+ # Handles connections - retries failed connections, passes commands along
122
+ class ConnectionHandler < Connection
123
+ attr_accessor :max_retries
124
+ attr_accessor :session_token
125
+ def initialize(max_retries = 2)
126
+ @max_retries = max_retries
127
+ end
128
+ def show_session_token
129
+ return @session_token
130
+ end
131
+ def show_auth_token
132
+ return @auth_token
133
+ end
134
+ def create_session(auth_token = @auth_token, format = :xml)
135
+ @auth_token = auth_token
136
+ result = JSON.parse(super())
137
+ if result.has_key?('ErrorNumber')
138
+ return result.to_s
139
+ else
140
+ @session_token = result['SessionToken']
141
+ return result['SessionToken']
142
+ end
143
+ end
144
+ def search(options, session_token, auth_token, format = :xml)
145
+ attempts = 0
146
+ @session_token = session_token
147
+ @auth_token = auth_token
148
+ loop do
149
+ result = JSON.parse(super(options, format))
150
+ if result.has_key?('ErrorNumber')
151
+ case result['ErrorNumber']
152
+ when "108"
153
+ @session_token = self.create_session
154
+ result = JSON.parse(super(options, format))
155
+ when "109"
156
+ @session_token = self.create_session
157
+ result = JSON.parse(super(options, format))
158
+ when "104"
159
+ self.uid_authenticate(:json)
160
+ result = JSON.parse(super(options, format))
161
+ when "107"
162
+ self.uid_authenticate(:json)
163
+ result = JSON.parse(super(options, format))
164
+ else
165
+ return result
166
+ end
167
+ attempts += 1
168
+ if attempts >= @max_retries
169
+ return result
170
+ end
171
+ else
172
+ return result
173
+ end
174
+ end
175
+ end
176
+ def info (session_token, auth_token, format= :xml)
177
+ attempts = 0
178
+ @auth_token = auth_token
179
+ @session_token = session_token
180
+ loop do
181
+ result = JSON.parse(super(format)) # JSON Parse
182
+ if result.has_key?('ErrorNumber')
183
+ case result['ErrorNumber']
184
+ when "108"
185
+ @session_token = self.create_session
186
+ when "109"
187
+ @session_token = self.create_session
188
+ when "104"
189
+ self.uid_authenticate(:json)
190
+ when "107"
191
+ self.uid_authenticate(:json)
192
+ end
193
+ attempts += 1
194
+ if attempts >= @max_retries
195
+ return result
196
+ end
197
+ else
198
+ return result
199
+ end
200
+ end
201
+ end
202
+ def retrieve(dbid, an, session_token, auth_token, format = :xml)
203
+ attempts = 0
204
+ @session_token = session_token
205
+ @auth_token = auth_token
206
+ loop do
207
+ result = JSON.parse(super(dbid, an, format))
208
+ if result.has_key?('ErrorNumber')
209
+ case result['ErrorNumber']
210
+ when "108"
211
+ @session_token = self.create_session
212
+ when "109"
213
+ @session_token = self.create_session
214
+ when "104"
215
+ self.uid_authenticate(:json)
216
+ when "107"
217
+ self.uid_authenticate(:json)
218
+ end
219
+ attempts += 1
220
+ if attempts >= @max_retries
221
+ return result
222
+ end
223
+ else
224
+ return result
225
+ end
226
+ end
227
+ end
228
+ end
229
+ end
230
+
231
+ # Benchmark response times
232
+ def benchmark(q = false)
233
+ start = Time.now
234
+ connection = EDSApi::ConnectionHandler.new(2)
235
+ connection.uid_init('USERID', 'PASSWORD', 'PROFILEID')
236
+ connection.uid_authenticate(:json)
237
+ puts((start - Time.now).abs) unless q
238
+ connection.create_session
239
+ puts((start - Time.now).abs) unless q
240
+ connection.search('query-1=AND,galapagos+hawk', :json)
241
+ puts((start - Time.now).abs) unless q
242
+ connection.end_session
243
+ puts((start - Time.now).abs) unless q
244
+ end
245
+
246
+ # Run benchmark with warm up run; only if file was called directly and not required
247
+ if __FILE__ == $0
248
+ benchmark(true)
249
+ benchmark
250
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ebsco-discovery-service-api
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Richard McCormack
9
+ - Eric Frierson
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-06-14 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: Set of Ruby functions to interface with the EBSCO Discovery Service API.
16
+ email: eds@ebscohost.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/ebsco-discovery-service-api.rb
22
+ homepage: http://rubygems.org/gems/ebsco-discovery-service-api
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.24
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: EBSCO Discovery Service API Ruby Library
46
+ test_files: []