velir_kaltura-ruby 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/.document +5 -0
  2. data/.gitignore +7 -0
  3. data/LICENSE +661 -0
  4. data/README.rdoc +45 -0
  5. data/Rakefile +60 -0
  6. data/VERSION +1 -0
  7. data/VERSION.yml +5 -0
  8. data/lib/kaltura/constants/access_control_order_by.rb +8 -0
  9. data/lib/kaltura/constants/audio_codec.rb +11 -0
  10. data/lib/kaltura/constants/base.rb +34 -0
  11. data/lib/kaltura/constants/batch_job.rb +62 -0
  12. data/lib/kaltura/constants/bit_rate_mode.rb +8 -0
  13. data/lib/kaltura/constants/category_order_by.rb +12 -0
  14. data/lib/kaltura/constants/commercial_use_type.rb +8 -0
  15. data/lib/kaltura/constants/container_format.rb +15 -0
  16. data/lib/kaltura/constants/control_panel_command.rb +34 -0
  17. data/lib/kaltura/constants/conversion_profile_order_by.rb +8 -0
  18. data/lib/kaltura/constants/country_restriction_type.rb +8 -0
  19. data/lib/kaltura/constants/data_entry_order_by.rb +14 -0
  20. data/lib/kaltura/constants/directory_restriction_type.rb +8 -0
  21. data/lib/kaltura/constants/document.rb +21 -0
  22. data/lib/kaltura/constants/duration_type.rb +10 -0
  23. data/lib/kaltura/constants/editor_type.rb +8 -0
  24. data/lib/kaltura/constants/email_ingestion_profile_status.rb +8 -0
  25. data/lib/kaltura/constants/entry.rb +35 -0
  26. data/lib/kaltura/constants/file_sync.rb +39 -0
  27. data/lib/kaltura/constants/flavor_asset_status.rb +12 -0
  28. data/lib/kaltura/constants/gender.rb +9 -0
  29. data/lib/kaltura/constants/google_syndication_feed.rb +21 -0
  30. data/lib/kaltura/constants/i_tunes_syndication_feed/categories.rb +75 -0
  31. data/lib/kaltura/constants/i_tunes_syndication_feed.rb +24 -0
  32. data/lib/kaltura/constants/license_type.rb +23 -0
  33. data/lib/kaltura/constants/live_stream_admin_entry_order_by.rb +24 -0
  34. data/lib/kaltura/constants/mail_job_order_by.rb +10 -0
  35. data/lib/kaltura/constants/media.rb +36 -0
  36. data/lib/kaltura/constants/mix_entry_order_by.rb +22 -0
  37. data/lib/kaltura/constants/moderation.rb +22 -0
  38. data/lib/kaltura/constants/notification.rb +24 -0
  39. data/lib/kaltura/constants/nullable_boolean.rb +9 -0
  40. data/lib/kaltura/constants/partner.rb +39 -0
  41. data/lib/kaltura/constants/playable_entry_order_by.rb +22 -0
  42. data/lib/kaltura/constants/playlist.rb +22 -0
  43. data/lib/kaltura/constants/report_type.rb +14 -0
  44. data/lib/kaltura/constants/search_provider_type.rb +22 -0
  45. data/lib/kaltura/constants/session_type.rb +8 -0
  46. data/lib/kaltura/constants/site_restriction_type.rb +8 -0
  47. data/lib/kaltura/constants/source_type.rb +11 -0
  48. data/lib/kaltura/constants/stats/kmc_event_type.rb +71 -0
  49. data/lib/kaltura/constants/stats.rb +36 -0
  50. data/lib/kaltura/constants/syndication_feed.rb +17 -0
  51. data/lib/kaltura/constants/system_user.rb +17 -0
  52. data/lib/kaltura/constants/tube_mogul_syndication_feed.rb +34 -0
  53. data/lib/kaltura/constants/ui_conf.rb +26 -0
  54. data/lib/kaltura/constants/upload_error_code.rb +9 -0
  55. data/lib/kaltura/constants/user.rb +16 -0
  56. data/lib/kaltura/constants/video_codec.rb +16 -0
  57. data/lib/kaltura/constants/widget.rb +15 -0
  58. data/lib/kaltura/constants/yahoo_syndication_feed.rb +44 -0
  59. data/lib/kaltura/constants.rb +64 -0
  60. data/lib/kaltura/kaltura_client.rb +4750 -0
  61. data/lib/kaltura/kaltura_client_base.rb +286 -0
  62. data/lib/kaltura.rb +16 -0
  63. data/test/kaltura-ruby_test.rb +7 -0
  64. data/test/test.rb +23 -0
  65. data/test/test_2.rb +26 -0
  66. data/test/test_error.rb +14 -0
  67. data/test/test_helper.rb +10 -0
  68. data/velir_kaltura-ruby.gemspec +123 -0
  69. metadata +196 -0
@@ -0,0 +1,286 @@
1
+ module Kaltura
2
+ class KalturaClientBase
3
+ attr_accessor :config
4
+ attr_accessor :ks
5
+ attr_reader :is_multirequest
6
+
7
+ def initialize(config)
8
+ @should_log = false
9
+ @config = config
10
+ @calls_queue = []
11
+
12
+ if @config.logger != nil
13
+ @should_log = true
14
+ end
15
+ end
16
+
17
+ def queue_service_action_call(service, action, params = {})
18
+ # in start session partner id is optional (default -1). if partner id was not set, use the one in the config
19
+ if (!params.key?('partnerId') || params['partnerId'] == -1)
20
+ params['partnerId'] = config.partner_id;
21
+ end
22
+
23
+ add_param(params, 'ks', @ks);
24
+
25
+ call = KalturaServiceActionCall.new(service, action, params);
26
+ @calls_queue.push(call);
27
+ end
28
+
29
+ def do_queue()
30
+ start_time = Time.now
31
+
32
+ if @calls_queue.length == 0
33
+ @is_multirequest = false
34
+ return nil
35
+ end
36
+
37
+ log('service url: [' + @config.service_url + ']')
38
+ # append the basic params
39
+ params = {}
40
+ add_param(params, "format", @config.format)
41
+ add_param(params, "clientTag", @config.client_tag)
42
+
43
+ url = @config.service_url+"/api_v3/index.php?service="
44
+ if (@is_multirequest)
45
+ url += "multirequest"
46
+ i = 1
47
+ @calls_queue.each_value do |call|
48
+ call_params = call.get_params_for_multirequest(i.next)
49
+ params.merge!(call_params)
50
+ end
51
+ else
52
+ call = @calls_queue[0]
53
+ url += call.service + "&action=" + call.action
54
+ params.merge!(call.params)
55
+ end
56
+
57
+ # reset
58
+ @calls_queue = []
59
+ @is_multirequest = false
60
+
61
+ signature = signature(params)
62
+ add_param(params, "kalsig", signature)
63
+
64
+ log("url: " + url)
65
+ log("params: " + params.to_yaml)
66
+ result = do_http_request(url, params)
67
+
68
+ result_object = parse_to_objects(result.body)
69
+
70
+ log("result (object yaml dump): " + result_object.to_yaml)
71
+
72
+ end_time = Time.now
73
+ log("execution time for [#{url}]: [#{end_time - start_time}]")
74
+
75
+ return result_object
76
+ end
77
+
78
+ def do_http_request(url, params)
79
+ res = RestClient.post url , params
80
+ return res
81
+ end
82
+
83
+ def parse_to_objects(data)
84
+ parse_xml_to_objects(data)
85
+ end
86
+
87
+ def parse_xml_to_objects(xml)
88
+ doc = REXML::Document.new(xml)
89
+ raise_exception_if_error(doc)
90
+ doc.elements.each('xml/result') do | element |
91
+ return KalturaClassFactory.object_from_xml(element)
92
+ end
93
+ end
94
+
95
+ def raise_exception_if_error(doc)
96
+ if is_error(doc)
97
+ code = doc.elements["xml/result/error/code"].text
98
+ message = doc.elements["xml/result/error/message"].text
99
+ raise KalturaAPIError.new(code,message)
100
+ end
101
+ end
102
+
103
+ def is_error(doc)
104
+ doc.elements["xml/result/error/message"] && doc.elements["xml/result/error/code"]
105
+ end
106
+
107
+ def start_multirequest()
108
+ @is_multirequest = true
109
+ end
110
+
111
+ def do_multirequest()
112
+ return do_queue()
113
+ end
114
+
115
+ def signature(params)
116
+ str = params.keys.map {|key| key.to_s }.sort.map {|key|
117
+ "#{escape(key)}#{escape(params[key])}"
118
+ }.join("")
119
+
120
+ Digest::MD5.hexdigest(str)
121
+ end
122
+
123
+ def add_param(params, name, value)
124
+ if value == nil
125
+ return
126
+ elsif value.is_a? Hash
127
+ value.each do |sub_name, sub_value|
128
+ add_param(params, "#{name}:#{sub_name}", sub_value);
129
+ end
130
+ elsif value.is_a? KalturaObjectBase
131
+ add_param(params, name, value.to_params)
132
+ else
133
+ params[name] = value
134
+ end
135
+ end
136
+
137
+ # Escapes a query parameter. Stolen from RFuzz
138
+ def escape(s)
139
+ s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
140
+ '%' + $1.unpack('H2'*$1.size).join('%').upcase
141
+ }.tr(' ', '+')
142
+ end
143
+
144
+ def log(msg)
145
+ if @should_log
146
+ config.logger.log(msg)
147
+ end
148
+ end
149
+ end
150
+
151
+ class KalturaServiceActionCall
152
+ attr_accessor :service
153
+ attr_accessor :action
154
+ attr_accessor :params
155
+
156
+ def initialize(service, action, params = array())
157
+ @service = service
158
+ @action = action
159
+ @params = parse_params(params)
160
+ end
161
+
162
+ def parse_params(params)
163
+ new_params = {}
164
+ params.each do |key, val|
165
+ if val.kind_of? Hash
166
+ new_params[key] = parse_params(val)
167
+ else
168
+ new_params[key] = val
169
+ end
170
+ end
171
+ return new_params
172
+ end
173
+
174
+ def get_params_for_multirequest(multirequest_index)
175
+ multirequest_params = {}
176
+ multirequest_params[multirequest_index+":service"] = @service
177
+ multirequest_params[multirequest_index+":action"] = @action
178
+ @params.each do |key|
179
+ multirequest_params[multirequest_index+":"+key] = @params[key]
180
+ end
181
+ return multirequest_params
182
+ end
183
+ end
184
+
185
+ class KalturaObjectBase
186
+ attr_accessor :object_type
187
+
188
+ def to_params
189
+ params = {};
190
+ params["objectType"] = self.class.name.split('::').last
191
+ instance_variables.each do |var|
192
+ value = instance_variable_get(var)
193
+ var = var.sub('@', '')
194
+ kvar = camelcase(var)
195
+ if (value != nil)
196
+ if (value.is_a? KalturaObjectBase)
197
+ params[kvar] = value.to_params;
198
+ else
199
+ params[kvar] = value;
200
+ end
201
+ end
202
+ end
203
+ return params;
204
+ end
205
+
206
+ def to_b(val)
207
+ return [true, 'true', 1, '1'].include?(val.is_a?(String) ? val.downcase : val)
208
+ end
209
+
210
+ def camelcase(val)
211
+ val = val.split('_').map { |e| e.capitalize }.join()
212
+ val[0,1].downcase + val[1,val.length]
213
+ end
214
+ end
215
+
216
+ class KalturaServiceBase
217
+ attr_accessor :client
218
+
219
+ def initialize(client)
220
+ @client = client
221
+ end
222
+ end
223
+
224
+ class KalturaConfiguration
225
+ attr_accessor :logger
226
+ attr_accessor :service_url
227
+ attr_accessor :format
228
+ attr_accessor :client_tag
229
+ attr_accessor :timeout
230
+ attr_accessor :partner_id
231
+
232
+ def initialize(partner_id = -1)
233
+ @service_url = "http://www.kaltura.com"
234
+ @format = 2 # xml
235
+ @client_tag = "ruby"
236
+ @timeout = 10
237
+ @partner_id = partner_id
238
+ end
239
+
240
+ def service_url=(url)
241
+ @service_url = url.chomp('/')
242
+ end
243
+ end
244
+
245
+ class KalturaClassFactory
246
+ def self.object_from_xml(xml_element)
247
+ instance = nil
248
+ if xml_element.elements.size > 0
249
+ if xml_element.elements[1].name == 'item' # array
250
+ instance = []
251
+ xml_element.elements.each('item') do | element |
252
+ instance.push(KalturaClassFactory.object_from_xml(element))
253
+ end
254
+ else # object
255
+ object_type_element = xml_element.get_text('objectType')
256
+ if (object_type_element != nil)
257
+ object_class = xml_element.get_text('objectType').value
258
+ instance = Object.const_get(object_class).new
259
+ xml_element.elements.each do | element |
260
+ value = KalturaClassFactory.object_from_xml(element)
261
+ instance.send(self.underscore(element.name) + "=", value);
262
+ end
263
+ end
264
+ end
265
+ else # simple type
266
+ return xml_element.text
267
+ end
268
+
269
+ return instance;
270
+ end
271
+
272
+ def self.underscore(val)
273
+ val.gsub(/(.)([A-Z])/,'\1_\2').downcase
274
+ end
275
+ end
276
+
277
+ class KalturaAPIError < RuntimeError
278
+ attr_reader :code
279
+ attr_reader :message
280
+
281
+ def initialize(code,message)
282
+ @code = code
283
+ @message = message
284
+ end
285
+ end
286
+ end
data/lib/kaltura.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'kaltura/kaltura_client_base'
2
+ require 'kaltura/kaltura_client'
3
+ require 'kaltura/constants'
4
+ require 'yaml'
5
+ require 'rubygems'
6
+ require 'json'
7
+ require 'rest_client'
8
+ require 'net/http'
9
+ require 'digest/md5'
10
+ require 'rexml/document'
11
+
12
+ #include Kaltura
13
+
14
+ module Kaltura
15
+
16
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class KalturaRubyTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
data/test/test.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "kaltura_client.rb"
2
+
3
+ include Kaltura
4
+ conf = KalturaConfiguration.new(250, 25000)
5
+ user = KalturaSessionUser.new(2)
6
+ cl = KalturaClient.new(conf)
7
+ secert = 'dae1be648b8a86d25adafdac2d32e8c3'
8
+ cl.start(user, secert)
9
+
10
+ kshow = KalturaKShow.new()
11
+ kshow.name = "test ruby"
12
+ kshow.description = "desc ruby"
13
+
14
+ result = cl.addKShow(user, kshow, 1)
15
+
16
+ print (result['result']['kshow']['name'])
17
+
18
+ print("\n")
19
+ print("\n")
20
+
21
+ result = cl.search(user, KalturaEntryMediaType::VIDEO, KalturaEntryMediaSource::MYSPACE, "funny");
22
+
23
+ print (result['result'])
data/test/test_2.rb ADDED
@@ -0,0 +1,26 @@
1
+ require "ruby_client.rb"
2
+
3
+ include Kaltura
4
+
5
+ config = KalturaConfiguration.new(450)
6
+ #config.logger = MyLog.new
7
+ #config.service_url = 'http://www.kaltura.com/'
8
+ client = KalturaClient.new(config)
9
+ secret = ''
10
+ ks = client.session_service.start(secret, '', KalturaSessionType::ADMIN)
11
+ puts ks
12
+ client.ks = ks
13
+ filter = KalturaFilter.new
14
+ pager = KalturaFilterPager.new
15
+ pager.page_size = 1
16
+ pager.page_index = 2
17
+ #result = client.mixing_service.list(filter, pager);
18
+ media_entry = KalturaMediaEntry.new
19
+ media_entry.name = "Testing Ruby Client"
20
+ media_entry.media_type = KalturaMediaType::VIDEO
21
+ url = 'http://kaldev.kaltura.com/content/zbale/roman/kaltura_logo_animated_black_1.flv';
22
+ media_entry = client.media_service.add_from_url(media_entry, url);
23
+ puts media_entry.to_yaml
24
+
25
+ #puts result.to_yaml
26
+ #puts result.objects[0].has_real_thumbnail
@@ -0,0 +1,14 @@
1
+ require "kaltura_client.rb"
2
+ include Kaltura
3
+
4
+ config = KalturaConfiguration.new(1)
5
+ client = KalturaClient.new(config)
6
+ secret = 'wront_secret'
7
+
8
+ begin
9
+ ks = client.session_service.start(secret, '', KalturaSessionType::ADMIN)
10
+ puts ks
11
+ rescue KalturaAPIError => e
12
+ puts 'error code: ' + e.code
13
+ puts 'error message: ' + e.message
14
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'kaltura-ruby'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,123 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{velir_kaltura-ruby}
8
+ s.version = "0.2.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Patrick Robertson"]
12
+ s.date = %q{2010-07-12}
13
+ s.email = %q{patrick.robertson@velir.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "VERSION.yml",
26
+ "lib/kaltura.rb",
27
+ "lib/kaltura/constants.rb",
28
+ "lib/kaltura/constants/access_control_order_by.rb",
29
+ "lib/kaltura/constants/audio_codec.rb",
30
+ "lib/kaltura/constants/base.rb",
31
+ "lib/kaltura/constants/batch_job.rb",
32
+ "lib/kaltura/constants/bit_rate_mode.rb",
33
+ "lib/kaltura/constants/category_order_by.rb",
34
+ "lib/kaltura/constants/commercial_use_type.rb",
35
+ "lib/kaltura/constants/container_format.rb",
36
+ "lib/kaltura/constants/control_panel_command.rb",
37
+ "lib/kaltura/constants/conversion_profile_order_by.rb",
38
+ "lib/kaltura/constants/country_restriction_type.rb",
39
+ "lib/kaltura/constants/data_entry_order_by.rb",
40
+ "lib/kaltura/constants/directory_restriction_type.rb",
41
+ "lib/kaltura/constants/document.rb",
42
+ "lib/kaltura/constants/duration_type.rb",
43
+ "lib/kaltura/constants/editor_type.rb",
44
+ "lib/kaltura/constants/email_ingestion_profile_status.rb",
45
+ "lib/kaltura/constants/entry.rb",
46
+ "lib/kaltura/constants/file_sync.rb",
47
+ "lib/kaltura/constants/flavor_asset_status.rb",
48
+ "lib/kaltura/constants/gender.rb",
49
+ "lib/kaltura/constants/google_syndication_feed.rb",
50
+ "lib/kaltura/constants/i_tunes_syndication_feed.rb",
51
+ "lib/kaltura/constants/i_tunes_syndication_feed/categories.rb",
52
+ "lib/kaltura/constants/license_type.rb",
53
+ "lib/kaltura/constants/live_stream_admin_entry_order_by.rb",
54
+ "lib/kaltura/constants/mail_job_order_by.rb",
55
+ "lib/kaltura/constants/media.rb",
56
+ "lib/kaltura/constants/mix_entry_order_by.rb",
57
+ "lib/kaltura/constants/moderation.rb",
58
+ "lib/kaltura/constants/notification.rb",
59
+ "lib/kaltura/constants/nullable_boolean.rb",
60
+ "lib/kaltura/constants/partner.rb",
61
+ "lib/kaltura/constants/playable_entry_order_by.rb",
62
+ "lib/kaltura/constants/playlist.rb",
63
+ "lib/kaltura/constants/report_type.rb",
64
+ "lib/kaltura/constants/search_provider_type.rb",
65
+ "lib/kaltura/constants/session_type.rb",
66
+ "lib/kaltura/constants/site_restriction_type.rb",
67
+ "lib/kaltura/constants/source_type.rb",
68
+ "lib/kaltura/constants/stats.rb",
69
+ "lib/kaltura/constants/stats/kmc_event_type.rb",
70
+ "lib/kaltura/constants/syndication_feed.rb",
71
+ "lib/kaltura/constants/system_user.rb",
72
+ "lib/kaltura/constants/tube_mogul_syndication_feed.rb",
73
+ "lib/kaltura/constants/ui_conf.rb",
74
+ "lib/kaltura/constants/upload_error_code.rb",
75
+ "lib/kaltura/constants/user.rb",
76
+ "lib/kaltura/constants/video_codec.rb",
77
+ "lib/kaltura/constants/widget.rb",
78
+ "lib/kaltura/constants/yahoo_syndication_feed.rb",
79
+ "lib/kaltura/kaltura_client.rb",
80
+ "lib/kaltura/kaltura_client_base.rb",
81
+ "test/kaltura-ruby_test.rb",
82
+ "test/test.rb",
83
+ "test/test_2.rb",
84
+ "test/test_error.rb",
85
+ "test/test_helper.rb",
86
+ "velir_kaltura-ruby.gemspec"
87
+ ]
88
+ s.homepage = %q{http://github.com/Velir/kaltura-ruby}
89
+ s.rdoc_options = ["--charset=UTF-8"]
90
+ s.require_paths = ["lib"]
91
+ s.rubygems_version = %q{1.3.7}
92
+ s.summary = %q{Ruby gem for accessing the Kaltura API}
93
+ s.test_files = [
94
+ "test/kaltura-ruby_test.rb",
95
+ "test/test.rb",
96
+ "test/test_2.rb",
97
+ "test/test_error.rb",
98
+ "test/test_helper.rb"
99
+ ]
100
+
101
+ if s.respond_to? :specification_version then
102
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
103
+ s.specification_version = 3
104
+
105
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
106
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
107
+ s.add_runtime_dependency(%q<activeresource>, [">= 0"])
108
+ s.add_runtime_dependency(%q<json>, [">= 0"])
109
+ s.add_runtime_dependency(%q<rest-client>, [">= 1.5.1"])
110
+ else
111
+ s.add_dependency(%q<activesupport>, [">= 0"])
112
+ s.add_dependency(%q<activeresource>, [">= 0"])
113
+ s.add_dependency(%q<json>, [">= 0"])
114
+ s.add_dependency(%q<rest-client>, [">= 1.5.1"])
115
+ end
116
+ else
117
+ s.add_dependency(%q<activesupport>, [">= 0"])
118
+ s.add_dependency(%q<activeresource>, [">= 0"])
119
+ s.add_dependency(%q<json>, [">= 0"])
120
+ s.add_dependency(%q<rest-client>, [">= 1.5.1"])
121
+ end
122
+ end
123
+