Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove instance methods module #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 55 additions & 56 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,114 +1,113 @@
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

DEPENDENCIES
factory_girl
mocha
sqlite3
sqlite3 (~> 1.3.5)
zencodable!
118 changes: 56 additions & 62 deletions lib/zencodable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddZencoderJobIdToVideo < ActiveRecord::Migration
def change
add_column :videos, :zencoder_job_id, :string
end
end
12 changes: 6 additions & 6 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
#
# 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"
t.string "thumbnail_content_type"
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|
Expand All @@ -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
2 changes: 1 addition & 1 deletion zencodable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down