acts_as_kaltura 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ Coming up :)
@@ -0,0 +1,61 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActsAsKaltura
4
+ module Annotation
5
+ extend ActiveSupport::Concern
6
+
7
+ class KalturaAnnotationAddFailure < ::StandardError; end
8
+ class KalturaAnnotationUpdateFailure < ::StandardError; end
9
+
10
+ module ClassMethods
11
+ def acts_as_kaltura_annotation options = { }
12
+ before_create :create_kaltura_annotation
13
+ after_update :update_kaltura_annotation
14
+ after_destroy :delete_kaltura_annotation
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+
20
+ def create_kaltura_annotation
21
+ if self.cuepoint_key.nil?
22
+ @kaltura_annotation = self.class.kaltura_client.cuepoint_service.
23
+ add( self.as_annotation_cuepoint )
24
+ raise KalturaAnnotationAddFailure if @kaltura_annotation.nil?
25
+
26
+ self.cuepoint_key = @kaltura_annotation.id.to_s
27
+ end
28
+ end
29
+
30
+ def update_kaltura_annotation
31
+ if self.cuepoint_key.present?
32
+ @kaltura_annotation = self.class.kaltura_client.cuepoint_service.
33
+ update(self.cuepoint_key, self.as_annotation_cuepoint )
34
+ raise KalturaAnnotationUpdateFailure if @kaltura_annotation.nil?
35
+ end
36
+ end
37
+
38
+ def delete_kaltura_annotation
39
+ if self.cuepoint_key.present?
40
+ self.class.kaltura_client.cuepoint_service.delete(self.cuepoint_key)
41
+ @kaltura_annotation = nil
42
+ end
43
+ end
44
+
45
+ def kaltura_annotation
46
+ @kaltura_annotation ||= find_kaltura_annotation
47
+ end
48
+
49
+ def kaltura_annotation=(annotation)
50
+ @kaltura_annotation = annotation
51
+ end
52
+
53
+ private
54
+ def find_kaltura_annotation
55
+ if self.cuepoint_key.present?
56
+ self.class.kaltura_client.cuepoint_service.get(self.cuepoint_key)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActsAsKaltura
4
+ module Callbacks
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ cattr_accessor :_kaltura_after_save_callbacks
9
+ @@_kaltura_after_save_callbacks = []
10
+ end
11
+
12
+ module ClassMethods
13
+ def set_kaltura_after_save_callback(callback_method)
14
+ _kaltura_after_save_callbacks << callback_method
15
+ end
16
+
17
+ def run_kaltura_after_save_callbacks(instance)
18
+ if _kaltura_after_save_callbacks.present?
19
+ _kaltura_after_save_callbacks.each { |m| instance.send m }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,73 @@
1
+ require 'yaml'
2
+ require 'active_support/concern'
3
+
4
+ module ActsAsKaltura
5
+ module Config
6
+ extend ActiveSupport::Concern
7
+
8
+ class Logger
9
+ def log(*args)
10
+ puts args
11
+ end
12
+ end
13
+
14
+ included do
15
+ cattr_accessor :_kaltura_config_file
16
+ @@_kaltura_config_file = Rails.root.join('config', 'kaltura.yml').to_s
17
+
18
+ cattr_accessor :_kaltura_client
19
+ end
20
+
21
+ module ClassMethods
22
+ def kaltura_configs(env = Rails.env.to_s)
23
+ if !defined?(@@_kaltura_configs)
24
+ @@_kaltura_configs = YAML.load(File.read(_kaltura_config_file)).with_indifferent_access
25
+ end
26
+
27
+ @@_kaltura_configs[env]
28
+ end
29
+
30
+ def kaltura_client(env = Rails.env.to_s)
31
+ if _kaltura_client.nil?
32
+ configs = kaltura_configs(env)
33
+ config = Kaltura::Configuration.new(configs[:partner_id])
34
+
35
+ # Set timeout if mentioned in configuration
36
+ if configs[:timeout].present?
37
+ config.timeout = configs[:timeout]
38
+ end
39
+
40
+ # Set debug logger if mentioned in configuration
41
+ if configs[:debug]
42
+ config.logger = ActsAsKaltura::Config::Logger.new
43
+ _debug_response
44
+ end
45
+
46
+ client = Kaltura::Client.new(config)
47
+ session = client.session_service.start(
48
+ configs[:admin_secret], '', Kaltura::Constants::SessionType::ADMIN)
49
+ client.ks = session
50
+ _kaltura_client = client
51
+ end
52
+
53
+ _kaltura_client
54
+ end
55
+
56
+ def reload_kaltura_client(env = Rails.env.to_s)
57
+ _kaltura_client = nil
58
+ kaltura_client
59
+ end
60
+
61
+ def _debug_response
62
+ if ['test', 'development'].include?(Rails.env)
63
+ Kaltura::ClientBase.class_eval do
64
+ def parse_to_objects(data)
65
+ puts data
66
+ parse_xml_to_objects(data)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActsAsKaltura
4
+ module Delegator
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ self.set_kaltura_after_save_callback :reset_kaltura_delegated_attributes
9
+ end
10
+
11
+ module ClassMethods
12
+ def delegates_kaltura_attributes(attrs)
13
+ attrs.each do |attr|
14
+ # def thumbnail_url
15
+ # _lookup_local_attribute(:"thumbnail_url") || _lookup_kaltura_attribute(:"thumbnail_url")
16
+ # end
17
+ class_eval <<-RUBY
18
+ def #{attr}
19
+ self._lookup_local_attribute(:'#{attr.to_s}') ||
20
+ self._lookup_kaltura_attribute(:'#{attr.to_s}')
21
+ end
22
+ RUBY
23
+ end
24
+ end
25
+ end
26
+
27
+ module InstanceMethods
28
+ protected
29
+ def reset_kaltura_delegated_attributes
30
+ if self.class._kaltura_options.present?
31
+ self.class._kaltura_options[:delegate].each do |attr|
32
+ self.send(:"#{attr}=", nil)
33
+ end
34
+ end
35
+ end
36
+
37
+ def _lookup_local_attribute(attr)
38
+ self.read_attribute attr
39
+ end
40
+
41
+ def _lookup_kaltura_attribute(attr)
42
+ if entry = self.kaltura_entry
43
+ value = entry.send(attr)
44
+ self.update_column(attr, value) if value
45
+ value
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,25 @@
1
+ module ActsAsKaltura
2
+ module Extension
3
+ class KalturaCuepoint < Kaltura::BaseEntry
4
+ TYPE_AD = 'adCuePoint.Ad'
5
+ TYPE_ANNOTATION = 'annotation.Annotation'
6
+ TYPE_CODE = 'codeCuePoint.Code'
7
+
8
+ STATUS_DELETED = 2
9
+ STATUS_READY = 1
10
+
11
+ dual_attr_accessor :createdAt, :cuePointType, :entryId, :forceStop, :id,
12
+ :partnerData, :partnerId, :partnerSortValue,
13
+ :startTime, :status, :systemName, :tags, :thumbOffset,
14
+ :updatedAt, :userId
15
+ end
16
+
17
+ class KalturaAnnotation < KalturaCuepoint
18
+ dual_attr_accessor :duration, :endTime, :parentId, :text, :cuepointId
19
+ end
20
+ end
21
+ end
22
+
23
+ module Kaltura
24
+ class Annotation < ActsAsKaltura::Extension::KalturaAnnotation; end
25
+ end
@@ -0,0 +1,34 @@
1
+ module ActsAsKaltura
2
+ module Extension
3
+ class KalturaCuepointFilter < Kaltura::Filter::BaseEntryFilter
4
+ attr_accessor :order_by
5
+ attr_accessor :advanced_search
6
+ attr_accessor :id_equal
7
+ attr_accessor :id_in
8
+ attr_accessor :cue_point_type_equal
9
+ attr_accessor :cue_point_type_in
10
+ attr_accessor :status_equal
11
+ attr_accessor :status_in
12
+ attr_accessor :entry_id_equal
13
+ attr_accessor :entry_id_in
14
+ attr_accessor :created_at_greater_than_or_equal
15
+ attr_accessor :created_at_less_than_or_equal
16
+ attr_accessor :updated_at_greater_than_or_equal
17
+ attr_accessor :updated_at_less_than_or_equal
18
+ attr_accessor :tags_like
19
+ attr_accessor :tags_multi_like_or
20
+ attr_accessor :tags_multi_like_and
21
+ attr_accessor :start_time_greater_than_or_equal
22
+ attr_accessor :start_time_less_than_or_equal
23
+ attr_accessor :user_id_equal
24
+ attr_accessor :user_id_in
25
+ attr_accessor :partner_sort_value_equal
26
+ attr_accessor :partner_sort_value_in
27
+ attr_accessor :partner_sort_value_greater_than_or_equal
28
+ attr_accessor :partner_sort_value_less_than_or_equal
29
+ attr_accessor :force_stop_equal
30
+ attr_accessor :system_name_equal
31
+ attr_accessor :system_name_in
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ module ActsAsKaltura
2
+ module Extension
3
+ class CuePointListResponse < Kaltura::Response::BaseResponse; end
4
+ end
5
+ end
6
+
7
+ module Kaltura::Response
8
+ CuePointListResponse = ActsAsKaltura::Extension::CuePointListResponse
9
+ end
@@ -0,0 +1,60 @@
1
+ module ActsAsKaltura
2
+ module Extension
3
+ module Service
4
+
5
+ class CuePointService < Kaltura::Service::BaseService
6
+ #
7
+ # Create new cuepoint
8
+ def add(cuepoint)
9
+ kparams = { }
10
+ client.add_param(kparams, 'cuePoint', cuepoint)
11
+ perform_request('cuepoint_cuepoint', 'add', kparams, false)
12
+ end
13
+
14
+ #
15
+ # Retrieve kaltura entry
16
+ def get(entry_id, version = -1)
17
+ kparams = { }
18
+ client.add_param(kparams, 'id', entry_id)
19
+ client.add_param(kparams, 'version', version)
20
+ perform_request('cuepoint_cuepoint', 'get', kparams, false)
21
+ end
22
+
23
+ #
24
+ # Count total available cue points for the specified filtered list
25
+ def count(filter=nil)
26
+ kparams = { }
27
+ client.add_param(kparams, 'filter', filter)
28
+ perform_request('cuepoint_cuepoint', 'count', kparams, false)
29
+ end
30
+
31
+ #
32
+ # Update an existing cuepoint object
33
+ def update(entry_id, cuepoint)
34
+ kparams = { }
35
+ client.add_param(kparams, 'id', entry_id)
36
+ client.add_param(kparams, 'cuePoint', cuepoint)
37
+ perform_request('cuepoint_cuepoint', 'update', kparams, false)
38
+ end
39
+
40
+ #
41
+ # Retrieve list of existing videos, you can filter by passing filter
42
+ # Also paginate through passing paginate object
43
+ def list(filter = nil, pager = nil)
44
+ kparams = { }
45
+ client.add_param(kparams, 'filter', filter)
46
+ client.add_param(kparams, 'pager', pager)
47
+ perform_request('cuepoint_cuepoint', 'list', kparams, false)
48
+ end
49
+
50
+ #
51
+ # Delete the specific cuepoint
52
+ def delete(entry_id)
53
+ kparams = { }
54
+ client.add_param(kparams, 'id', entry_id)
55
+ perform_request('cuepoint_cuepoint', 'delete', kparams, false)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_support/concern'
2
+ require 'acts_as_kaltura/extension/entry'
3
+ require 'acts_as_kaltura/extension/response'
4
+ require 'acts_as_kaltura/extension/filter'
5
+ require 'acts_as_kaltura/extension/service'
6
+
7
+ module ActsAsKaltura
8
+ module Extension
9
+ extend ActiveSupport::Concern
10
+
11
+ module InstanceMethods
12
+ def cuepoint_service
13
+ @cuepoint_service ||= ActsAsKaltura::Extension::Service::CuePointService.new(self)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActsAsKaltura
4
+ module TwoWayAttrAccessor
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def dual_attr_accessor(*var_names)
9
+ var_names.each do |m|
10
+ m = m.to_s
11
+ setter_proc = lambda do |value|
12
+ self.instance_variable_set :"@#{m.underscore}", value
13
+ end
14
+ self.send :define_method, :"#{m}=", setter_proc
15
+ self.send :define_method, :"#{m.underscore}=", setter_proc
16
+
17
+ getter_proc = lambda { self.instance_variable_get :"@#{m.underscore}" }
18
+ self.send :define_method, m.to_sym, getter_proc
19
+ self.send :define_method, :"#{m.underscore}", getter_proc
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ class Object
27
+ include ActsAsKaltura::TwoWayAttrAccessor
28
+ end
@@ -0,0 +1,160 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActsAsKaltura
4
+ module Video
5
+ extend ActiveSupport::Concern
6
+
7
+ class NoUploadedFile < ::StandardError; end
8
+ class NoUploadedTokenFound < ::StandardError; end
9
+ class KalturaVideoAddFailure < ::StandardError; end
10
+
11
+ included do
12
+ class_attribute :_kaltura_options, :instance_writer => false
13
+ end
14
+
15
+ module ClassMethods
16
+ def acts_as_kaltura_video(options = {})
17
+ # Set instance accessors
18
+ attr_accessor :video_file, :uploaded_video_token
19
+ self._kaltura_options = options
20
+
21
+ # Delegate kaltura attributes if :delegate option provided
22
+ if options[:delegate].present?
23
+ delegates_kaltura_attributes options[:delegate]
24
+ end
25
+
26
+ # Set validators
27
+ validates :video_file, :presence => {:on => :create}
28
+
29
+ # Set filters
30
+ before_save :process_uploaded_video_file
31
+ before_create :create_kaltura_video_entry
32
+ before_update :update_kaltura_video_entry
33
+ after_destroy :destroy_kaltura_video_entry
34
+ end
35
+ end
36
+
37
+ module InstanceMethods
38
+
39
+ #
40
+ # Push user uploaded video file to kaltura server
41
+ # If successful set upload token with the object
42
+ #
43
+ def process_uploaded_video_file
44
+ if @video_file.present?
45
+ video_file_stream = if @video_file.respond_to?(:path, true)
46
+ File.open(@video_file.path)
47
+ else
48
+ @video_file
49
+ end
50
+ @uploaded_video_token = self.class.kaltura_client.
51
+ media_service.upload( video_file_stream )
52
+ raise ActsAsKaltura::Video::NoUploadedTokenFound if @uploaded_video_token.nil?
53
+ end
54
+ end
55
+
56
+ #
57
+ # Create kaltura video entry based on current video data
58
+ # if successful set kaltura id
59
+ #
60
+ def create_kaltura_video_entry
61
+ if @video_file.present?
62
+ # There must have uploaded token otherwise this should be invoked
63
+ raise NoUploadedTokenFound if @uploaded_video_token.nil?
64
+
65
+ # Covert host model as kaltura entry
66
+ entry = as_kaltura_entry
67
+
68
+ # Request kaltura service to store video entry for the
69
+ # uploaded video file
70
+ stored_entry = add_kaltuar_video_file(entry)
71
+
72
+ # Nil return should throw exception
73
+ raise KalturaVideoAddFailure if stored_entry.nil?
74
+
75
+ # Set kaltura_key from stored kaltura entry
76
+ self.kaltura_key = stored_entry.id.to_s
77
+ self.kaltura_syncd_at = Time.now
78
+
79
+ self.class.run_kaltura_after_save_callbacks self
80
+ end
81
+ end
82
+
83
+ #
84
+ # Update kaltura existing video entry
85
+ # If successful update kaltura_syncd_at
86
+ #
87
+ def update_kaltura_video_entry
88
+ if kaltura_key.present?
89
+ entry = as_kaltura_entry
90
+ update_kaltura_entry(entry)
91
+
92
+ if @video_file.nil?
93
+ entry.id = kaltura_key
94
+ add_kaltuar_video_file(entry)
95
+ end
96
+
97
+ self.class.run_kaltura_after_save_callbacks self
98
+ end
99
+ end
100
+
101
+ def kaltura_entry
102
+ find_kaltura_entry if @kaltura_entry.nil?
103
+ @kaltura_entry
104
+ end
105
+
106
+ def kaltura_entry=(entry)
107
+ @kaltura_entry = entry
108
+ end
109
+
110
+ def destroy_kaltura_video_entry
111
+ if kaltura_key
112
+ destroy_kaltura_entry
113
+ end
114
+ end
115
+
116
+ private
117
+ def add_kaltuar_video_file(entry)
118
+ if @video_file && @uploaded_video_token
119
+ self.class.kaltura_client.media_service.
120
+ add_from_uploaded_file(entry, @uploaded_video_token)
121
+ end
122
+ end
123
+
124
+ def update_kaltura_entry(entry)
125
+ if kaltura_key.present?
126
+ self.class.kaltura_client.media_service.update(kaltura_key, entry)
127
+ self.kaltura_syncd_at = Time.now
128
+ end
129
+ end
130
+
131
+ def find_kaltura_entry
132
+ if kaltura_key
133
+ if @kaltura_entry.nil?
134
+ @kaltura_entry = self.class.kaltura_client.media_service.get(kaltura_key)
135
+ end
136
+
137
+ @kaltura_entry
138
+ end
139
+ end
140
+
141
+ def destroy_kaltura_entry
142
+ if kaltura_key
143
+ self.class.kaltura_client.media_service.delete(kaltura_key)
144
+ @kaltura_entry = nil
145
+ end
146
+ rescue
147
+
148
+ end
149
+
150
+ def as_kaltura_entry
151
+ Kaltura::MediaEntry.new.tap do |entry|
152
+ entry.name = title
153
+ entry.description = description
154
+ entry.tags = tags.map &:name
155
+ entry.media_type = Kaltura::Constants::Media::Type::VIDEO
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,27 @@
1
+ require 'acts_as_kaltura/two_way_attr_accessor'
2
+ require 'acts_as_kaltura/config'
3
+ require 'acts_as_kaltura/callbacks'
4
+ require 'acts_as_kaltura/delegator'
5
+ require 'acts_as_kaltura/extension'
6
+ require 'acts_as_kaltura/video'
7
+ require 'acts_as_kaltura/annotation'
8
+
9
+ module ActsAsKaltura
10
+ VERSION = '1.0.0'
11
+ end
12
+
13
+ module ActiveRecord
14
+ class Base
15
+ include ActsAsKaltura::Config
16
+ include ActsAsKaltura::Callbacks
17
+ include ActsAsKaltura::Delegator
18
+ include ActsAsKaltura::Video
19
+ include ActsAsKaltura::Annotation
20
+ end
21
+ end
22
+
23
+ module Kaltura
24
+ class Client
25
+ include ActsAsKaltura::Extension
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_kaltura
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - nhm tanveer hossain khan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-02-01 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jeweler
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: activerecord
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: velir_kaltura-ruby
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ description:
60
+ email: hasan@somewherein.net
61
+ executables: []
62
+
63
+ extensions: []
64
+
65
+ extra_rdoc_files:
66
+ - README.md
67
+ files:
68
+ - lib/acts_as_kaltura.rb
69
+ - lib/acts_as_kaltura/annotation.rb
70
+ - lib/acts_as_kaltura/callbacks.rb
71
+ - lib/acts_as_kaltura/config.rb
72
+ - lib/acts_as_kaltura/delegator.rb
73
+ - lib/acts_as_kaltura/extension.rb
74
+ - lib/acts_as_kaltura/extension/entry.rb
75
+ - lib/acts_as_kaltura/extension/filter.rb
76
+ - lib/acts_as_kaltura/extension/response.rb
77
+ - lib/acts_as_kaltura/extension/service.rb
78
+ - lib/acts_as_kaltura/two_way_attr_accessor.rb
79
+ - lib/acts_as_kaltura/video.rb
80
+ - README.md
81
+ homepage:
82
+ licenses: []
83
+
84
+ post_install_message:
85
+ rdoc_options: []
86
+
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.10
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: An extension for supporting acts_as_kaltura_video and acts_as_kaltura_annotation (which automatically maintains kaltura video and cuepoint)
108
+ test_files: []
109
+