require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'rubygems' require 'App42Response.rb' require 'appTab/BillResponseBuilder.rb' require 'appTab/Bill.rb' require 'appTab/BillMonth.rb' module App42 module AppTab # # AppTab - Billing service. This service is used along with the Usage service. It generates # Bill for a particular based on Usage Scheme. For e.g. if user sid's bill has to be seen # for May and 2012. This service will list all the charging transactions and calculate the bill for May # and tell the total usage and price. The calculation is done based on the Price which is given during # scheme creation, the unit of charging and corresponding usage. # AppTab currently just maintains the data and does calculation. How the Bill is rendered and the interface # with Payment Gateway is left with the App developer. # # @see Usage # @see Bill # class BillService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "BillService->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "bill" @version = "1.0" end # # Get usage for Scheme based on Month and Year. This is useful to show the # user the charging details of the User for the Scheme # # @param userName # - The user for which the charging information has to be # fetched # @param usageName # - The name of the Scheme # @param billMonth # - The month name for which the usage has to be fetched e.g. # BillMonth.JANUARY, BillMonth.DECEMBER # @param year # - The year for which the usage has to be fetched e.g. 2012, # 2011 # # @returns All the charging transactions with the total usage and total # price for that month # # @throws App42Exception # def usage_time_by_month_and_year(userName, usageName, billMonth, year) puts "usageTimeByMonthAndYear Called " puts "Base url #{@base_url}" response = nil; billObj = nil; billObj = Bill.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "UserName"); util.throwExceptionIfNullOrBlank(usageName, "UsageName"); util.throwExceptionIfNullOrBlank(billMonth, "billMonth"); util.throwExceptionIfNullOrBlank(year, "Year"); 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 bm = BillMonth.new() params.store("usageName", usageName); params.store("year", "" + (year.to_i).to_s); params.store("month", bm.enum(billMonth)); params.store("userName", userName); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/time/#{usageName}/#{(year.to_i).to_s}/#{billMonth}/#{userName}" response = connection.get(signature, resource_url, query_params) billObj = BillResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return billObj end # # Get usage for Scheme based on Month and Year. This is useful to show the # user the charging details of the User for the Scheme # # @param userName # - The user for which the charging information has to be # fetched # @param usageName # - The name of the Scheme # @param billMonth # - The month name for which the usage has to be fetched e.g. # BillMonth.JANUARY, BillMonth.DECEMBER # @param year # - The year for which the usage has to be fetched e.g. 2012, # 2011 # # @returns All the charging transactions with the total usage and total # price for that month # # @throws App42Exception # def usage_storage_by_month_and_year(userName, usageName, billMonth, year) puts "usageStorageByMonthAndYear Called " puts "Base url #{@base_url}" response = nil; billObj = nil; billObj = Bill.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "UserName"); util.throwExceptionIfNullOrBlank(usageName, "UsageName"); util.throwExceptionIfNullOrBlank(billMonth, "billMonth"); util.throwExceptionIfNullOrBlank(year, "Year"); 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 bm = BillMonth.new() params.store("usageName", usageName); params.store("year", "" + (year.to_i).to_s); params.store("month", bm.enum(billMonth)); params.store("userName", userName); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/storage/#{usageName}/#{(year.to_i).to_s}/#{billMonth}/#{userName}" response = connection.get(signature, resource_url, query_params) billObj = BillResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return billObj end # # Get usage for Scheme based on Month and Year. This is useful to show the # user the charging details of the User for the Scheme # # @param userName # - The user for which the charging information has to be # fetched # @param usageName # - The name of the Scheme # @param billMonth # - The month name for which the usage has to be fetched e.g. # BillMonth.JANUARY, BillMonth.DECEMBER # @param year # - The year for which the usage has to be fetched e.g. 2012, # 2011 # # @returns All the charging transactions with the total usage and total # price for that month # # @throws App42Exception # def usage_bandwidth_by_month_and_year(userName, usageName, billMonth, year) puts "usageBandwidthByMonthAndYear Called " puts "Base url #{@base_url}" response = nil; billObj = nil; billObj = Bill.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "UserName"); util.throwExceptionIfNullOrBlank(usageName, "UsageName"); util.throwExceptionIfNullOrBlank(billMonth, "billMonth"); util.throwExceptionIfNullOrBlank(year, "Year"); 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 bm = BillMonth.new() params.store("usageName", usageName); params.store("year", "" + (year.to_i).to_s); params.store("month", bm.enum(billMonth)); params.store("userName", userName); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/bandwidth/#{usageName}/#{(year.to_i).to_s}/#{billMonth}/#{userName}" response = connection.get(signature, resource_url, query_params) billObj = BillResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return billObj end # # Get usage for Scheme based on Month and Year. This is useful to show the # user the charging details of the User for the Scheme # # @param userName # - The user for which the charging information has to be # fetched # @param usageName # - The name of the Scheme # @param billMonth # - The month name for which the usage has to be fetched e.g. # BillMonth.JANUARY, BillMonth.DECEMBER # @param year # - The year for which the usage has to be fetched e.g. 2012, # 2011 # # @returns All the charging transactions with the total usage and total # price for that month # # @throws App42Exception # def usage_level_by_month_and_year(userName, usageName, billMonth, year) puts "usageLevelByMonthAndYear Called " puts "Base url #{@base_url}" response = nil; billObj = nil; billObj = Bill.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "UserName"); util.throwExceptionIfNullOrBlank(usageName, "UsageName"); util.throwExceptionIfNullOrBlank(billMonth, "billMonth"); util.throwExceptionIfNullOrBlank(year, "Year"); 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 bm = BillMonth.new() params.store("usageName", usageName); params.store("year", "" + (year.to_i).to_s); params.store("month", bm.enum(billMonth)); params.store("userName", userName); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/level/#{usageName}/#{(year.to_i).to_s}/#{billMonth}/#{userName}" response = connection.get(signature, resource_url, query_params) billObj = BillResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return billObj end # # Get usage for Scheme based on Month and Year. This is useful to show the # user the charging details of the User for the Scheme # # @param userName # - The user for which the charging information has to be # fetched # @param usageName # - The name of the Scheme # @param billMonth # - The month name for which the usage has to be fetched e.g. # BillMonth.JANUARY, BillMonth.DECEMBER # @param year # - The year for which the usage has to be fetched e.g. 2012, # 2011 # # @returns All the charging transactions with the total usage and total # price for that month # # @throws App42Exception # def usage_one_time_by_month_and_year(userName, usageName, billMonth, year) puts "usageOneTimeByMonthAndYear Called " puts "Base url #{@base_url}" response = nil; billObj = nil; billObj = Bill.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "UserName"); util.throwExceptionIfNullOrBlank(usageName, "UsageName"); util.throwExceptionIfNullOrBlank(billMonth, "billMonth"); util.throwExceptionIfNullOrBlank(year, "Year"); 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 bm = BillMonth.new() params.store("usageName", usageName); params.store("year", "" + (year.to_i).to_s); params.store("month", bm.enum(billMonth)); params.store("userName", userName); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/oneTime/#{usageName}/#{(year.to_i).to_s}/#{billMonth}/#{userName}" response = connection.get(signature, resource_url, query_params) billObj = BillResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return billObj end # # Get usage for Scheme based on Month and Year. This is useful to show the # user the charging details of the User for the Scheme # # @param userName # - The user for which the charging information has to be # fetched # @param usageName # - The name of the Scheme # @param billMonth # - The month name for which the usage has to be fetched e.g. # BillMonth.JANUARY, BillMonth.DECEMBER # @param year # - The year for which the usage has to be fetched e.g. 2012, # 2011 # # @returns All the charging transactions with the total usage and total # price for that month # # @throws App42Exception # def usage_feature_by_month_and_year(userName, usageName, billMonth, year) puts "usageFeatureByMonthAndYear Called " puts "Base url #{@base_url}" response = nil; billObj = nil; billObj = Bill.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "UserName"); util.throwExceptionIfNullOrBlank(usageName, "UsageName"); util.throwExceptionIfNullOrBlank(billMonth, "billMonth"); util.throwExceptionIfNullOrBlank(year, "Year"); 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 bm = BillMonth.new() params.store("usageName", usageName); params.store("year", "" + (year.to_i).to_s); params.store("month", bm.enum(billMonth)); params.store("userName", userName); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/feature/#{usageName}/#{(year.to_i).to_s}/#{billMonth}/#{userName}" response = connection.get(signature, resource_url, query_params) billObj = BillResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return billObj end # # Get usage for Scheme based on Month and Year. This is useful to show the # user the charging details of the User for the Scheme # # @param userName # - The user for which the charging information has to be # fetched # @param licenseName # - The name of the License # @param billMonth # - The month name for which the usage has to be fetched e.g. # BillMonth.JANUARY, BillMonth.DECEMBER # @param year # - The year for which the usage has to be fetched e.g. 2012, # 2011 # # @returns All the charging transactions with the total usage and total # price for that month # # @throws App42Exception # # def usage_license_by_month_and_year(userName, licenseName, billMonth, year) puts "usageLicenseByMonthAndYear Called " puts "Base url #{@base_url}" response = nil; billObj = nil; billObj = Bill.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "UserName"); util.throwExceptionIfNullOrBlank(licenseName, "LicenseName"); util.throwExceptionIfNullOrBlank(billMonth, "billMonth"); util.throwExceptionIfNullOrBlank(year, "Year"); 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 bm = BillMonth.new() params.store("licenseName", licenseName); params.store("year", "" + (year.to_i).to_s); params.store("month", bm.enum(billMonth)); params.store("userName", userName); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/license/#{licenseName}/#{(year.to_i).to_s}/#{billMonth}/#{userName}" response = connection.get(signature, resource_url, query_params) billObj = BillResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return billObj end end end end