Class: Kaltura::KalturaClientBase

Inherits:
Object
  • Object
show all
Defined in:
lib/kaltura_client_base.rb

Direct Known Subclasses

KalturaClient

Constant Summary collapse

FORMATS =
{
:KALTURA_SERVICE_FORMAT_JSON => 1, 
:KALTURA_SERVICE_FORMAT_XML => 2,
:KALTURA_SERVICE_FORMAT_PHP => 3}
KALTURA_API_VERSION =
"0.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ KalturaClientBase

Kaltura client constuctor, expecting configuration object

Parameters:

  • KalturaConfiguration

    $config



73
74
75
76
77
78
79
80
81
82
# File 'lib/kaltura_client_base.rb', line 73

def initialize(config)
@shouldLog = false
@config = config;

logger = config.getLogger()
  
if logger != nil
    @shouldLog = true
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



39
40
41
# File 'lib/kaltura_client_base.rb', line 39

def config
  @config
end

#ksObject

Returns the value of attribute ks.



40
41
42
# File 'lib/kaltura_client_base.rb', line 40

def ks
  @ks
end

#shouldLogObject

Returns the value of attribute shouldLog.



41
42
43
# File 'lib/kaltura_client_base.rb', line 41

def shouldLog
  @shouldLog
end

Instance Method Details

#addOptionalParam(params, paramName, paramValue) ⇒ Object



141
142
143
# File 'lib/kaltura_client_base.rb', line 141

def addOptionalParam(params, paramName, paramValue)
	params[paramName] = paramValue if paramValue
end

#connectionObject



49
50
51
# File 'lib/kaltura_client_base.rb', line 49

def connection
  @connection ||= ActiveResource::Connection.new(@config.serviceUrl)
end

#escape(s) ⇒ Object

Escapes a query parameter. Stolen from RFuzz



62
63
64
65
66
# File 'lib/kaltura_client_base.rb', line 62

def escape(s)
  s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
    '%' + $1.unpack('H2'*$1.size).join('%').upcase
  }.tr(' ', '+')
end

#getKsObject



133
134
135
# File 'lib/kaltura_client_base.rb', line 133

def getKs()
	@ks
end

#headersObject



57
58
59
# File 'lib/kaltura_client_base.rb', line 57

def headers
  @headers ||= { 'Content-Type' => 'application/x-www-form-urlencoded' }
end

#hit(method, session_user, params) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kaltura_client_base.rb', line 84

def hit(method, session_user, params)
   start_time = Time.now
   
   log("service url: [#{@config.serviceUrl}]");
   log("trying to call method: [#{method}] for user id: [#{session_user.userId}] using session: [#{@ks}]");
   
   # append the basic params
   params["kaltura_api_version"] 	= KalturaClientBase::KALTURA_API_VERSION
   params["partner_id"] 			= @config.partnerId
   params["subp_id"] 				= @config.subPartnerId
   params["format"]          = @config.format
   params["uid"]             = session_user.userId
   addOptionalParam(params, "user_name", session_user.screenName)
   addOptionalParam(params, "ks", @ks)
   
   params["kalsig"] = signature(params);
 
   url = "/index.php/partnerservices2/" + method
   log("full reqeust url: [#{url}] params: [#{serialize_params(params)}]")
   
	response = post(url, params)
   
   log("result (serialized): #{response.body}");
   
	json = JSON.parse(response.body)
   #log("result (object dump): #{dump}");
   
   end_time = Time.now
   
   log("execution time for method [#{method}]: [#{end_time - start_time}]")
   
   return json
end

#log(msg) ⇒ Object



145
146
147
148
149
150
# File 'lib/kaltura_client_base.rb', line 145

def log(msg)
   #print "#{msg}\n"
	if @shouldLog
		config.getLogger().log(msg)
	end
end

#post(path, options) ⇒ Object



53
54
55
# File 'lib/kaltura_client_base.rb', line 53

def post(path, options)
  connection.post(path, serialize_params(options), headers)
end

#serialize_params(params) ⇒ Object



43
44
45
46
47
# File 'lib/kaltura_client_base.rb', line 43

def serialize_params(params)
  params.keys.map {|key| key.to_s }.sort.map {|key|
    "#{escape(key)}=#{escape(params[key])}"
  }.join("&")
end

#setKs(ks) ⇒ Object



137
138
139
# File 'lib/kaltura_client_base.rb', line 137

def setKs(ks)
	@ks = ks
end

#signature(params) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/kaltura_client_base.rb', line 125

def signature(params)
   str = params.keys.map {|key| key.to_s }.sort.map {|key|
     "#{escape(key)}#{escape(params[key])}"
   }.join("")
   
   Digest::MD5.hexdigest(str)
end

#start(session_user, secret) ⇒ Object



118
119
120
121
122
123
# File 'lib/kaltura_client_base.rb', line 118

def start(session_user, secret)
	result = startSession(session_user, secret)
  
	@ks = result["result"]["ks"]
	return result
end