diff --git a/Gemfile.lock b/Gemfile.lock index f2801a8..00ac642 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,108 +1,107 @@ PATH remote: . specs: - zencodable (0.1.6) + zencodable (0.2.1) aws-s3 - rails (~> 3.1.0) + rails typhoeus zencoder GEM remote: http://rubygems.org/ specs: - actionmailer (3.1.3) - actionpack (= 3.1.3) - mail (~> 2.3.0) - actionpack (3.1.3) - activemodel (= 3.1.3) - activesupport (= 3.1.3) + actionmailer (3.2.6) + actionpack (= 3.2.6) + mail (~> 2.4.4) + actionpack (3.2.6) + activemodel (= 3.2.6) + activesupport (= 3.2.6) builder (~> 3.0.0) erubis (~> 2.7.0) - i18n (~> 0.6) - rack (~> 1.3.5) - rack-cache (~> 1.1) - rack-mount (~> 0.8.2) + journey (~> 1.0.1) + rack (~> 1.4.0) + rack-cache (~> 1.2) rack-test (~> 0.6.1) - sprockets (~> 2.0.3) - activemodel (3.1.3) - activesupport (= 3.1.3) + sprockets (~> 2.1.3) + activemodel (3.2.6) + activesupport (= 3.2.6) builder (~> 3.0.0) - i18n (~> 0.6) - activerecord (3.1.3) - activemodel (= 3.1.3) - activesupport (= 3.1.3) - arel (~> 2.2.1) + activerecord (3.2.6) + activemodel (= 3.2.6) + activesupport (= 3.2.6) + arel (~> 3.0.2) tzinfo (~> 0.3.29) - activeresource (3.1.3) - activemodel (= 3.1.3) - activesupport (= 3.1.3) - activesupport (3.1.3) + activeresource (3.2.6) + activemodel (= 3.2.6) + activesupport (= 3.2.6) + activesupport (3.2.6) + i18n (~> 0.6) multi_json (~> 1.0) - arel (2.2.1) - aws-s3 (0.6.2) + arel (3.0.2) + aws-s3 (0.6.3) builder mime-types xml-simple builder (3.0.0) erubis (2.7.0) factory_girl (2.1.0) + ffi (1.0.11) hike (1.2.1) i18n (0.6.0) - json (1.6.3) - mail (2.3.0) + journey (1.0.4) + json (1.7.3) + mail (2.4.4) i18n (>= 0.4.0) mime-types (~> 1.16) treetop (~> 1.4.8) metaclass (0.0.1) - mime-types (1.17.2) + mime-types (1.18) mocha (0.10.0) metaclass (~> 0.0.1) - multi_json (1.0.4) + multi_json (1.3.6) polyglot (0.3.3) - rack (1.3.5) - rack-cache (1.1) + rack (1.4.1) + rack-cache (1.2) rack (>= 0.4) - rack-mount (0.8.3) - rack (>= 1.0.0) rack-ssl (1.3.2) rack rack-test (0.6.1) rack (>= 1.0) - rails (3.1.3) - actionmailer (= 3.1.3) - actionpack (= 3.1.3) - activerecord (= 3.1.3) - activeresource (= 3.1.3) - activesupport (= 3.1.3) + rails (3.2.6) + actionmailer (= 3.2.6) + actionpack (= 3.2.6) + activerecord (= 3.2.6) + activeresource (= 3.2.6) + activesupport (= 3.2.6) bundler (~> 1.0) - railties (= 3.1.3) - railties (3.1.3) - actionpack (= 3.1.3) - activesupport (= 3.1.3) + railties (= 3.2.6) + railties (3.2.6) + actionpack (= 3.2.6) + activesupport (= 3.2.6) rack-ssl (~> 1.3.2) rake (>= 0.8.7) rdoc (~> 3.4) - thor (~> 0.14.6) + thor (>= 0.14.6, < 2.0) rake (0.9.2.2) - rdoc (3.11) + rdoc (3.12) json (~> 1.4) - sprockets (2.0.3) + sprockets (2.1.3) hike (~> 1.2) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - sqlite3 (1.3.4) - thor (0.14.6) + sqlite3 (1.3.6) + thor (0.15.3) tilt (1.3.3) treetop (1.4.10) polyglot polyglot (>= 0.3.1) - typhoeus (0.3.3) - mime-types - tzinfo (0.3.31) + typhoeus (0.4.2) + ffi (~> 1.0) + mime-types (~> 1.18) + tzinfo (0.3.33) xml-simple (1.1.1) - zencoder (2.3.2) - activesupport - i18n + zencoder (2.4.2) + multi_json PLATFORMS ruby @@ -110,5 +109,5 @@ PLATFORMS DEPENDENCIES factory_girl mocha - sqlite3 + sqlite3 (~> 1.3.5) zencodable! diff --git a/lib/zencodable.rb b/lib/zencodable.rb index df24d57..8ff7ed1 100644 --- a/lib/zencodable.rb +++ b/lib/zencodable.rb @@ -31,87 +31,81 @@ def has_video_encodings target_association, options = {} end - module InstanceMethods - - def job_status - unless ['finished','failed'].include? zencoder_job_status - logger.debug "Unfinished job found. Updating details." - update_job - end - self.zencoder_job_status - end - - def create_job - if self.origin_url_changed? - logger.debug "Origin URL changed. Creating new ZenCoder job." - if @job = Encoder::Job.create(self) - logger.debug "ZenCoder job created, ID = #{@job.id}" - self.zencoder_job_id = @job.id - self.zencoder_job_status = 'new' - self.zencoder_job_created = Time.now - self.zencoder_job_finished = nil - end - end + def job_status + unless ['finished','failed'].include? zencoder_job_status + logger.debug "Unfinished job found. Updating details." + update_job end + self.zencoder_job_status + end - def update_job - self.zencoder_job_status = encoder_job.status - self.zencoder_job_finished = encoder_job.finished_at - if encoded_files = encoder_job.files - self.video_files = encoded_files.collect{ |file| video_files_class.new(file) } - end - if self.class.encoder_thumbnails_association && (encoded_thumbs = encoder_job.thumbnails) - self.video_thumbnails = encoded_thumbs.collect{ |file| video_thumbnails_class.new(file) } + def create_job + if self.origin_url_changed? + logger.debug "Origin URL changed. Creating new ZenCoder job." + if @job = Encoder::Job.create(self) + logger.debug "ZenCoder job created, ID = #{@job.id}" + self.zencoder_job_id = @job.id + self.zencoder_job_status = 'new' + self.zencoder_job_created = Time.now + self.zencoder_job_finished = nil end - save - end - - def source_file_for(fmt) - self.video_files.where(:format => fmt).first end + end - def video_files - self.send(video_files_method) + def update_job + self.zencoder_job_status = encoder_job.status + self.zencoder_job_finished = encoder_job.finished_at + if encoded_files = encoder_job.files + self.video_files = encoded_files.collect{ |file| video_files_class.new(file) } end - - def video_thumbnails - self.send(video_files_thumbnails_method) + if self.class.encoder_thumbnails_association && (encoded_thumbs = encoder_job.thumbnails) + self.video_thumbnails = encoded_thumbs.collect{ |file| video_thumbnails_class.new(file) } end + save + end - def video_files= *args - self.send "#{video_files_method}=", *args - end + def source_file_for(fmt) + self.video_files.where(:format => fmt).first + end - def video_thumbnails= *args - self.send("#{video_files_thumbnails_method}=", *args) if video_files_thumbnails_method - end + def video_files + self.send(video_files_method) + end + def video_thumbnails + self.send(video_files_thumbnails_method) + end - private - def encoder_job - @job ||= Encoder::Job.new(self.zencoder_job_id) - end + def video_files= *args + self.send "#{video_files_method}=", *args + end - def video_files_method - self.class.encoder_output_files_association - end + def video_thumbnails= *args + self.send("#{video_files_thumbnails_method}=", *args) if video_files_thumbnails_method + end - def video_files_thumbnails_method - self.class.encoder_thumbnails_association - end - # need to know the Class of the associations so we can instantiate some when job is complete. - def video_files_class - self.class.reflect_on_all_associations(:has_many).detect{ |reflection| reflection.name == self.class.encoder_output_files_association }.klass - end + private + def encoder_job + @job ||= Encoder::Job.new(self.zencoder_job_id) + end - def video_thumbnails_class - self.class.reflect_on_all_associations(:has_many).detect{ |reflection| reflection.name == self.class.encoder_thumbnails_association }.klass - end + def video_files_method + self.class.encoder_output_files_association + end + def video_files_thumbnails_method + self.class.encoder_thumbnails_association end + # need to know the Class of the associations so we can instantiate some when job is complete. + def video_files_class + self.class.reflect_on_all_associations(:has_many).detect{ |reflection| reflection.name == self.class.encoder_output_files_association }.klass + end + def video_thumbnails_class + self.class.reflect_on_all_associations(:has_many).detect{ |reflection| reflection.name == self.class.encoder_thumbnails_association }.klass + end module Encoder include Zencoder diff --git a/test/dummy/db/migrate/20120620011015_add_zencoder_job_id_to_video.rb b/test/dummy/db/migrate/20120620011015_add_zencoder_job_id_to_video.rb new file mode 100644 index 0000000..1e4faf3 --- /dev/null +++ b/test/dummy/db/migrate/20120620011015_add_zencoder_job_id_to_video.rb @@ -0,0 +1,5 @@ +class AddZencoderJobIdToVideo < ActiveRecord::Migration + def change + add_column :videos, :zencoder_job_id, :string + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index ce49b50..b04d995 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20111202212901) do +ActiveRecord::Schema.define(:version => 20120620011015) do create_table "video_file_thumbnails", :force => true do |t| t.string "thumbnail_file_name" @@ -19,8 +19,8 @@ t.integer "thumbnail_file_size" t.datetime "thumbnail_updated_at" t.integer "video_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "video_files", :force => true do |t| @@ -40,13 +40,13 @@ create_table "videos", :force => true do |t| t.string "title" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "origin_url" - t.string "zencoder_job_id" t.string "zencoder_job_status" t.datetime "zencoder_job_created" t.datetime "zencoder_job_finished" - t.datetime "created_at" - t.datetime "updated_at" + t.string "zencoder_job_id" end end diff --git a/zencodable.gemspec b/zencodable.gemspec index 9e1a811..0fe9c1d 100644 --- a/zencodable.gemspec +++ b/zencodable.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |s| s.add_dependency "zencoder" s.add_runtime_dependency "typhoeus" - s.add_development_dependency "sqlite3" + s.add_development_dependency "sqlite3", "~>1.3.5" s.add_development_dependency "mocha" s.add_development_dependency "factory_girl"