Class: EDSApi::ConnectionHandler

Inherits:
Connection show all
Defined in:
lib/edsapi_wrapper.rb

Overview

Handles connections - retries failed connections, passes commands along

Instance Attribute Summary collapse

Attributes inherited from Connection

#auth_token, #guest, #password, #userid

Instance Method Summary collapse

Methods inherited from Connection

#end_session, #ip_authenticate, #ip_init, #show_log, #uid_authenticate, #uid_init

Constructor Details

#initialize(max_retries = 2) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



1100
1101
1102
1103
# File 'lib/edsapi_wrapper.rb', line 1100

def initialize(max_retries = 2)
	@max_retries = max_retries
	super()
end

Instance Attribute Details

#infoObject

Returns the value of attribute info.



1097
1098
1099
# File 'lib/edsapi_wrapper.rb', line 1097

def info
  @info
end

#max_retriesObject

Returns the value of attribute max_retries.



1095
1096
1097
# File 'lib/edsapi_wrapper.rb', line 1095

def max_retries
  @max_retries
end

#search_resultsObject

Returns the value of attribute search_results.



1098
1099
1100
# File 'lib/edsapi_wrapper.rb', line 1098

def search_results
  @search_results
end

#session_tokenObject

Returns the value of attribute session_token.



1096
1097
1098
# File 'lib/edsapi_wrapper.rb', line 1096

def session_token
  @session_token
end

Instance Method Details

#create_session(auth_token = @auth_token, format = :xml) ⇒ Object



1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
# File 'lib/edsapi_wrapper.rb', line 1113

def create_session(auth_token = @auth_token, format = :xml)
	@auth_token = auth_token
	result = JSON.parse(super())
  if result.has_key?('ErrorNumber')
		return result.to_s
  else
		@session_token = result['SessionToken']
		@info = EDSApi::EDSAPIInfo.new(self.request_info(@session_token,@auth_token))
  	return result['SessionToken']
  end
end

#new_search(searchterm, use_defaults = "y") ⇒ Object



1125
1126
1127
1128
1129
# File 'lib/edsapi_wrapper.rb', line 1125

def new_search(searchterm, use_defaults = "y")
	options = @info.default_options
	options['query'] = searchterm
	return self.search(options)
end

#page(paginate = "next") ⇒ Object



1147
1148
1149
# File 'lib/edsapi_wrapper.rb', line 1147

def page(paginate = "next")

end

#request_info(session_token, auth_token, format = :xml) ⇒ Object



1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
# File 'lib/edsapi_wrapper.rb', line 1213

def request_info (session_token, auth_token, format= :xml)
 attempts = 0
 @auth_token = auth_token
 @session_token = session_token
loop do
  result = JSON.parse(super(format)) # JSON Parse
  if result.has_key?('ErrorNumber')
	  case result['ErrorNumber']
	  	when "108"
	  		@session_token = self.create_session
	  	when "109"
	  		@session_token = self.create_session
	  	when "104"
	  		self.uid_authenticate(:json)
		when "107"
			self.uid_authenticate(:json)
	  end
	  attempts += 1
	  if attempts >= @max_retries
	  	return result
	  end
  else
  	return result
  end
  end
end

#request_retrieve(dbid, an, highlightterms, ebookpreferredformat = 'ebook-epub', session_token = @session_token, auth_token = @auth_token, format = :xml) ⇒ Object



1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
# File 'lib/edsapi_wrapper.rb', line 1240

def request_retrieve(dbid, an, highlightterms, ebookpreferredformat = 'ebook-epub', session_token = @session_token, auth_token = @auth_token, format = :xml)
	attempts = 0
	@session_token = session_token
	@auth_token = auth_token
	loop do
	  result = JSON.parse(super(dbid, an, highlightterms, ebookpreferredformat, format))
	  if result.has_key?('ErrorNumber')
		  case result['ErrorNumber']
		  	when "108"
		  		@session_token = self.create_session
		  	when "109"
		  		@session_token = self.create_session
		  	when "104"
		  		self.uid_authenticate(:json)
			when "107"
				self.uid_authenticate(:json)
		  end
		  attempts += 1
		  if attempts >= @max_retries
			return result
		  end
	  else
	  	return result
	  end
  end
end

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



1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
# File 'lib/edsapi_wrapper.rb', line 1161

def request_search(options, session_token = @session_token, auth_token = @auth_token, format = :xml)

	# temporary fix while API SI resolves
	# catches case where user navigates past result page 250 and applies facet/limiter
	if (options.index('&action=') && (options.index('&action=') > 0))
		if (options.index('&action=GoToPage(').nil? && options.index('&action=SetView(').nil?)
			if (options.index('&pagenumber=') && (options.index('&pagenumber=') > 0))
				beginSubstring = options.index('&pagenumber=') + 12
				currentpage = options[beginSubstring..-1]
				newOptions = options[0..beginSubstring-1]
				endSubstring = currentpage.index('&') - 1
				newOptionsEnd = currentpage[endSubstring+1..-1]
				options = newOptions + "1" + newOptionsEnd
			end
		end
	end

	attempts = 0
	@session_token = session_token
	@auth_token = auth_token
	loop do
		result = JSON.parse(super(options, format))
		if result.has_key?('ErrorNumber')
			case result['ErrorNumber']
			      when "108"
				      @session_token = self.create_session
				      result = JSON.parse(super(options, format))
			      when "109"
				      @session_token = self.create_session
				      result = JSON.parse(super(options, format))
			      when "104"
				      self.uid_authenticate(:json)
				      result = JSON.parse(super(options, format))
			      when "107"
				      self.uid_authenticate(:json)
				      result = JSON.parse(super(options, format))
			      else
				      return result
			end
			unless result.has_key?('ErrorNumber')
				return result
			end
			attempts += 1
			if attempts >= @max_retries
			      return result
			end
		else
		      return result
		end
	end
end

#retrieve(options) ⇒ Object



1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/edsapi_wrapper.rb', line 1151

def retrieve(options)
	options['highlightterms'] = @current_searchterms
	options['ebookpreferredformat'] = 'ebook-epub'
	apiresponse = request_retrieve(options['dbid'],options['an'],options['hhighlightterms'])
	unless apiresponse["Record"].nil?
		return EDSApi::EDSAPIRecord.new(apiresponse["Record"])
	end
	return apiresponse
end

#search(options, actions = []) ⇒ Object



1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
# File 'lib/edsapi_wrapper.rb', line 1131

def search(options, actions = [])
	if actions.kind_of?(Array) && actions.count > 0
		options['action'] = actions
	elsif actions.length > 0
		options['action'] = [actions]
	end
	apiresponse = EDSApi::EDSAPIResponse.new(request_search(URI.encode_www_form(options)))
	@current_searchterms = apiresponse.searchterms
	@search_results = apiresponse
	return apiresponse
end

#search_actions(actions) ⇒ Object



1143
1144
1145
# File 'lib/edsapi_wrapper.rb', line 1143

def search_actions(actions)
	return self.search(@search_results.current_search,actions)
end

#show_auth_tokenObject



1109
1110
1111
# File 'lib/edsapi_wrapper.rb', line 1109

def show_auth_token
  return @auth_token
end

#show_session_tokenObject



1105
1106
1107
# File 'lib/edsapi_wrapper.rb', line 1105

def show_session_token
  return @session_token
end