Class: Kaltura::ClientBase

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

Direct Known Subclasses

Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ClientBase

Returns a new instance of ClientBase.



7
8
9
10
11
12
13
14
15
# File 'lib/kaltura/kaltura_client_base.rb', line 7

def initialize(config)
	@should_log = false
	@config = config
	@calls_queue = []
	
	if @config.logger != nil
		@should_log = true
	end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/kaltura/kaltura_client_base.rb', line 3

def config
  @config
end

#is_multirequestObject (readonly)

Returns the value of attribute is_multirequest.



5
6
7
# File 'lib/kaltura/kaltura_client_base.rb', line 5

def is_multirequest
  @is_multirequest
end

#ksObject

Returns the value of attribute ks.



4
5
6
# File 'lib/kaltura/kaltura_client_base.rb', line 4

def ks
  @ks
end

Instance Method Details

#add_param(params, name, value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/kaltura/kaltura_client_base.rb', line 123

def add_param(params, name, value)
	if value == nil
		return
	elsif value.is_a? Hash
		value.each do |sub_name, sub_value|
			add_param(params, "#{name}:#{sub_name}", sub_value);
		end
	elsif value.is_a? ObjectBase
		add_param(params, name, value.to_params)
	else
		params[name] = value
	end
end

#do_http_request(url, params) ⇒ Object



78
79
80
81
# File 'lib/kaltura/kaltura_client_base.rb', line 78

def do_http_request(url, params)
	res = RestClient.post url , params
	return res
end

#do_multirequestObject



111
112
113
# File 'lib/kaltura/kaltura_client_base.rb', line 111

def do_multirequest()
	return do_queue()
end

#do_queueObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kaltura/kaltura_client_base.rb', line 29

def do_queue()
	start_time = Time.now
	
	if @calls_queue.length == 0
		@is_multirequest = false
		return nil
	end
			
	log('service url: [' + @config.service_url + ']')
	# append the basic params
	params = {}
	add_param(params, "format", @config.format)
	add_param(params, "clientTag", @config.client_tag)
	
	url = @config.service_url+"/api_v3/index.php?service="
	if (@is_multirequest)
		url += "multirequest"
		i = 1
		@calls_queue.each_value do |call|
			call_params = call.get_params_for_multirequest(i.next)
			params.merge!(call_params)
		end
	else
		call = @calls_queue[0]
		url += call.service + "&action=" + call.action
		params.merge!(call.params)
	end
	
	# reset
	@calls_queue = []
	@is_multirequest = false
	
	signature = signature(params)
	add_param(params, "kalsig", signature)
	
	log("url: " + url)
	log("params: " + params.to_yaml)
	result = do_http_request(url, params)
	
	result_object = parse_to_objects(result.body)

	log("result (object yaml dump): " + result_object.to_yaml)
	
	end_time = Time.now
	log("execution time for [#{url}]: [#{end_time - start_time}]")
	
	return result_object			
end

#escape(s) ⇒ Object

Escapes a query parameter. Stolen from RFuzz



138
139
140
141
142
# File 'lib/kaltura/kaltura_client_base.rb', line 138

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

#is_error(doc) ⇒ Object



103
104
105
# File 'lib/kaltura/kaltura_client_base.rb', line 103

def is_error(doc)
  doc.elements["xml/result/error/message"] && doc.elements["xml/result/error/code"]
end

#log(msg) ⇒ Object



144
145
146
147
148
# File 'lib/kaltura/kaltura_client_base.rb', line 144

def log(msg)
	if @should_log
		config.logger.log(msg)
	end
end

#parse_to_objects(data) ⇒ Object



83
84
85
# File 'lib/kaltura/kaltura_client_base.rb', line 83

def parse_to_objects(data)
	parse_xml_to_objects(data)
end

#parse_xml_to_objects(xml) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/kaltura/kaltura_client_base.rb', line 87

def parse_xml_to_objects(xml)
	doc = REXML::Document.new(xml)
	raise_exception_if_error(doc)
	doc.elements.each('xml/result') do | element |
		return ClassFactory.object_from_xml(element)
	end
end

#queue_service_action_call(service, action, params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kaltura/kaltura_client_base.rb', line 17

def queue_service_action_call(service, action, params = {})
	# in start session partner id is optional (default -1). if partner id was not set, use the one in the config
	if (!params.key?('partnerId') || params['partnerId'] == -1)
		params['partnerId'] = config.partner_id;
	end	
	
	add_param(params, 'ks', @ks);
	
	call = ServiceActionCall.new(service, action, params);
	@calls_queue.push(call);
end

#raise_exception_if_error(doc) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/kaltura/kaltura_client_base.rb', line 95

def raise_exception_if_error(doc)
  if is_error(doc)
    code = doc.elements["xml/result/error/code"].text
    message = doc.elements["xml/result/error/message"].text
    raise APIError.new(code,message)
  end
end

#signature(params) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/kaltura/kaltura_client_base.rb', line 115

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_multirequestObject



107
108
109
# File 'lib/kaltura/kaltura_client_base.rb', line 107

def start_multirequest()
	@is_multirequest = true
end