Permalink
Browse files

Some named scopes on Video.

  • Loading branch information...
1 parent 237a059 commit 2a0ffad200bcc1a3839ac8de67a65f3f276837b0 dave committed Oct 28, 2010
Showing with 18 additions and 10 deletions.
  1. +18 −10 lib/enigmamachine/models/video.rb
@@ -100,10 +100,14 @@ def self.complete
all(:state => 'complete', :order => [:updated_at.desc])
end
+ # Named scope returning all videos which are not yet downloaded.
+ #
def self.waiting_for_download
all(:state => 'waiting_for_download')
end
+ # Named scope returning all videos which currently downloading.
+ #
def self.downloading
all(:state => 'downloading')
end
@@ -215,26 +219,30 @@ def notify_complete
# are called. It's a chicken-and-egg situation. So I'm hitting it with the
# big hammer and setting state manually.
#
- # This is a disgusting abuse of the state machine, but it sort of works.
- # By the time this method is complete, tests will assume our state is
+ # This is a disgusting abuse of the state machine, but it works.
+ #
+ # NOTE: By the time this method is complete, tests will assume our state is
# "unencoded" (as a result of the download_complete! event) rather than
# "downloading", as it should be.
#
def do_download
self.state = "downloading"
self.save!
http = EventMachine::HttpRequest.new(file).get :timeout => 10
- http.stream { |data|
- outfile = File.join(Dir.pwd, "downloads", self.id.to_s, File.basename(URI.parse(file).path))
+
+ http.stream do |data|
+ filename = File.basename(URI.parse(file).path)
+ outfile = File.join(Dir.pwd, "downloads", self.id.to_s, filename)
File.open(outfile, 'a') {|f| f.write(data) }
- }
- http.callback {
- puts "download complete, calling event..."
+ end
+
+ http.callback do
download_complete!
- }
- http.errback {
+ end
+
+ http.errback do
download_error!
- }
+ end
end
# Returns false if the video is available via http

0 comments on commit 2a0ffad

Please sign in to comment.