Class: App42::Gallery::PhotoService

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

Overview

Adds Photo to the created Album on the Cloud All photos for a given Album can be managed through this service. Photos can be uploaded to the cloud. Uploaded photos are accessible through the generated URL. The service also creates a thumbnail for the Photo which has been uploaded.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ PhotoService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


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

def initialize(api_key, secret_key, base_url)
  puts "Photo Gallery->initialize"
  @api_key = api_key
  @secret_key = secret_key
  @base_url = base_url
  @resource = "gallery"
  @version = "1.0"
end

Instance Method Details

#add_photo(userName, albumName, photoName, photoDescription, path) ⇒ Object

Adds Photo for a particular user and album. The Photo is uploaded on the cloud

Parameters:

  • userName
    • Name of the User whose photo has to be added

  • albumName
    • Name of the Album in which photo has to be added

  • photoName
    • Name of the Photo that has to be added

  • photoDescription
    • Description of the Photo that has to be added

  • path
    • Path from where Photo has to be picked for addition

Returns:

  • Album object containing the Photo which has been added

Raises:

  • App42Exception



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gallery/PhotoService.rb', line 60

def add_photo(userName, albumName, photoName, photoDescription, path)
  puts "Add photo called "
  puts "Base url #{@base_url}"
  response = nil;
  photoObj = nil;
  photoObj = Album.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(albumName, "Album Name");
  util.throwExceptionIfNullOrBlank(photoName, "Photo Name");
  util.throwExceptionIfNullOrBlank(photoDescription, "Description");
  util.throwExceptionIfNullOrBlank(path, "Path");
  if (FileTest.exists?(path) == false)
    raise App42Exception.new("The file with the name #{path} not found")
  end
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    params = Hash.new
    query_params = Hash.new
    query_params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    params = query_params.clone
    post_params = Hash.new
    post_params.store("userName", userName);
    post_params.store("albumName", albumName);
    post_params.store("name", photoName);
    post_params.store("description", photoDescription);
    params = params.merge(post_params)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{userName}"
    response = connection.photoMultipart(signature, resource_url, query_params, params, path)
    puts "Response is #{response}"
    album = AlbumResponseBuilder.new()
    photoObj = album.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return photoObj
end

#add_photo_with_stream(userName, albumName, photoName, photoDescription, spath) ⇒ Object

Adds Photo for a particular user and album via Stream. The Photo is uploaded on the cloud

Parameters:

  • userName
    • Name of the User whose photo has to be added

  • albumName
    • Name of the Album in which photo has to be added

  • photoName
    • Name of the Photo that has to be added

  • photoDescription
    • Description of the Photo that has to be added

  • spath
    • Input Stream for the Photo that has to be added

Returns:

  • Album object containing the Photo which has been added

Raises:

  • App42Exception



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gallery/PhotoService.rb', line 124

def add_photo_with_stream(userName, albumName, photoName, photoDescription, spath)
  puts "Add photo with Stream called "
  puts "Base url #{@base_url}"
  response = nil;
  photoObj = nil;
  photoObj = Album.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(albumName, "Album Name");
  util.throwExceptionIfNullOrBlank(photoName, "Photo Name");
  util.throwExceptionIfNullOrBlank(photoDescription, "Description");
  util.throwExceptionIfNullOrBlank(spath, "Path");
  if (FileTest.exists?(spath) == false)
    raise App42Exception.new("The file with the name #{spath} not found")
  end
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    params = Hash.new
    query_params = Hash.new
    query_params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    params = query_params.clone
    post_params = Hash.new
    post_params.store("userName", userName);
    post_params.store("albumName", albumName);
    post_params.store("name", photoName);
    post_params.store("description", photoDescription);
    params = params.merge(post_params)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{userName}"
    response = connection.photoMultipartStream(signature, resource_url, query_params, params, spath)
    puts "Response is #{response}"
    album = AlbumResponseBuilder.new()
    photoObj = album.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return photoObj
end

#add_tag_to_photo(userName, albumName, photoName, tagList) ⇒ Object

Adds tag to the Photo of the user in the album.

Parameters:

  • userName
    • Name of the User whose name has to be tagged to photo

  • albumName
    • Album name whose photo is to be tagged

  • photoName
    • Photo name to be tagged

  • tagList
    • list of tages in Photo

Returns:

  • Album object containing the Photo which has been added

Raises:

  • App42Exception



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/gallery/PhotoService.rb', line 487

def add_tag_to_photo(userName, albumName, photoName, tagList)
  puts "addTagToPhoto called "
  puts "Base url #{@base_url}"
  response = nil;
  photoObj = nil;
  photoObj = Album.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(albumName, "Album Name");
  util.throwExceptionIfNullOrBlank(photoName, "Photo Name");
  util.throwExceptionIfNullOrBlank(tagList, "TagList");
  if tagList.size() == 0
    raise App42Exception.new("TagList cannot be empty. Please add the name to be tagged");
  end
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    tagArray = Array.new()
    for tag in tagList do
      tagArray.push(tag)
    end
    body = {'app42' => {"photo"=> {
      "userName" => userName,
      "albumName" => albumName,
      "photoName" => photoName, "tags" => {
      "tag" => tagArray
      }}}}.to_json
    puts "Body #{body}"
    params = Hash.new
    query_params = Hash.new
    query_params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    params = query_params.clone
    post_params = Hash.new
    post_params.store("body", body);
    params = params.merge(post_params)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/tag"
    response = connection.post(signature, resource_url, query_params, body)
    puts "Response is #{response}"
    album = AlbumResponseBuilder.new()
    photoObj = album.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return photoObj
end

#get_photos(userName) ⇒ Object

Fetch all the Photos based on the userName

Parameters:

  • userName
    • Name of the User whose photos have to be fetched

Returns:

  • ArrayList of Album object containing all the Photos for the given userName

Raises:

  • App42Exception



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/gallery/PhotoService.rb', line 180

def get_photos(userName)
  puts "Get Photos Called "
  puts "Base url #{@base_url}"
  response = nil;
  albumList = nil;
  albumList = Array.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User 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("userName", userName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{userName}"
    response = connection.get(signature, resource_url, query_params)
    photo = AlbumResponseBuilder.new()
    albumList = photo.buildArrayResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return albumList
end

#get_photos_by_album_and_photo_name(userName, albumName, photoName) ⇒ Object

Fetch the particular photo based on the userName, album name and photo name

Parameters:

  • userName
    • Name of the User whose photo has to be fetched

  • albumName
    • Name of the Album from which photo has to be fetched

  • photoName
    • Name of the Photo that has to be fetched

Returns:

  • Album object containing the Photo for the given userName, albumName and photoName

Raises:

  • App42Exception



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/gallery/PhotoService.rb', line 333

def get_photos_by_album_and_photo_name(userName, albumName, photoName)
  puts "Get Photos Called "
  puts "Base url #{@base_url}"
  response = nil;
  photoObj = nil;
  photoObj = Album.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(albumName, "Album Name");
  util.throwExceptionIfNullOrBlank(photoName, "Photo 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("userName", userName)
    params.store("albumName", albumName)
    params.store("name", photoName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}/#{photoName}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    photo = AlbumResponseBuilder.new()
    photoObj = photo.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return photoObj
end

#get_photos_by_album_name(userName, albumName) ⇒ Object

Fetch all Photos based on the userName and album name

Parameters:

  • userName
    • Name of the User whose photos have to be fetched

  • albumName
    • Name of the Album from which photos have to be fetched

Returns:

  • Album object containing all the Photos for the given userName and albumName

Raises:

  • App42Exception



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/gallery/PhotoService.rb', line 225

def get_photos_by_album_name(userName, albumName)
  puts "Get Photos Called "
  puts "Base url #{@base_url}"
  response = nil;
  photoObj = nil;
  photoObj = Album.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(albumName, "Album 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("userName", userName)
    params.store("albumName", albumName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    photo = AlbumResponseBuilder.new()
    photoObj = photo.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return photoObj
end

#get_photos_by_album_name_by_paging(userName, albumName, max, offset) ⇒ Object

Fetch all Photos based on the userName and album name by paging.

albumName

Parameters:

  • userName
    • Name of the User whose photos have to be fetched

  • albumName
    • Name of the Album from which photos have to be fetched

  • max
    • Maximum number of records to be fetched

  • offset
    • From where the records are to be fetched

Returns:

  • Album object containing all the Photos for the given userName and

Raises:

  • App42Exception



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/gallery/PhotoService.rb', line 278

def get_photos_by_album_name_by_paging(userName, albumName, max, offset)
  puts "getPhotosByAlbumNameByPaging Called "
  puts "Base url #{@base_url}"
  response = nil;
  photoObj = nil;
  photoObj = Album.new
  util = Util.new
  util.validateMax(max);
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(albumName, "Album Name");
  util.throwExceptionIfNullOrBlank(max, "Max");
  util.throwExceptionIfNullOrBlank(offset, "Offset");
  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("userName", userName)
    params.store("albumName", albumName)
    params.store("max", "" + (max.to_i).to_s);
    params.store("offset", "" + (offset.to_i).to_s);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/album/#{userName}/#{albumName}/paging/#{(max.to_i).to_s}/#{(offset.to_i).to_s}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    photo = AlbumResponseBuilder.new()
    photoObj = photo.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return photoObj
end

#get_photos_count_by_album_name(userName, albumName) ⇒ Object

Fetch the count of all Photos based on the userName and album name

albumName

Parameters:

  • userName
    • Name of the User whose count of photos have to be fetched

  • albumName
    • Name of the Album from which count of photos have to be fetched

Returns:

  • App42Response object containing the count of all the Photos for the given userName and

Raises:

  • App42Exception



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/gallery/PhotoService.rb', line 435

def get_photos_count_by_album_name(userName, albumName)
  puts "getPhotosCountByAlbumName Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(albumName, "Album 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("userName", userName);
    params.store("albumName", albumName);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}/count"
    response = connection.get(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
    responseObj = AlbumResponseBuilder.new()
    responseObj.getTotalRecords(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#get_tagged_photos(userName, tag) ⇒ Object

Fetch all the Photos based on the userName and tag

userName

Parameters:

  • userName
    • Name of the User whose photos have to be fetched

  • tag
    • tag on which basis photos have to be fetched

Returns:

  • ArrayList of Album object containing all the Photos for the given

Raises:

  • App42Exception



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/gallery/PhotoService.rb', line 553

def get_tagged_photos(userName, tag)
  puts "getTaggedPhoto called "
  puts "Base url #{@base_url}"
  response = nil;
  albumList = nil;
  albumList = Array.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(tag, "Tag");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    params = Hash.new
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("userName", userName);
    params.store("tag", tag)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/tag/#{tag}/userName/#{userName}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    album = AlbumResponseBuilder.new()
    albumList = album.buildArrayResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return albumList
end

#remove_photo(userName, albumName, photoName) ⇒ Object

Removes the particular Photo from the specified Album for a particular user. Note: The Photo is removed from the cloud and wont be accessible in future

Parameters:

  • userName
    • Name of the User whose photo has to be removed

  • albumName
    • Name of the Album in which photo has to be removed

  • photoName
    • Name of the Photo that has to be removed

Returns:

  • App42Response if removed successfully

Raises:

  • App42Exception



386
387
388
389
390
391
392
393
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
# File 'lib/gallery/PhotoService.rb', line 386

def remove_photo(userName, albumName, photoName)
  puts "Delete Photo Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  util.throwExceptionIfNullOrBlank(albumName, "Album Name");
  util.throwExceptionIfNullOrBlank(photoName, "Photo 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("userName", userName)
    params.store("albumName", albumName)
    params.store("name", photoName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}/#{photoName}"
    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