# ----------------------------------------------------------------------- # 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 'Shopping/CatalogueResponseBuilder.rb' require 'App42Exception.rb' #require 'Shopping/Catalogue.rb' #require 'Shopping/itemData.rb' module App42 module Shopping # # This Service provides a complete cloud based catalogue management. An app can keep # all its items based on category on the Cloud. This service provides several utility # methods to manage catalogue on the cloud. # # One can add items with its related information in a particular category. And there can # be several categories in a catalogue. The App developer can create several catalogues if needed. # # The Cart service can be used along with Catalogue service to create an end to end Shopping feature for # a Mobile and Web App. # # @see Cart, ItemData # class CatalogueService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Shopping ->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "catalogue" @version = "1.0" end # # Creates a Catalogue for a particular App. Categories can be added to the Catalogue # # @param catalogueName # - Name of the Catalogue to be created # @param catalogueDescription # - Description of the catalogue to be createds # # @return Catalogue object # # @raise App42Exception # def create_catalogue(catalogueName, catalogueDescription) puts "Create Catalogue Called " puts "Base url #{catalogueDescription}" response = nil; catalogueObj = nil; catalogueObj = Catalogue.new util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); util.throwExceptionIfNullOrBlank(catalogueDescription, "catalogueDescription"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"catalogue"=> { "name" => catalogueName, "description" => catalogueDescription }}}.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) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.post(signature, resource_url, query_params, body) catalogue = CatalogueResponseBuilder.new() catalogueObj = catalogue.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return catalogueObj end # # Creates a Category for a particular Catalogue e.g. Books, Music etc. # # @param catalogueName # - Name of the Catalogue for which Category has to be created # @param categoryName # - Name of the Category that has to be created # @param categoryDescription # - Description of the category to be created # # @return Catalogue object containing created category information # # @raise App42Exception # def create_category(catalogueName, categoryName, categoryDescription) puts "Create Category Called " puts "Base url #{@base_url}" response = nil; catalogueObj = nil; catalogueObj = Catalogue.new util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "categoryName"); util.throwExceptionIfNullOrBlank(categoryDescription, "categoryDescription"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"catalogue"=> {"categories"=> {"category" => { "name" => categoryName, "description" => categoryDescription, }}}}}.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) params.store("catalogueName", catalogueName) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}/category" response = connection.post(signature, resource_url, query_params, body) catalogue = CatalogueResponseBuilder.new() catalogueObj = catalogue.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return catalogueObj end # # Creates a Item in a Category for a particular Catelogue # # @param catalogueName # - Name of the Catalogue to which item has to be added # @param categoryName # - Name of the Category to which item has to be added # @param itemData # - Item Information that has to be added # # @return Catalogue object containing added item. # # @raise App42Exception # # @see ItemData # def add_item(catalogueName, categoryName, itemData) puts "Add Item Called " puts "Base url #{@base_url}" response = nil; catalogueObj = nil; catalogueObj = Catalogue.new util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "CatalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "CategoryName"); util.throwExceptionIfNullOrBlank(itemData, "ItemData"); file = File.new(itemData.image()) if (FileTest.exists?(file) == false) raise App42Exception.new("The file with the name #{file} not found") end begin puts "file is #{file} and #{itemData.itemId}" connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new post_params=Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, 'catalogueName' => catalogueName, 'categoryName' => categoryName } params = query_params.clone post_params.store("itemId",itemData.itemId) post_params.store("name",itemData.name) post_params.store("description",itemData.description) post_params.store("price",(itemData.price.to_i).to_s) params = params.merge(post_params) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}/item" response = connection.shopMultipart(signature, resource_url, query_params, params, file) catalogue = CatalogueResponseBuilder.new() catalogueObj = catalogue.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return catalogueObj end # # Fetches all items for a Catalogue # # @param catalogueName # - Name of the Catalogue from which item has to be fetched # # @return Catalogue object containing all Items # # @raise App42Exception # def get_items(catalogueName) puts "Get Items Called " puts "Base url #{@base_url}" response = nil; catalogueObj = nil; catalogueObj = Catalogue.new util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); 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 puts params params.store("catalogueName", catalogueName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}" response = connection.get(signature, resource_url, query_params) catalogue = CatalogueResponseBuilder.new catalogueObj = catalogue.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return catalogueObj end # # Fetches all items for a Catalogue and Category # # @param catalogueName # - Name of the Catalogue from which item has to be fetched # @param categoryName # - Name of the Category from which item has to be fetched # # @return Catalogue object # # @raise App42Exception # def get_items_by_category(catalogueName, categoryName) puts "Get Items By Category Called " puts "Base url #{@base_url}" response = nil; catalogueObj = nil; catalogueObj = Catalogue.new util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "categoryName"); 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("catalogueName", catalogueName) params.store("categoryName", categoryName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}" response = connection.get(signature, resource_url, query_params) catalogue = CatalogueResponseBuilder.new catalogueObj = catalogue.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return catalogueObj end # # Fetches Item by id for a Catalogue and Category # # @param catalogueName # - Name of the Catalogue from which item has to be fetched # @param categoryName # - Name of the Category from which item has to be fetched # @param itemId # - Item id for which information has to be fetched. # # @return Catalogue object # # @raise App42Exception # def get_item_by_id(catalogueName, categoryName, itemId) puts "Get Items By ID Called " puts "Base url #{@base_url}" response = nil; catalogueObj = nil; catalogueObj = Catalogue.new util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "categoryName"); util.throwExceptionIfNullOrBlank(itemId, "itemId"); 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 puts params params.store("catalogueName", catalogueName) params.store("categoryName", categoryName) params.store("itemId", itemId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}/#{itemId}" response = connection.get(signature, resource_url, query_params) catalogue = CatalogueResponseBuilder.new catalogueObj = catalogue.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return catalogueObj end # # Removes all Items in a Catalogue # # @param catalogueName # - Name of the Catalogue from which item has to be removed # # @return Catalogue object containing removed items # # @raise App42Exception # def remove_all_items(catalogueName) puts "Remove All Items Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); 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 puts params params.store("catalogueName", catalogueName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}" 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 # # Removes all Items from a Catalogue and Category # # @param catalogueName # - Name of the Catalogue from which item has to be removed # @param categoryName # - Name of the Category from which item has to be removed # # @return Catalogue object containing removed item # # @raise App42Exception # def remove_items_by_category(catalogueName, categoryName) puts "Remove Items By Category Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "categoryName"); 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 puts params params.store("catalogueName", catalogueName) params.store("categoryName", categoryName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}" 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 # # Removes Item by Id # # @param catalogueName # - Name of the Catalogue from which item has to be removed # @param categoryName # - Name of the Category from which item has to be removed # @param itemId # - Item id which has to be removed # # @return Catalogue object containing removed item # # @raise App42Exception # def remove_item_by_id(catalogueName, categoryName, itemId) puts "Remove Items By ID Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "categoryName"); util.throwExceptionIfNullOrBlank(itemId, "itemId"); 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 puts params params.store("catalogueName", catalogueName) params.store("categoryName", categoryName) params.store("itemId", itemId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}/#{itemId}" 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 all items for a Catalogue and Category by paging. # # @param catalogueName # - Name of the Catalogue from which item has to be fetched # @param categoryName # - Name of the Category from which item has to be fetched # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Catalogue object # # @raise App42Exception # def get_items_by_category_by_paging(catalogueName, categoryName, max, offset) puts "Get Items By Category Called " puts "Base url #{@base_url}" response = nil; catalogueObj = nil; catalogueObj = Catalogue.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(catalogueName, "catalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "categoryName"); 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("catalogueName", catalogueName) params.store("categoryName", categoryName) 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}/paging/#{catalogueName}/#{categoryName}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) catalogue = CatalogueResponseBuilder.new catalogueObj = catalogue.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return catalogueObj end # # Fetches count of all items for a Catalogue and Category # # @param catalogueName # - Name of the Catalogue from which count of item has to be fetched # @param categoryName # - Name of the Category from which count of item has to be fetched # # @return App42Response object # # @raise App42Exception # def get_items_count_by_category(catalogueName, categoryName) puts "get_items_count_by_category Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "CatalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "CategoryName"); 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("catalogueName", catalogueName); params.store("categoryName", categoryName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}/#{categoryName}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse = response responseObj.isResponseSuccess = true responseObj = CatalogueResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Deletes category # # @param catalogueName # - Name of the Catalogue from which count of item has to be fetched # @param categoryName # - Name of the Category from which count of item has to be fetched # # @return App42Response object # # @raise App42Exception # def delete_category(catalogueName, categoryName) puts "deleteCategory Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(catalogueName, "CatalogueName"); util.throwExceptionIfNullOrBlank(categoryName, "CategoryName"); 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("catalogueName", catalogueName); params.store("categoryName", categoryName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{catalogueName}/category/#{categoryName}" 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 end end end