Class: EDSApi::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/edsapi_wrapper.rb

Overview

Connection object. Does what it says. ConnectionHandler is what is usually desired and wraps auto-reonnect features, etc.

Direct Known Subclasses

ConnectionHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



931
932
933
# File 'lib/edsapi_wrapper.rb', line 931

def initialize
	@log = ""
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



928
929
930
# File 'lib/edsapi_wrapper.rb', line 928

def auth_token
  @auth_token
end

#guestObject

Returns the value of attribute guest.



928
929
930
# File 'lib/edsapi_wrapper.rb', line 928

def guest
  @guest
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



929
930
931
# File 'lib/edsapi_wrapper.rb', line 929

def password=(value)
  @password = value
end

#session_tokenObject

Returns the value of attribute session_token.



928
929
930
# File 'lib/edsapi_wrapper.rb', line 928

def session_token
  @session_token
end

#userid=(value) ⇒ Object (writeonly)

Sets the attribute userid

Parameters:

  • value

    the value to set the attribute userid to.



929
930
931
# File 'lib/edsapi_wrapper.rb', line 929

def userid=(value)
  @userid = value
end

Instance Method Details

#create_sessionObject

Create the session



990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/edsapi_wrapper.rb', line 990

def create_session
	uri = URI "#{API_URL}edsapi/rest/createsession?profile=#{@profile}&guest=#{@guest}"
	req = Net::HTTP::Get.new(uri.request_uri)
	req['x-authenticationToken'] = @auth_token
	req['Accept'] = "application/json"
#			Net::HTTP.start(uri.hostname, uri.port) { |http|
#  			doc = JSON.parse(http.request(req).body)
#				return doc['SessionToken']
#			}
	Net::HTTP.start(uri.hostname, uri.port, :read_timeout => 10) { |http|

			begin
	  return http.request(req).body
	rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
	  raise "No response from server"
	end
	}
end

#end_session(session_token) ⇒ Object

End the session



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/edsapi_wrapper.rb', line 1009

def end_session(session_token)
	uri = URI "#{API_URL}edsapi/rest/endsession?sessiontoken=#{CGI::escape(session_token)}"
	req = Net::HTTP::Get.new(uri.request_uri)
	req['x-authenticationToken'] = @auth_token
	Net::HTTP.start(uri.hostname, uri.port, :read_timeout => 10) { |http|
			begin
	  http.request(req)
	rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
	  raise "No response from server"
	end
	}
	return true
end

#ip_authenticate(format = :xml) ⇒ Object



974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/edsapi_wrapper.rb', line 974

def ip_authenticate(format = :xml)
	uri = URI "#{API_URL_S}authservice/rest/ipauth"
	req = Net::HTTP::Post.new(uri.request_uri)
	req["Accept"] = "application/json" #if format == :json
	https = Net::HTTP.new(uri.hostname, uri.port)
	https.read_timeout=10
	https.use_ssl = true
	https.verify_mode = OpenSSL::SSL::VERIFY_NONE
	begin
	  doc = JSON.parse(https.request(req).body)
	rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
	  raise "No response from server"
	end
	@auth_token = doc['AuthToken']
end

#ip_init(profile, guest = 'y') ⇒ Object



943
944
945
946
947
# File 'lib/edsapi_wrapper.rb', line 943

def ip_init(profile, guest = 'y')
	@profile = profile
	@guest = guest
	return self
end

#request_info(format = :xml) ⇒ Object

Info method



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/edsapi_wrapper.rb', line 1070

def request_info(format = :xml)
	uri = URI "#{API_URL}edsapi/rest/Info"
	@log << uri.to_s << " -- "

	req = Net::HTTP::Get.new(uri.request_uri)
	req['x-authenticationToken'] = @auth_token
	req['x-sessionToken'] = @session_token
	req['Accept'] = 'application/json' #if format == :json
	Net::HTTP.start(uri.hostname, uri.port, :read_timeout => 4) { |http|
			begin
	  return http.request(req).body
	rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
	  raise "No response from server"
	end
	}
end

#request_retrieve(dbid, an, highlightterms = "", ebookpreferredformat = "", format = :xml) ⇒ Object

Retrieve specific information



1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
# File 'lib/edsapi_wrapper.rb', line 1042

def request_retrieve(dbid, an, highlightterms = "", ebookpreferredformat = "", format = :xml)
	uri = URI "#{API_URL}edsapi/rest/retrieve?dbid=#{dbid}&an=#{an}"
	@log << uri.to_s << " -- "

	if highlightterms != ""
		updateURI = uri.to_s
		updateURI = updateURI + "&highlightterms=#{highlightterms}"
		uri = URI updateURI
	end
	if ebookpreferredformat != ""
		updateURI = uri.to_s
		updateURI = updateURI + "&ebookpreferredformat=#{ebookpreferredformat}"
		uri = URI updateURI
	end
	req = Net::HTTP::Get.new(uri.request_uri)
	req['x-authenticationToken'] = @auth_token
	req['x-sessionToken'] = @session_token
	req['Accept'] = 'application/json' #if format == :json

	Net::HTTP.start(uri.hostname, uri.port, :read_timeout => 4) { |http|
			begin
	  return http.request(req).body
	rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
	  raise "No response from server"
	end
	}
end

#request_search(options, format = :xml) ⇒ Object

Run a search query, XML results are returned



1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
# File 'lib/edsapi_wrapper.rb', line 1023

def request_search(options, format = :xml)
			uri = URI "#{API_URL}edsapi/rest/Search?#{options}"
			@log << uri.to_s << " -- "
			#return uri.request_uri
			req = Net::HTTP::Get.new(uri.request_uri)

			req['x-authenticationToken'] = @auth_token
			req['x-sessionToken'] = @session_token
			req['Accept'] = 'application/json' #if format == :json

			Net::HTTP.start(uri.hostname, uri.port, :read_timeout => 10) { |http|
  			begin
			  return http.request(req).body
			rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
			  raise "No response from server"
			end
			}
end

#show_logObject



1087
1088
1089
# File 'lib/edsapi_wrapper.rb', line 1087

def show_log
	return @log
end

#uid_authenticate(format = :xml) ⇒ Object



951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'lib/edsapi_wrapper.rb', line 951

def uid_authenticate(format = :xml)
	# DO NOT SEND CALL IF YOU HAVE A VALID AUTH TOKEN
	xml = "<UIDAuthRequestMessage xmlns='http://www.ebscohost.com/services/public/AuthService/Response/2012/06/01'><UserId>#{@userid}</UserId><Password>#{@password}</Password></UIDAuthRequestMessage>"
	uri = URI "#{API_URL_S}authservice/rest/uidauth"
	req = Net::HTTP::Post.new(uri.request_uri)
	req["Content-Type"] = "application/xml"
	req["Accept"] = "application/json" #if format == :json
	req.body = xml
	https = Net::HTTP.new(uri.hostname, uri.port)
	https.read_timeout=10
	https.use_ssl = true
	https.verify_mode = OpenSSL::SSL::VERIFY_NONE
	begin
	  doc = JSON.parse(https.request(req).body)
	rescue Timeout::Error, Errno::EINVAL, Net::ReadTimeout, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
	  about "No response from server"
	end
	if doc.has_key?('ErrorNumber')
	   raise "Bad response from server - error code #{result['ErrorNumber']}"
	else
	   @auth_token = doc['AuthToken']
	end
end

#uid_init(userid, password, profile, guest = 'y') ⇒ Object

Init the object with userid and pass.



936
937
938
939
940
941
942
# File 'lib/edsapi_wrapper.rb', line 936

def uid_init(userid, password, profile, guest = 'y')
	@userid = userid
	@password = password
	@profile = profile
	@guest = guest
	return self
end