# ----------------------------------------------------------------------- # 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 'social/SocialResponseBuilder.rb' require 'social/Social.rb' module App42 module Social # # Connect to the User's multiple social accounts. Also used to update the # status individually or all at once for the linked social accounts. # class SocialService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Social Service->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "social" @version = "1.0" end # # Links the User Facebook access credentials to the App User account. # @param userName # - Name of the user whose Facebook account to be linked # @param appId # - Facebook App Id # @param appSecret # - Facebook App Secret # @param accessToken # - Facebook Access Token that has been received after authorization # # @return The Social object # # @raise App42Exception # def link_user_facebook_account(userName, accessToken, appId, appSecret) response = nil socialObj = Social.new socialObj = nil util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName") util.throwExceptionIfNullOrBlank(accessToken, "accessToken") util.throwExceptionIfNullOrBlank(appId, "appId") util.throwExceptionIfNullOrBlank(appSecret, "appSecret") begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "accessToken" => accessToken, "appId" => appId, "appSecret" => appSecret }}}.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}/facebook/linkuser" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Links the User Facebook access credentials to the App User account. # # @param userName # - Name of the user whose Facebook account to be linked # @param accessToken # - Facebook Access Token that has been received after authorization # # @return The Social object # # @raise App42Exception # def link_user_facebook_account_with_token(userName, accessToken) response = nil socialObj = Social.new socialObj = nil util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName") util.throwExceptionIfNullOrBlank(accessToken, "accessToken") begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "accessToken" => accessToken }}}.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}/facebook/linkuser/accesscredentials" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Updates the Facebook status of the specified user. # # @param userName # - Name of the user for whom the status needs to be updated # @param status # - status that has to be updated # # @return The Social object # # @raise App42Exception # def update_facebook_status(userName,status) puts "update Status Called " puts "Base url #{@base_url}" response = nil; socialObj = nil; socialObj = Social.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName"); util.throwExceptionIfNullOrBlank(status, "status"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "status" => status }}}.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}/facebook/updatestatus" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Links the User Twitter access credentials to the App User account. # # @param userName # - Name of the user whose Twitter account to be linked # @param consumerKey # - Twitter App Consumer Key # @param consumerSecret # - Twitter App Consumer Secret # @param accessToken # - Request Token retrieved from Twitter after authorizing the App # @param accessTokenSecret # - Request Token Secret retrieved from Twitter after authorizing the App # # @return the Social object # # @raise App42Exception # def link_user_twitter_account(userName, accessToken, accessTokenSecret, consumerKey, consumerSecret) puts "linkUserTwitterAccount Called " puts "Base url #{@base_url}" response = nil social = Social.new social = nil util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName") util.throwExceptionIfNullOrBlank(accessToken, "accessToken") util.throwExceptionIfNullOrBlank(accessTokenSecret, "accessTokenSecret") util.throwExceptionIfNullOrBlank(consumerKey, "consumerKey") util.throwExceptionIfNullOrBlank(consumerSecret, "consumerSecret") begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "accessToken" => accessToken, "accessTokenSecret" => accessTokenSecret, "consumerKey" => consumerKey, "consumerSecret" => consumerSecret }}}.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}/twitter/linkuser" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Links the User Twitter access credentials to the App User account. # # @param userName # - Name of the user whose Twitter account to be linked # @param accessToken # - Request Token retrieved from Twitter after authorizing the App # @param accessTokenSecret # - Request Token Secret retrieved from Twitter after authorizing the App # # @return the Social object # # @raise App42Exception # def link_user_twitter_account_with_token(userName, accessToken, accessTokenSecret) puts "linkUserTwitterAccount Called " puts "Base url #{@base_url}" response = nil social = Social.new social = nil util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName") util.throwExceptionIfNullOrBlank(accessToken, "accessToken") util.throwExceptionIfNullOrBlank(accessTokenSecret, "accessTokenSecret") begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "accessToken" => accessToken, "accessTokenSecret" => accessTokenSecret }}}.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}/twitter/linkuser/accesscredentials" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Updates the Twitter status of the specified user. # # @param userName # - Name of the user for whom the status needs to be updated # @param status # - status that has to be updated # # @return The Social object # # @raise App42Exception # def update_twitter_status(userName,status) puts "updateStatus Called " puts "Base url #{@base_url}" response = nil; socialObj = Social.new socialObj = nil; util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName"); util.throwExceptionIfNullOrBlank(status, "status"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "status" => status }}}.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}/twitter/updatestatus" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Links the User LinkedIn access credentials to the App User account. # # @param userName # - Name of the user whose LinkedIn account to be linked # @param apiKey # - LinkedIn App API Key # @param secretKey # - LinkedIn App Secret Key # @param accessToken # - LinkedIn Access Token that has been received after authorization # @param accessTokenSecret # - LinkedIn Access Token Secret that has been received after authorization # # @return The Social object # # @raise App42Exception # def link_user_linkedIn_account(userName, accessToken, accessTokenSecret, apiKey, secretKey) puts "linkUserLinkedInAccount Called " puts "Base url #{@base_url}" response = nil social = Social.new social = nil util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName") util.throwExceptionIfNullOrBlank(accessToken, "accessToken") util.throwExceptionIfNullOrBlank(accessTokenSecret, "accessTokenSecret") util.throwExceptionIfNullOrBlank(apiKey, "apiKey") util.throwExceptionIfNullOrBlank(secretKey, "secretKey") begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "accessToken" => accessToken, "accessTokenSecret" => accessTokenSecret, "apiKey" => apiKey, "secretKey" => secretKey }}}.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}/linkedin/linkuser" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Links the User LinkedIn access credentials to the App User account. # # @param userName # - Name of the user whose LinkedIn account to be linked # @param accessToken # - LinkedIn Access Token that has been received after authorization # @param accessTokenSecret # - LinkedIn Access Token Secret that has been received after authorization # # @return The Social object # # @raise App42Exception # def link_user_linkedIn_account_with_token(userName, accessToken, accessTokenSecret) puts "linkUserLinkedInAccount Called " puts "Base url #{@base_url}" response = nil social = Social.new social = nil util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName") util.throwExceptionIfNullOrBlank(accessToken, "accessToken") util.throwExceptionIfNullOrBlank(accessTokenSecret, "accessTokenSecret") begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "accessToken" => accessToken, "accessTokenSecret" => accessTokenSecret }}}.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}/linkedin/linkuser/accesscredentials" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Updates the LinkedIn status of the specified user. # # @param userName # - Name of the user for whom the status needs to be updated # @param status # - status that has to be updated # # @return The Social object # # @raise App42Exception # def update_linkedIn_status(userName, status) puts "updateStatus Called " puts "Base url #{@base_url}" response = nil; socialObj = nil; socialObj = Social.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName") util.throwExceptionIfNullOrBlank(status, "status") begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "status" => status }}}.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}/linkedin/updatestatus" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return socialObj end # # Updates the status for all linked social accounts of the specified user. # # @param userName # - Name of the user for whom the status needs to be updated # @param status # - status that has to be updated # # @return The Social object # # @raise App42Exception # def update_social_status_for_all(userName, status) puts "updateStatus Called " puts "Base url #{@base_url}" response = nil; socialObj = Social.new socialObj = nil; util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName") util.throwExceptionIfNullOrBlank(status, "status") begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"social"=> { "userName" => userName, "status" => status }}}.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}/social/updatestatus/all" response = connection.post(signature, resource_url, query_params, body) social = SocialResponseBuilder.new() socialObj = social.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end puts socialObj.userName() return socialObj end end end end