# ----------------------------------------------------------------------- # 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 'review/ReviewResponseBuilder.rb' require 'review/Review.rb' module App42 module Review # # The service is a Review & Rating manager for any item. The item can be anything which has an id # # e.g. App on a AppStore/Marketplace, items in a catalogue, articles, blogs etc. # It manages the comments and its associated rating. It also provides methods to fetch average, highest etc. Reviews. # Reviews can be also be muted or unmuted if it has any objectionable content. # # @see Review # class ReviewService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Session Service->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "review" @version = "1.0" end # # Creates review for the specified item on the cloud # # @param userId # - The user who has created the review # @param itemId # - The item for which the review has to be created # @param reviewComment # - The review comment # @param reviewRating # - Review rating in double # # @return Review object containing the review which has been created # # @raise App42Exception # def create_review(userID, itemID, reviewComment, reviewRating) puts "create_review Called " puts "Base url #{@base_url}" response = nil reviewObj = nil reviewObj = Review.new() util = Util.new util.throwExceptionIfNullOrBlank(userID, "User Id"); util.throwExceptionIfNullOrBlank(itemID, "Item Id"); util.throwExceptionIfNullOrBlank(reviewComment, "Review Comment"); util.throwExceptionIfNullOrBlank(reviewRating, "Review Rating"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"review"=> { "userId" => userID, "itemId" => itemID, "comment" => reviewComment, "rating" => (reviewRating.to_i).to_s }}}.to_json puts "Body #{body}" params = Hash.new 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) review = ReviewResponseBuilder.new reviewObj = review.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewObj end # # Fetches all reviews for the App # # @return ArrayList of Review object containing all the reviews for the App # # @raise App42Exception # def get_all_reviews() puts "getAllAttribute Review Called " puts "Base url #{@base_url}" response = nil reviewList = nil reviewList = 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 query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.get(signature, resource_url, query_params) review = ReviewResponseBuilder.new reviewList = review.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewList end # # Fetches all reviews for the App by Paging. # # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return ArrayList of Review object containing all the reviews for the App # # @raise App42Exception # def get_all_reviews_by_paging(max, offset) puts "get_all_reviews_by_paging Called " puts "Base url #{@base_url}" response = nil reviewList = nil reviewList = Array.new util = Util.new util.validateMax(max); 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("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/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) review = ReviewResponseBuilder.new reviewList = review.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewList end # # Fetches All Reviews based on the itemId # # @param itemId # - The item for which reviews have to be fetched # # @return ArrayList of Review object containing all the reviews for a item # # @raise App42Exception # def get_reviews_by_item(itemId) puts "get_reviews_by_item Called " puts "Base url #{@base_url}" response = nil reviewList = nil; reviewList = Array.new util = Util.new util.throwExceptionIfNullOrBlank(itemId, "Item Id"); 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("itemId", itemId); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/item/#{itemId}" response = connection.get(signature, resource_url, query_params) review = ReviewResponseBuilder.new reviewList = review.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewList end # # Fetches All Reviews based on the itemId by Paging. # # @param itemId # - The item for which reviews have to be fetched # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return ArrayList of Review object containing all the reviews for a item # # @raise App42Exception # def get_review_by_item_by_paging(itemId, max, offset) puts "get_review_by_item_by_paging Called " puts "Base url #{@base_url}" response = nil reviewList = nil; reviewList = Array.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); util.throwExceptionIfNullOrBlank(itemId, "Item Id"); 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("max", "" + (max.to_i).to_s); params.store("offset", "" + (offset.to_i).to_s); params.store("itemId", "" + itemId); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/item/#{itemId}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) review = ReviewResponseBuilder.new reviewList = review.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewList end # # Fetches the highest review for the specified itemId # # @param itemId # - The item for which the highest review has to be fetched # # @return Review object containing the highest review for a item # # @raise App42Exception # def get_highest_review_by_item(itemId) puts "get_highest_review_by_item Review Called " puts "Base url #{@base_url}" response = nil reviewObj = nil reviewObj = Review.new() util = Util.new util.throwExceptionIfNullOrBlank(itemId, "Item Id"); 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("itemId", itemId); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{itemId}/highest" response = connection.get(signature, resource_url, query_params) review = ReviewResponseBuilder.new reviewObj = review.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewObj end # # Fetches the lowest review for the specified itemId # # @param itemId # - The item for which the lowest review has to be fetched # # @return Review object containing the lowest review for a item # # @raise App42Exception # def get_lowest_review_by_item(itemId) puts "get_lowest_review_by_item Review Called " puts "Base url #{@base_url}" response = nil reviewObj = nil reviewObj = Review.new() util = Util.new util.throwExceptionIfNullOrBlank(itemId, "Item Id"); 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("itemId", itemId); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{itemId}/lowest" response = connection.get(signature, resource_url, query_params) review = ReviewResponseBuilder.new reviewObj = review.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewObj end # # Fetches the average review for the specified itemId # # @param itemId # - The item for which the average review has to be fetched # # @return Review object containing the average review for a item # # @raise App42Exception # def get_average_review_by_item(itemId) puts "get_average_review_by_item Review Called " puts "Base url #{@base_url}" response = nil reviewObj = nil reviewObj = Review.new() util = Util.new util.throwExceptionIfNullOrBlank(itemId, "Item Id"); 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("itemId", itemId); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{itemId}/average" response = connection.get(signature, resource_url, query_params) review = ReviewResponseBuilder.new reviewObj = review.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewObj end # # Mutes the specified review # # @param reviewId # - The Id of the review which has to be muted # # @return App42Response if muted successfully # # @raise App42Exception # def mute(reviewId) puts "mute review Called " puts "Base url #{@base_url}" response = nil reviewObj = nil reviewObj = Review.new() util = Util.new util.throwExceptionIfNullOrBlank(reviewId, "Review Id"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"review"=> { "id" => reviewId }}}.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}/mute" response = connection.put(signature, resource_url, query_params, body) review = ReviewResponseBuilder.new reviewObj = review.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewObj end # # UnMutes the specified review # # @param reviewId # - The Id of the review which has to be unmuted # # @return App42Response if unmuted successfully # # @raise App42Exception # def unmute(reviewId) puts "unmute review Called " puts "Base url #{@base_url}" response = nil reviewObj = nil reviewObj = Review.new() util = Util.new util.throwExceptionIfNullOrBlank(reviewId, "Review Id"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"review"=> { "id" => reviewId }}}.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}/unmute" response = connection.put(signature, resource_url, query_params, body) review = ReviewResponseBuilder.new reviewObj = review.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return reviewObj end # # Fetches count of all reviews for the App # # @return App42Response containing count of all the reviews for the App # # @raise App42Exception # def get_all_reviews_count() puts "get_all_reviews_count Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.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 query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = ReviewResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Fetches count of All Reviews based on the itemId # # @param itemId # - The item for which count of reviews have to be fetched # # @return App42Response containing count of all the reviews for a item # # @raise App42Exception # def get_reviews_count_by_item(itemId) puts "get_reviews_count_by_item Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(itemId, "Item Id"); 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("itemId", itemId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/item/#{itemId}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = ReviewResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end end end end