Class: Kaltura::KalturaClientBase

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

Direct Known Subclasses

KalturaClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ KalturaClientBase

Returns a new instance of KalturaClientBase.



13
14
15
16
17
18
19
20
21
# File 'lib/kaltura_client_base.rb', line 13

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.



9
10
11
# File 'lib/kaltura_client_base.rb', line 9

def config
  @config
end

#is_multirequestObject (readonly)

Returns the value of attribute is_multirequest.



11
12
13
# File 'lib/kaltura_client_base.rb', line 11

def is_multirequest
  @is_multirequest
end

#ksObject

Returns the value of attribute ks.



10
11
12
# File 'lib/kaltura_client_base.rb', line 10

def ks
  @ks
end

Instance Method Details

#add_param(params, name, value) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/kaltura_client_base.rb', line 129

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? KalturaObjectBase
		add_param(params, name, value.to_params)
	else
		params[name] = value
	end
end

#do_http_request(url, params) ⇒ Object



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

def do_http_request(url, params)
	url = URI.parse(url)
	req = Net::HTTP::Post.new(url.path + '?' + url.query)
	req.set_form_data(params)
	res = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
	return res
end

#do_multirequestObject



117
118
119
# File 'lib/kaltura_client_base.rb', line 117

def do_multirequest()
	return do_queue()
end

#do_queueObject



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
77
78
79
80
81
82
83
84
# File 'lib/kaltura_client_base.rb', line 35

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



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

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

#log(msg) ⇒ Object



150
151
152
153
154
# File 'lib/kaltura_client_base.rb', line 150

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

#parse_element_to_object(xml_element) ⇒ Object



105
106
# File 'lib/kaltura_client_base.rb', line 105

def parse_element_to_object(xml_element)
end

#parse_elements_to_array(xml_elements) ⇒ Object



108
109
110
111
# File 'lib/kaltura_client_base.rb', line 108

def parse_elements_to_array(xml_elements)
	xml_elements.each('item') do | element |
	end
end

#parse_to_objects(data) ⇒ Object



94
95
96
# File 'lib/kaltura_client_base.rb', line 94

def parse_to_objects(data)
	parse_xml_to_objects(data)
end

#parse_xml_to_objects(xml) ⇒ Object



98
99
100
101
102
103
# File 'lib/kaltura_client_base.rb', line 98

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

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



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kaltura_client_base.rb', line 23

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 = KalturaServiceActionCall.new(service, action, params);
	@calls_queue.push(call);
end

#signature(params) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/kaltura_client_base.rb', line 121

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



113
114
115
# File 'lib/kaltura_client_base.rb', line 113

def start_multirequest()
	@is_multirequest = true
end