Class: EDSApi::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ebsco-discovery-service-api.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.



927
928
929
# File 'lib/ebsco-discovery-service-api.rb', line 927

def initialize
	@log = ""
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



924
925
926
# File 'lib/ebsco-discovery-service-api.rb', line 924

def auth_token
  @auth_token
end

#guestObject

Returns the value of attribute guest.



924
925
926
# File 'lib/ebsco-discovery-service-api.rb', line 924

def guest
  @guest
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



925
926
927
# File 'lib/ebsco-discovery-service-api.rb', line 925

def password=(value)
  @password = value
end

#session_tokenObject

Returns the value of attribute session_token.



924
925
926
# File 'lib/ebsco-discovery-service-api.rb', line 924

def session_token
  @session_token
end

#userid=(value) ⇒ Object (writeonly)

Sets the attribute userid

Parameters:

  • value

    the value to set the attribute userid to.



925
926
927
# File 'lib/ebsco-discovery-service-api.rb', line 925

def userid=(value)
  @userid = value
end

Instance Method Details

#create_sessionObject

Create the session



986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/ebsco-discovery-service-api.rb', line 986

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



1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/ebsco-discovery-service-api.rb', line 1005

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



970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'lib/ebsco-discovery-service-api.rb', line 970

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



939
940
941
942
943
# File 'lib/ebsco-discovery-service-api.rb', line 939

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

#request_info(format = :xml) ⇒ Object

Info method



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/ebsco-discovery-service-api.rb', line 1066

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



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'lib/ebsco-discovery-service-api.rb', line 1038

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



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/ebsco-discovery-service-api.rb', line 1019

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



1083
1084
1085
# File 'lib/ebsco-discovery-service-api.rb', line 1083

def show_log
	return @log
end

#uid_authenticate(format = :xml) ⇒ Object



947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'lib/ebsco-discovery-service-api.rb', line 947

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.



932
933
934
935
936
937
938
# File 'lib/ebsco-discovery-service-api.rb', line 932

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