goncalossilva-kaltura_fu 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ module KalturaFu
2
+ module Entry
3
+ module Metadata
4
+
5
+ ##
6
+ # It is necessary to have the check for valid Kaltura MediaEntry methods available at both
7
+ # the class and instance level, so they are thrown into this module.
8
+ ##
9
+ module ClassAndInstanceMethods
10
+
11
+ ##
12
+ # Checks if a requested attribute is in fact a valid MediaEntry atrribute.
13
+ ##
14
+ def valid_entry_attribute?(request_attribute)
15
+ object_methods, media_entry_methods = Object.instance_methods , Kaltura::MediaEntry.instance_methods
16
+
17
+ #clean out all the setter methods from the media entry methods
18
+ valid_media_entry_methods = media_entry_methods.map{|m| m unless m =~/^(.*)=/}.compact!
19
+
20
+ valid_media_entry_methods -= object_methods
21
+ valid_entry_attributes.include?(request_attribute.to_sym)
22
+ end
23
+
24
+ ##
25
+ # @private
26
+ ##
27
+ def valid_entry_attributes
28
+ object_methods, media_entry_methods = Object.instance_methods , Kaltura::MediaEntry.instance_methods
29
+
30
+ #clean out all the setter methods from the media entry methods
31
+ valid_media_entry_methods = media_entry_methods.map{|m| m.to_sym unless m =~/^(.*)=/}.compact!
32
+
33
+ valid_media_entry_methods -= object_methods
34
+ end
35
+ ##
36
+ # Determines if an attribute is valid in the sense of the add method making sense. Only
37
+ # categories and tags are currently considered valid.
38
+ ##
39
+ def valid_add_attribute?(request_attribute)
40
+ case request_attribute.to_s
41
+ when /^(.*)_(categories|tags)/
42
+ return true
43
+ when /^(categories|tags)/
44
+ return true
45
+ else
46
+ return false
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,71 @@
1
+ module KalturaFu
2
+ module Entry
3
+ module Metadata
4
+ module ClassMethods
5
+
6
+ # Contains the names of the generated attribute methods.
7
+ def generated_methods #:nodoc:
8
+ @generated_methods ||= Set.new
9
+ end
10
+
11
+ def generated_methods?
12
+ !generated_methods.empty?
13
+ end
14
+
15
+ ##
16
+ # This method is called from with method_missing. It generates
17
+ # actual methods for all the valid Kaltura::Media Entry methods
18
+ # the first time a dynamic getter/setter/adder is called.
19
+ ##
20
+ def define_attribute_methods
21
+ return if generated_methods?
22
+ valid_entry_attributes.each do |name|
23
+ define_set_method(name)
24
+ define_get_method(name)
25
+ define_add_method(name) if valid_add_attribute?(name)
26
+ end
27
+ end
28
+
29
+ ##
30
+ # Defines the set method for a specific Media Entry attribute
31
+ ##
32
+ def define_set_method(attr_name)
33
+ evaluate_attribute_method( attr_name,
34
+ "def set_#{attr_name}(entry,new_value);set_attribute('#{attr_name}',entry,new_value);end",
35
+ "set_#{attr_name}"
36
+ )
37
+ end
38
+
39
+ ##
40
+ # defines a get methods
41
+ ##
42
+ def define_get_method(attr_name)
43
+ evaluate_attribute_method( attr_name,
44
+ "def get_#{attr_name}(entry);get_entry(entry).send('#{attr_name}');end",
45
+ "get_#{attr_name}"
46
+ )
47
+ end
48
+
49
+ def define_add_method(attr_name)
50
+ evaluate_attribute_method( attr_name,
51
+ "def add_#{attr_name}(entry,new_value);add_attribute('#{attr_name}',entry,new_value);end",
52
+ "add_#{attr_name}"
53
+ )
54
+ end
55
+
56
+ def evaluate_attribute_method(attr_name, method_definition, method_name=attr_name)
57
+ generated_methods << method_name
58
+
59
+ begin
60
+ class_eval(method_definition, __FILE__, __LINE__)
61
+ rescue SyntaxError => err
62
+ generated_methods.delete(attr_name)
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+
@@ -0,0 +1,28 @@
1
+ require 'kaltura_fu'
2
+ require 'kaltura_fu/view_helpers'
3
+ require 'rails'
4
+
5
+ module KalturaFu
6
+ class Railtie < Rails::Railtie
7
+ initializer 'install kaltura_fu' do
8
+ $: << File.dirname(__FILE__) + '/../lib'
9
+
10
+ ActionView::Base.send :include, KalturaFu::ViewHelpers
11
+
12
+ kaltura_yml = File.join(RAILS_ROOT,'config','kaltura.yml')
13
+
14
+ unless File.exists?(kaltura_yml)
15
+ raise RuntimeError, "Unable to find \"config/kaltura.yml\" file."
16
+ end
17
+
18
+ config_file = YAML.load_file(kaltura_yml)[Rails.env]
19
+ KalturaFu.config = config_file.symbolize_keys
20
+
21
+
22
+ unless[:partner_id,:subpartner_id,:administrator_secret].all? {|key| KalturaFu.config.key?(key)}
23
+ raise RuntimeError, "Kaltura config requires :partner_id, :subpartner_id,"+
24
+ "and :administrator_secret keys"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,182 @@
1
+ module KalturaFu
2
+
3
+ ##
4
+ # The ViewHelpers module provides extensions to Rails ActionView class that allow interactions with Kaltura on rails view layer.
5
+ #
6
+ # @author Patrick Robertson
7
+ ##
8
+ module ViewHelpers
9
+ # default UI Conf ID of the kdp player
10
+ DEFAULT_KPLAYER = '1339442'
11
+ # default embedded KDP width
12
+ PLAYER_WIDTH = '400'
13
+ # default embedded KDP height
14
+ PLAYER_HEIGHT = '330'
15
+
16
+ ##
17
+ # Convienence to include SWFObject and the required Kaltura upload embed javascripts.
18
+ ##
19
+ def include_kaltura_fu(*args)
20
+ content = javascript_include_tag('kaltura_upload')
21
+ content << "\n#{javascript_include_tag('http://ajax.googleapis.com' +
22
+ '/ajax/libs/swfobject/2.2/swfobject.js')}"
23
+ end
24
+
25
+ ##
26
+ # Returns the thumbnail of the provided Kaltura Entry.
27
+ # @param [String] entry_id Kaltura entry_id
28
+ # @param [Hash] options the options for the thumbnail parameters.
29
+ # @option options [Array] :size ([]) an array of [width,height]
30
+ # @option options [String] :second (nil) the second of the Kaltura entry that the thumbnail should be of.
31
+ #
32
+ # @return [String] Image tag of the thumbnail resource.
33
+ ##
34
+ def kaltura_thumbnail(entry_id,options={})
35
+ options[:size] ||= []
36
+ size_parameters = ""
37
+ seconds_parameter = ""
38
+
39
+ unless options[:size].empty?
40
+ size_parameters = "/width/#{options[:size].first}/height/" +
41
+ "#{options[:size].last}"
42
+ else
43
+ # if the thumbnail width and height are defined in the config,
44
+ # use it, assuming it wasn't locally overriden
45
+ if KalturaFu.config[:thumb_width] && KalturaFu.config[:thumb_height]
46
+ size_parameters = "/width/#{KalturaFu.config[:thumb_width]}/height/" +
47
+ "#{KalturaFu.config[:thumb_height]}"
48
+ end
49
+ end
50
+
51
+ unless options[:second].nil?
52
+ seconds_parameter = "/vid_sec/#{options[:second]}"
53
+ else
54
+ seconds_parameter = "/vid_sec/5"
55
+ end
56
+
57
+ image_tag("http://www.kaltura.com/p/#{KalturaFu.config[:partner_id]}" +
58
+ "/thumbnail/entry_id/#{entry_id}" +
59
+ seconds_parameter +
60
+ size_parameters)
61
+ end
62
+
63
+ ##
64
+ # Returns the code needed to embed a KDPv3 player.
65
+ #
66
+ # @param [String] entry_id Kaltura entry_id
67
+ # @param [Hash] options the embed code options.
68
+ # @option options [String] :div_id ('kplayer') The div element that the flash object will be inserted into.
69
+ # @option options [Array] :size ([]) The [width,wight] of the player.
70
+ # @option options [Boolean] :use_url (false) flag to determine whether entry_id is an entry or a URL of a flash file.
71
+ # @option options [String] :player_conf_id (KalturaFu.config(:player_conf_id)) A UI Conf ID to override the player with.
72
+ #
73
+ # @return [String] returns a string representation of the html/javascript necessary to play a Kaltura entry.
74
+ ##
75
+ def kaltura_player_embed(entry_id,options={})
76
+ player_conf_parameter = "/ui_conf_id/"
77
+ options[:div_id] ||= "kplayer"
78
+ options[:size] ||= []
79
+ options[:use_url] ||= false
80
+ width = PLAYER_WIDTH
81
+ height = PLAYER_HEIGHT
82
+ source_type = "entryId"
83
+
84
+ unless options[:size].empty?
85
+ width = options[:size].first
86
+ height = options[:size].last
87
+ end
88
+
89
+ if options[:use_url] == true
90
+ source_type = "url"
91
+ end
92
+
93
+ unless options[:player_conf_id].nil?
94
+ player_conf_parameter += "#{options[:player_conf_id]}"
95
+ else
96
+ unless KalturaFu.config[:player_conf_id].nil?
97
+ player_conf_parameter += "#{KalturaFu.config[:player_conf_id]}"
98
+ else
99
+ player_conf_parameter += "#{DEFAULT_KPLAYER}"
100
+ end
101
+ end
102
+
103
+ "<div id=\"#{options[:div_id]}\"></div>
104
+ <script type=\"text/javascript\">
105
+ var params= {
106
+ allowscriptaccess: \"always\",
107
+ allownetworking: \"all\",
108
+ allowfullscreen: \"true\",
109
+ wmode: \"opaque\"
110
+ };
111
+ var flashVars = {};
112
+ flashVars.sourceType = \"#{source_type}\";
113
+ flashVars.entryId = \"#{entry_id}\";
114
+ flashVars.emptyF = \"onKdpEmpty\";
115
+ flashVars.readyF = \"onKdpReady\";
116
+
117
+ var attributes = {
118
+ id: \"#{options[:div_id]}\",
119
+ name: \"#{options[:div_id]}\"
120
+ };
121
+
122
+ swfobject.embedSWF(\"http://www.kaltura.com/kwidget/wid/_#{KalturaFu.config[:partner_id]}" + player_conf_parameter + "\",\"#{options[:div_id]}\",\"#{width}\",\"#{height}\",\"10.0.0\",\"http://ttv.mit.edu/swfs/expressinstall.swf\",flashVars,params,attributes);
123
+ </script>"
124
+ end
125
+
126
+ ##
127
+ # Returns the html/javascript necessary for a KSU widget.
128
+ #
129
+ # @param [Hash] options
130
+ # @option options [String] :div_id ('uploader') div that the flash object will be inserted into.
131
+ ##
132
+ def kaltura_upload_embed(options={})
133
+ options[:div_id] ||="uploader"
134
+ "<div id=\"#{options[:div_id]}\"></div>
135
+ <script type=\"text/javascript\">
136
+
137
+ var params = {
138
+ allowScriptAccess: \"always\",
139
+ allowNetworking: \"all\",
140
+ wmode: \"transparent\"
141
+ };
142
+ var attributes = {
143
+ id: \"uploader\",
144
+ name: \"KUpload\"
145
+ };
146
+ var flashVars = {
147
+ uid: \"ANONYMOUS\",
148
+ partnerId: \"#{KalturaFu.config[:partner_id]}\",
149
+ subPId: \"#{KalturaFu.config[:subpartner_id]}\",
150
+ entryId: \"-1\",
151
+ ks: \"#{KalturaFu.session_key}\",
152
+ uiConfId: '1103',
153
+ jsDelegate: \"delegate\",
154
+ maxFileSize: \"999999999\",
155
+ maxTotalSize: \"999999999\"
156
+ };
157
+
158
+ swfobject.embedSWF(\"http://www.kaltura.com/kupload/ui_conf_id/1103\", \"uploader\", \"160\", \"26\", \"9.0.0\", \"expressInstall.swf\", flashVars, params,attributes);
159
+
160
+ </script>"
161
+ end
162
+
163
+ ##
164
+ # Creates a link_to tag that seeks to a certain time on a KDPv3 player.
165
+ #
166
+ # @param [String] content The text in the link tag.
167
+ # @param [Integer] seek_time The time in seconds to seek the player to.
168
+ # @param [Hash] options
169
+ #
170
+ # @option options [String] :div_id ('kplayer') The div of the KDP player.
171
+ ##
172
+ def kaltura_seek_link(content,seek_time,options={})
173
+ options[:div_id] ||= "kplayer"
174
+
175
+ options[:onclick] = "$(#{options[:div_id]}).get(0).sendNotification('doSeek',#{seek_time});window.scrollTo(0,0);return false;"
176
+ options.delete(:div_id)
177
+ link_to(content,"#", options)
178
+ end
179
+ end
180
+
181
+
182
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,22 @@
1
+ $: << File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'rubygems'
4
+ require 'kaltura-ruby'
5
+ require 'kaltura_fu'
6
+
7
+ ActionView::Base.send :include, KalturaFu::ViewHelpers
8
+
9
+ kaltura_yml = File.join(RAILS_ROOT,'config','kaltura.yml')
10
+
11
+ unless File.exists?(kaltura_yml)
12
+ raise RuntimeError, "Unable to find \"config/kaltura.yml\" file."
13
+ end
14
+
15
+
16
+ KalturaFu.config = YAML.load_file(kaltura_yml).symbolize_keys[RAILS_ENV.to_sym]
17
+
18
+
19
+ unless[:partner_id,:subpartner_id,:administrator_secret].all? {|key| KalturaFu.config.key?(key)}
20
+ raise RuntimeError, "Kaltura config requires :partner_id, :subpartner_id,"+
21
+ "and :administrator_secret keys"
22
+ end
data/spec/debug.log ADDED
@@ -0,0 +1 @@
1
+ # Logfile created on Mon Jun 14 10:07:52 -0400 2010 by /
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ class EntrySpecTester
4
+ include KalturaFu::Entry
5
+ end
6
+
7
+ describe "Kaltura Fu's Entry Class Methods" do
8
+ before(:all) do
9
+ KalturaFuTestConfiguration.setup
10
+ @tester = EntrySpecTester.new
11
+ end
12
+
13
+ it "Responds to upload" do
14
+ EntrySpecTester.should respond_to :upload
15
+ end
16
+
17
+ it "Allows a valid file to be uploaded and deleted" do
18
+ entry_id = nil
19
+ lambda {entry_id = EntrySpecTester.upload(KalturaFuTestConfiguration.video, :source=>:file)}.should_not raise_error
20
+
21
+ lambda {@tester.get_entry(entry_id)}.should_not raise_error
22
+ check = @tester.delete_entry(entry_id)
23
+ check.should be_true
24
+ end
25
+
26
+ it "Does nothing when you don't source the method as a file upload" do
27
+ entry_id = nil
28
+ lambda {entry_id = EntrySpecTester.upload("waffles",:source=>:url)}.should_not raise_error
29
+ entry_id.should be_nil
30
+ end
31
+
32
+ it "Handles valid Media Entry attributes when you provide them in the options hash" do
33
+ video_options = {}
34
+ video_options[:name] = "My fantastic movie."
35
+ video_options[:description] = "A movie of unparralled awesomeness."
36
+ video_options[:tags] = "man, this, is, awesome"
37
+ video_options[:source] = :file
38
+
39
+ entry_id = nil
40
+ lambda {entry_id = EntrySpecTester.upload(KalturaFuTestConfiguration.video,video_options)}.should_not raise_error
41
+
42
+ media_entry = nil
43
+ lambda {media_entry = @tester.get_entry(entry_id)}.should_not raise_error
44
+ media_entry.name.should == video_options[:name]
45
+ media_entry.description.should == video_options[:description]
46
+ media_entry.tags.should == video_options[:tags]
47
+
48
+ check = @tester.delete_entry(entry_id)
49
+ check.should be_true
50
+ end
51
+
52
+ it "Is totally cool with you supplying jibberish in the options too." do
53
+ video_options = {}
54
+ video_options[:name] = "My fantastic movie."
55
+ video_options[:description] = "A movie of unparralled awesomeness."
56
+ video_options[:tags] = "man, this, is, awesome"
57
+ video_options[:source] = :file
58
+ video_options[:waffles] = "WHATEVER WAFFLES"
59
+
60
+ entry_id = nil
61
+ lambda {entry_id = EntrySpecTester.upload(KalturaFuTestConfiguration.video,video_options)}.should_not raise_error
62
+
63
+ media_entry = nil
64
+ lambda {media_entry = @tester.get_entry(entry_id)}.should_not raise_error
65
+ media_entry.name.should == video_options[:name]
66
+ media_entry.description.should == video_options[:description]
67
+ media_entry.tags.should == video_options[:tags]
68
+ media_entry.should_not respond_to :waffles
69
+
70
+ check = @tester.delete_entry(entry_id)
71
+ check.should be_true
72
+ end
73
+
74
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ class FlavorSpecTester
4
+ include KalturaFu::Entry
5
+ end
6
+
7
+ class EntryUploader
8
+ include KalturaFu::Entry
9
+ end
10
+
11
+ describe "Actions specific to an entries flavors" do
12
+ before(:all) do
13
+ KalturaFuTestConfiguration.setup
14
+ end
15
+
16
+ before(:each) do
17
+ @entry_id = EntryUploader.upload(KalturaFuTestConfiguration.video,:source=>:file)
18
+ end
19
+
20
+ after(:each) do
21
+ EntryUploader.new.delete_entry(@entry_id)
22
+ end
23
+
24
+ it "should return a status of not-ready when a video uploads" do
25
+ flavor = FlavorSpecTester.new
26
+ flavor.check_status(@entry_id).should_not == Kaltura::Constants::FlavorAssetStatus::READY
27
+ end
28
+
29
+ it "should respond to original_flavor" do
30
+ flavor = FlavorSpecTester.new
31
+
32
+ flavor.should respond_to :original_flavor
33
+ end
34
+
35
+ it "should be able to get the original flavor ID without an error" do
36
+ flavor = FlavorSpecTester.new
37
+
38
+ lambda{flavor.original_flavor(@entry_id)}.should_not raise_error
39
+ end
40
+
41
+ it "should respond to original_file_extension" do
42
+ flavor = FlavorSpecTester.new
43
+
44
+ flavor.should respond_to :original_file_extension
45
+ end
46
+
47
+ it "should be able to get the original file extension without error" do
48
+ flavor = FlavorSpecTester.new
49
+
50
+ lambda{flavor.original_file_extension(@entry_id)}.should_not raise_error
51
+ end
52
+
53
+ it "should have a file extension of FLV for the test video" do
54
+ flavor = FlavorSpecTester.new
55
+
56
+ extension = nil
57
+ extension = flavor.original_file_extension(@entry_id)
58
+ extension.should == "flv"
59
+ end
60
+
61
+ it "should respond to original_download_url" do
62
+ flavor = FlavorSpecTester.new
63
+
64
+ flavor.should respond_to :original_download_url
65
+ end
66
+
67
+ it "shouldn't blow up when I call original_download_url" do
68
+ flavor = FlavorSpecTester.new
69
+
70
+ lambda {flavor.original_download_url(@entry_id)}.should_not raise_error
71
+ end
72
+
73
+ it "original_download_url should look like a reasonable URL" do
74
+ flavor = FlavorSpecTester.new
75
+
76
+ url = flavor.original_download_url(@entry_id)
77
+ test_url = "#{KalturaFu.config[:service_url]}/p/#{KalturaFu.config[:partner_id]}/sp/#{KalturaFu.config[:subpartner_id]}/serveFlavor/flavorId/#{flavor.original_flavor(@entry_id)}/name/#{flavor.original_flavor(@entry_id)}.#{flavor.original_file_extension(@entry_id)}?novar=0"
78
+ url.should == test_url
79
+ end
80
+
81
+ it "should respond to changes in service_url" do
82
+ flavor = FlavorSpecTester.new
83
+ old_service_url = KalturaFu.config[:service_url]
84
+ KalturaFu.config[:service_url] = "http://www.waffles.com"
85
+
86
+ url = flavor.original_download_url(@entry_id)
87
+ test_url = "http://www.waffles.com/p/#{KalturaFu.config[:partner_id]}/sp/#{KalturaFu.config[:subpartner_id]}/serveFlavor/flavorId/#{flavor.original_flavor(@entry_id)}/name/#{flavor.original_flavor(@entry_id)}.#{flavor.original_file_extension(@entry_id)}?novar=0"
88
+ KalturaFu.config[:service_url] = old_service_url
89
+ url.should == test_url
90
+ end
91
+ end