# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'App42Response.rb' require 'gallery/AlbumResponseBuilder.rb' require 'gallery/Album.rb' module App42 module Gallery # # Create Photo Gallery on the cloud. This service allows to manage i.e. create, # retrieve and remove albums on the cloud. Its useful for Mobile/Device App and Web App developer # who want Photo Gallery functionality. It gives them a complete Photo Gallery out of the # box and reduces the footprint on the device. Developers can focus on how the Photo Gallery # will be rendered and this Cloud API will manage the Gallery on the cloud thereby reducing development # time. # # @see Album # @see Photo # class AlbumService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # 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 # # Creates Album on the cloud # # @param userName # - The user to which the album belongs # @param albumName # - Name of the album to be created on the cloud # @param albumDescription # - Description of the album to be created # # @return Album object containing the album which has been created # # @raise App42Exception # def create_album(userName, albumName, albumDescription) puts "Create Album Called " puts "Base url #{@base_url}" response = nil; albumObj = nil; albumObj = Album.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album Name"); util.throwExceptionIfNullOrBlank(albumDescription, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"album"=> { "name" => albumName, "description" => albumDescription }}}.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}/album/#{userName}" response = connection.post(signature, resource_url, query_params, body) puts "Response is #{response}" album = AlbumResponseBuilder.new() albumObj = album.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return albumObj end # # Fetches all the Albums based on the userName # # @param userName # - The user for which the albums have to be fetched # # @return Album object containing all the album for the given userName # # @raise App42Exception # def get_albums(userName) puts "Get Albums 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) 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) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/album/#{userName}" response = connection.get(signature, resource_url, query_params) album = AlbumResponseBuilder.new() albumObj = album.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return albumObj end # # Fetches all the Albums based on the userName by Paging. # # @param userName # - The user for which the albums have to be fetched # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Album object containing all the album for the given userName # # @raise App42Exception # def get_albums_by_paging(userName, max, offset) puts "getAlbumsByPaging Called " puts "Base url #{@base_url}" response = nil; albumList = nil; albumList = Array.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); 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("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}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) album = AlbumResponseBuilder.new() albumObj = album.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return albumObj end # # Fetch all Album based on the userName and albumName # # @param userName # - The user for which the album has to be fetched # @param albumName # - Name of the album that has to be fetched # # @return Album object containing album for the given userName and albumName # # @raise App42Exception # def get_album_by_name(userName, albumName) puts "getAlbumByName Called " puts "Base url #{@base_url}" response = nil; albumObj = nil; albumObj = Album.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album Name"); 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("albumName", albumName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/album/#{userName}/#{albumName}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" album = AlbumResponseBuilder.new() albumObj = album.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return albumObj end # # Removes the album based on the userName and albumName. Note: All photos added to this Album will also be removed # # @param userName # - The user for which the album has to be removed # @param albumName # - Name of the album that has to be removed # # @return App42Response if removed successfully # # @raise App42Exception # def remove_album(userName, albumName) puts "Delete Album 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) 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("albumName", albumName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}" 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 # # Fetches the count of all the Albums based on the userName # # @param userName # - The user for which the count of albums have to be fetched # # @return App42Response object containing the count of all the album for the given userName # # @raise App42Exception # def get_albums_count(userName) puts "getAlbumsCount Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.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}/album/#{userName}/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 end end end