Class: App42::Session::SessionService

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

Overview

The Session Manager manages user sessions on the server side. It is a persistent Session Manager. It allows to save attributes in the session and retrieve them. This Session Manager is especially useful for Mobile/Device Apps which want to do session management.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ SessionService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


32
33
34
35
36
37
38
39
# File 'lib/session/SessionService.rb', line 32

def initialize(api_key, secret_key, base_url)
  puts "Session Service->initialize"
  @api_key = api_key
  @secret_key = secret_key
  @base_url = base_url
  @resource = "session"
  @version = "1.0"
end

Instance Method Details

#get_all_attribute(sessionId) ⇒ Object

Gets all the attributes for a given session id

Parameters:

  • sessionId
    • The session id for which the attribute has to be fetched

Returns:

  • Session object containing all the attributes and values which are stored for the session id

Raises:

  • App42Exception



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/session/SessionService.rb', line 305

def get_all_attribute(sessionId)
  puts "Get All Attributes Called "
  puts "Base url #{@base_url}"
  response = nil
  sessionObj = nil
  sessionObj = Session.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(sessionId,"Session Id")
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("sessionId", sessionId);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/id/#{sessionId}"
    response = connection.get(signature, resource_url, query_params)
    session = SessionResponseBuilder.new
    sessionObj = session.buildSessionResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return sessionObj
end

#get_attribute(sessionId, attributeName) ⇒ Object

Gets the attribute value in a session whose session id is provided.

Parameters:

  • sessionId
    • The session id for which the attribute has to be fetched

  • attributeName
    • The attribute key that has to be fetched

Returns:

  • Session object containing the attribute and value which is stored for the session id and attribute name

Raises:

  • App42Exception



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/session/SessionService.rb', line 260

def get_attribute(sessionId,attributeName)
  puts "Get Attribute Called "
  puts "Base url #{@base_url}"
  response = nil
  sessionObj = nil
  sessionObj = Session.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(sessionId, "Session Id");
  util.throwExceptionIfNullOrBlank(attributeName, "Attribute Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("sessionId", sessionId);
    params.store("name", attributeName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/id/#{sessionId}/#{attributeName}"
    response = connection.get(signature, resource_url, query_params)
    session = SessionResponseBuilder.new
    sessionObj = session.buildSessionResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return sessionObj
end

#get_session(uName) ⇒ Object

Create Session for the User. If the session does not exist it will create a new session

retrieving attributes.

Parameters:

  • uName
    • Username for which the session has to be created.

Returns:

  • Session object containing the session id of the created session. This id has to be used for storing or

Raises:

  • App42Exception



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/session/SessionService.rb', line 53

def get_session(uName)
  puts "Get Session Called "
  puts "Base url #{@base_url}"
  response = nil
  sessionObj = nil
  sessionObj = Session.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(uName, "User Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"session"=> {
      "userName" => uName
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}"
    response = connection.post(signature, resource_url, query_params, body)
    session = SessionResponseBuilder.new
    sessionObj = session.buildSessionResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return sessionObj
end

#get_session_boolean(uName, isCreate) ⇒ Object

Create User Session based on the isCreate boolean parameter. If isCreate is true and there is an existing session for the user, the existing session is returned. If there is no existing session for the user a new one is created. If isCreate is false and there is an existing session, the existing session is returned if there is no existing session new one is not created

a new one is to be created or not.

retrieving attributes.

Parameters:

  • uName
    • Username for which the session has to be created.

  • isCreate
    • A boolean value for specifying if an existing session is not there, should

Returns:

  • Session object containing the session id of the created session. This id has to be used for storing or

Raises:

  • App42Exception



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/session/SessionService.rb', line 108

def get_session_boolean(uName, isCreate)
  puts "Get Session Boolean Called "
  puts "Base url #{@base_url}"
  response = nil
  sessionObj = nil
  sessionObj = Session.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(uName, "User Name");
  util.throwExceptionIfNullOrBlank(isCreate, "Is Create");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"session"=> {
      "userName" => uName
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{isCreate}"
    response = connection.post(signature, resource_url, query_params, body)
    session = SessionResponseBuilder.new
    sessionObj = session.buildSessionResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return sessionObj
end

#invalidate(sessionId) ⇒ Object

Invalidate a session based on the session id. All the attributes store in the session will be lost.

Parameters:

  • sessionId
    • The session id for which the session has to be invalidated.

Returns:

  • App42Response if invalidated successfully

Raises:

  • App42Exception



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/session/SessionService.rb', line 156

def invalidate(sessionId)
  puts "In Validate Called "
  puts "Base url #{@base_url}"
  response = nil
  sessionObj = nil
  sessionObj = Session.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(sessionId, "Session Id");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"session"=> {
      "id" => sessionId
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}"
    response = connection.put(signature, resource_url, query_params, body)
    session = SessionResponseBuilder.new
    sessionObj = session.buildSessionResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return sessionObj
end

#remove_all_attribute(sessionId) ⇒ Object

Removes all the attributes for a given session id

Parameters:

  • sessionId
    • The session id for which the attributes has to be removed

Returns:

  • App42Response if removed successfully

Raises:

  • App42Exception



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/session/SessionService.rb', line 394

def remove_all_attribute(sessionId)
  puts "Remove All Attributes Called "
  puts "Base url #{@base_url}"
  response = nil
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(sessionId, "Session Id");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("sessionId", sessionId)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/id/#{sessionId}"
    response = connection.delete(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#remove_attribute(sessionId, attributeName) ⇒ Object

Removes the attribute from a session whose session id is provided.

Parameters:

  • sessionId
    • The session id for which the attribute has to be removed

  • attributeName
    • The attribute key that has to be removed

Returns:

  • App42Response if removed successfully

Raises:

  • App42Exception



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/session/SessionService.rb', line 350

def remove_attribute(sessionId,attributeName)
  puts "Remove Attribute Called "
  puts "Base url #{@base_url}"
  response = nil
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(sessionId, "Session Id");
  util.throwExceptionIfNullOrBlank(attributeName, "Attribute Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("sessionId", sessionId)
    params.store("name", attributeName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/id/#{sessionId}/#{attributeName}"
    response = connection.delete(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#set_attribute(sessionId, attributeName, attributeValue) ⇒ Object

Sets attribute in a session whose session id is provided. Attributes are stored in a key value pair.

Parameters:

  • sessionId
    • Session id for which the attribute has to be saved.

  • attributeName
    • The attribute key that needs to be stored

  • attributeValue
    • The attribute value that needs to be stored

Returns:

  • Session object containing the attribute and value which is stored

Raises:

  • App42Exception



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/session/SessionService.rb', line 207

def set_attribute(sessionId,attributeName,attributeValue)
  puts "Set Attribute Called "
  puts "Base url #{@base_url}"
  response = nil
  sessionObj = nil
  sessionObj = Session.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(sessionId, "Session Id");
  util.throwExceptionIfNullOrBlank(attributeName, "Attribute Name");
  util.throwExceptionIfNullOrBlank(attributeValue, "Attribute Value");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"session"=> {
      "name" => attributeName,
      "value" => attributeValue
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
      'sessionId'=> sessionId
    }
    query_params = params.clone
    params.store("body", body)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/id/#{sessionId}"
    response = connection.post(signature, resource_url, query_params, body)
    session = SessionResponseBuilder.new
    sessionObj = session.buildSessionResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return sessionObj
end