eds-alpha 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/eds-alpha.rb +197 -0
  2. metadata +47 -0
data/lib/eds-alpha.rb ADDED
@@ -0,0 +1,197 @@
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
+ attr_accessor :auth_token
16
+ attr_writer :userid, :password
17
+
18
+ # Init the object with userid and pass.
19
+ def uid_init(userid, password, profile)
20
+ @userid = userid
21
+ @password = password
22
+ @profile = profile
23
+ return self
24
+ end
25
+ def ip_init(profile)
26
+ @profile = profile
27
+ return self
28
+ end
29
+ # Auth with the server. Currently only uid auth is supported.
30
+ def uid_authenticate(format = :xml)
31
+ xml = "<UIDAuthRequestMessage xmlns='http://www.ebscohost.com/services/public/AuthService/Response/2012/06/01'><UserId>#{@userid}</UserId><Password>#{@password}</Password></UIDAuthRequestMessage>"
32
+ uri = URI "#{API_URL_S}authservice/rest/uidauth"
33
+ req = Net::HTTP::Post.new(uri.request_uri)
34
+ req["Content-Type"] = "application/xml"
35
+ req["Accept"] = "application/json" #if format == :json
36
+ req.body = xml
37
+ https = Net::HTTP.new(uri.hostname, uri.port)
38
+ https.use_ssl = true
39
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
40
+ doc = JSON.parse(https.request(req).body)
41
+ if doc.has_key?('ErrorNumber')
42
+ abort "Bad response from server - error code #{result['ErrorNumber']}"
43
+ else
44
+ @auth_token = doc['AuthToken']
45
+ end
46
+ end
47
+ def ip_authenticate(format = :xml)
48
+ uri = URI "#{API_URL_S}authservice/rest/ipauth"
49
+ req = Net::Http:Post.new(uri.request_uri)
50
+ req["Accept"] = "application/json" #if format == :json
51
+ https = Net::HTTP.new(uri.hostname, uri.port)
52
+ https.use_ssl = true
53
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
54
+ doc = JSON.parse(https.request(req).body)
55
+ @auth_token = doc['AuthToken']
56
+ end
57
+ # Create the session
58
+ def create_session
59
+ uri = URI "#{API_URL}edsapi/rest/createsession?profile=#{@profile}"
60
+ req = Net::HTTP::Get.new(uri.request_uri)
61
+ req['x-authenticationToken'] = @auth_token
62
+ req['Accept'] = "application/json"
63
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
64
+ doc = JSON.parse(http.request(req).body)
65
+ return doc['SessionToken']
66
+ }
67
+ end
68
+ # End the session
69
+ def end_session(session_token)
70
+ uri = URI "#{API_URL}edsapi/rest/endsession?sessiontoken=#{CGI::escape(session_token)}"
71
+ req = Net::HTTP::Get.new(uri.request_uri)
72
+ req['x-authenticationToken'] = @auth_token
73
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
74
+ http.request(req)
75
+ }
76
+ return true
77
+ end
78
+ # Run a search query, XML results are returned
79
+ def search(options, session_token, format = :xml)
80
+ uri = URI "#{API_URL}edsapi/rest/Search?#{options}"
81
+ req = Net::HTTP::Get.new(uri.request_uri)
82
+ req['x-authenticationToken'] = @auth_token
83
+ req['x-sessionToken'] = session_token
84
+ req['Accept'] = 'application/json' #if format == :json
85
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
86
+ return http.request(req).body
87
+ }
88
+ end
89
+ # Retrieve specific information
90
+ def retrieve(dbid, an, session_token, format = :xml)
91
+ uri = URI "#{API_URL}edsapi/rest/retrieve?dbid=#{dbid}&an=#{an}"
92
+ req = Net::HTTP::Get.new(uri.request_uri)
93
+ req['x-authenticationToken'] = @auth_token
94
+ req['x-sessionToken'] = session_token
95
+ req['Accept'] = 'application/json' #if format == :json
96
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
97
+ return http.request(req).body
98
+ }
99
+ end
100
+ # Info method
101
+ def info(session_token, format = :xml)
102
+ uri = URI "#{API_URL}edsapi/rest/Info"
103
+ req = Net::HTTP::Get.new(uri.request_uri)
104
+ req['x-authenticationToken'] = @auth_token
105
+ req['x-sessionToken'] = session_token
106
+ req['Accept'] = 'application/json' #if format == :json
107
+ Net::HTTP.start(uri.hostname, uri.port) { |http|
108
+ return http.request(req).body
109
+ }
110
+ end
111
+ end
112
+ # Handles connections - retries failed connections, passes commands along
113
+ class ConnectionHandler < Connection
114
+ attr_accessor :max_retries
115
+ def initialize(max_retries = 2)
116
+ @max_retries = max_retries
117
+ end
118
+ def search(options, session_token, format = :xml)
119
+ attempts = 0
120
+ loop do
121
+ result = JSON.parse(super(options, session_token, format))
122
+ if result.has_key?('ErrorNumber')
123
+ case result['ErrorNumber']
124
+ when "108"
125
+ session_token = self.create_session
126
+ when "104"
127
+ self.uid_authenticate(:json)
128
+ end
129
+ if ++attempts == @max_retries
130
+ abort "Bad response from server - error code #{result['ErrorNumber']}"
131
+ end
132
+ else
133
+ return result
134
+ end
135
+ end
136
+ end
137
+ def info (session_token, format= :xml)
138
+ attempts = 0
139
+ loop do
140
+ result = JSON.parse(super(session_token, format)) # JSON Parse
141
+ if result.has_key?('ErrorNumber')
142
+ case result['ErrorNumber']
143
+ when "108"
144
+ session_token = self.create_session
145
+ when "104"
146
+ self.uid_authenticate(:json)
147
+ end
148
+ if ++attempts == @max_retries
149
+ abort "Bad response from server - error code #{result['ErrorNumber']}"
150
+ end
151
+ else
152
+ return result
153
+ end
154
+ end
155
+ end
156
+ def retrieve(dbid, an, session_token, format = :xml)
157
+ attempts = 0
158
+ loop do
159
+ result = JSON.parse(super(dbid, an, session_token, format))
160
+ if result.has_key?('ErrorNumber')
161
+ case result['ErrorNumber']
162
+ when "108"
163
+ session_token = self.create_session
164
+ when "104"
165
+ self.uid_authenticate(:json)
166
+ end
167
+ if ++attempts == @max_retries
168
+ abort "Bad response from server - error code #{result['ErrorNumber']}"
169
+ end
170
+ else
171
+ return result
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ # Benchmark response times
179
+ def benchmark(q = false)
180
+ start = Time.now
181
+ connection = EDSApi::ConnectionHandler.new(2)
182
+ connection.uid_init('USERID', 'PASSWORD', 'PROFILEID')
183
+ connection.uid_authenticate(:json)
184
+ puts((start - Time.now).abs) unless q
185
+ connection.create_session
186
+ puts((start - Time.now).abs) unless q
187
+ connection.search('query-1=AND,galapagos+hawk', :json)
188
+ puts((start - Time.now).abs) unless q
189
+ connection.end_session
190
+ puts((start - Time.now).abs) unless q
191
+ end
192
+
193
+ # Run benchmark with warm up run; only if file was called directly and not required
194
+ if __FILE__ == $0
195
+ benchmark(true)
196
+ benchmark
197
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eds-alpha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Richard McCormack
9
+ - Eric Frierson
10
+ - Wenlong Jiang
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2013-01-14 00:00:00.000000000 Z
15
+ dependencies: []
16
+ description: Set of Ruby functions to interface with the EBSCO Discovery Service API.
17
+ email: eds@ebscohost.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/eds-alpha.rb
23
+ homepage: http://rubygems.org/gems/eds-alpha
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: EBSCO Discovery Service API Ruby Library
47
+ test_files: []