require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'rubygems' require 'App42Response.rb' require 'appTab/Test.rb' require 'appTab/DiscountType.rb' require 'appTab/DiscountResponseBuilder.rb' module App42 module AppTab class DiscountService def initialize(api_key, secret_key, base_url) puts "DiscountService->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "discount" @version = "1.0" end # # create a discount package for the app. # # @param discountName # - Create a Discount Name which should be unique # @param discountType # - Type of discount that you want't to create Like Storage, Bandwidth etc # @param discount # - Discount contains the details of the package you create # like discountPercent , discountUsage , startDate, endDate etc # @param description # - Description of the discountData to be created # # @return The Created DiscountData Object # # @raise App42Exception # def create_discount(discountName, discountType, discount, description) puts "createDiscount Called " puts "Base url #{@base_url}" response = nil; discountDataObj = nil; discountDataObj = App42::AppTab::Test.new() util = Util.new util.throwExceptionIfNullOrBlank(discountName, "discountName"); util.throwExceptionIfNullOrBlank(discountType, "DiscountType"); util.throwExceptionIfNullOrBlank(discount, "discount"); util.throwExceptionIfNullOrBlank(description, "description"); begin connection = App42::Connection::RESTConnection.new(@base_url) discountObj = Hash.new() discountObj.store("percentage", discount.percentage); discountObj.store("usage", discount.usage); strStartdate = util.get_timestamp_utc_from_date_discount(discount.startDate) strEnddate = util.get_timestamp_utc_from_date_discount(discount.endDate) discountObj.store("startDate", strStartdate); discountObj.store("endDate", strEnddate); body = {'app42' => {"app"=> {"discount"=>{ "discountName" => discountName, "description" => description, "type" => discountType, "discountDetails" => discountObj }}}}.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 params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.post(signature, resource_url, query_params, body) dis = DiscountResponseBuilder.new discountDataObj = dis.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return discountDataObj end # # NEW METHOD # def get_all_discount() puts "getAllDiscount called" puts "Base url #{@base_url}" response = nil; discountList = nil; discountList = Array.new() util = Util.new 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 puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.get(signature, resource_url, query_params) dis = DiscountResponseBuilder.new discountList = dis.buildArrayObject(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return discountList end # # NEW METHOD # def get_discount_by_name(discountName) puts "getDiscountByName called" puts "Base url #{@base_url}" response = nil; discountDataObj = nil; discountDataObj = Test.new() util = Util.new util.throwExceptionIfNullOrBlank(discountName, "discountName"); 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("discountName", discountName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{discountName}" response = connection.get(signature, resource_url, query_params) dis = DiscountResponseBuilder.new discountDataObj = dis.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return discountDataObj end # # NEW METHOD # def delete_discount_by_name(discountName) puts "deleteDiscountByName called" puts "Base url #{@base_url}" response = nil; discountDataObj = nil; discountDataObj = Test.new() util = Util.new util.throwExceptionIfNullOrBlank(discountName, "discountName"); 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("discountName", discountName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{discountName}" response = connection.delete(signature, resource_url, query_params) dis = DiscountResponseBuilder.new discountDataObj = dis.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return discountDataObj end # # @param promoCode # @return # @throws App42Exception # def get_discount_by_promo_code(promoCode) puts "get_discount_by_promo_code called" puts "Base url #{@base_url}" response = nil; discountDataObj = nil; discountDataObj = Test.new() util = Util.new util.throwExceptionIfNullOrBlank(promoCode, "promoCode"); 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("promoCode", promoCode); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/discount/#{promoCode}" response = connection.get(signature, resource_url, query_params) dis = DiscountResponseBuilder.new discountDataObj = dis.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return discountDataObj end end end end