lib/subtitle.rb in undertexter-0.1.4 vs lib/subtitle.rb in undertexter-0.1.5
- old
+ new
@@ -1,65 +1,67 @@
require 'fileutils'
require 'mimer_plus'
-class Subtitle
- attr_accessor :details, :downloads, :cds, :title, :movie_title, :url
+module Container
+ class Subtitle
+ attr_accessor :details, :downloads, :cds, :title, :movie_title, :url
- def initialize(args)
- args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] }
- end
+ def initialize(args)
+ args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] }
+ end
- def url
- @language == :english ? "http://eng.undertexter.se/subtitle.php?id=#{id}" : "http://undertexter.se/laddatext.php?id=#{id}"
- end
-
- # Downloading the file and saves it disk
- def download!(args = {})
- if args[:to].nil?
- dir = "/tmp"
- file_name = "#{dir}/#{generate_file_name}"
- else
- dir = generate_custom_file_path(args)
- file_name = "#{dir}/#{generate_file_name}"
+ def url
+ @language == :english ? "http://eng.undertexter.se/subtitle.php?id=#{id}" : "http://undertexter.se/laddatext.php?id=#{id}"
end
+
+ # Downloading the file and saves it disk
+ def download!(args = {})
+ if args[:to].nil?
+ dir = "/tmp"
+ file_name = "#{dir}/#{generate_file_name}"
+ else
+ dir = generate_custom_file_path(args)
+ file_name = "#{dir}/#{generate_file_name}"
+ end
- data = RestClient.get(self.url, :timeout => 10) rescue nil
- file = File.new(file_name, 'w')
- file.write(data)
- file.close
+ data = RestClient.get(self.url, :timeout => 10) rescue nil
+ file = File.new(file_name, 'w')
+ file.write(data)
+ file.close
- type = Mimer.identify(file_name)
+ type = Mimer.identify(file_name)
- if type.zip?
- file_ending = ".zip"
- elsif type.rar?
- file_ending = ".rar"
- else
- file_ending = ""
- end
+ if type.zip?
+ file_ending = ".zip"
+ elsif type.rar?
+ file_ending = ".rar"
+ else
+ file_ending = ""
+ end
- new_file_name = "#{dir}/#{title.gsub(/\s+/, '.')}#{file_ending}"
+ new_file_name = "#{dir}/#{title.gsub(/\s+/, '.')}#{file_ending}"
- # Changing the name on the file
- FileUtils.mv(file_name, new_file_name)
+ # Changing the name on the file
+ FileUtils.mv(file_name, new_file_name)
- # I like return :)
- return new_file_name
- end
-
- private
- def id
- @details.match(/id=(\d+)/)[1]
+ # I like return :)
+ return new_file_name
end
+
+ private
+ def id
+ @details.match(/id=(\d+)/)[1]
+ end
- def generate_file_name
- (0...30).map{65.+(rand(25)).chr}.join.downcase
- end
+ def generate_file_name
+ (0...30).map{65.+(rand(25)).chr}.join.downcase
+ end
- def generate_custom_file_path(args)
- # If the path is relative
- args[:to] = File.expand_path(args[:to]) unless args[:to].match(/^\//)
+ def generate_custom_file_path(args)
+ # If the path is relative
+ args[:to] = File.expand_path(args[:to]) unless args[:to].match(/^\//)
- # Makes sure that every directory structure looks the same
- Dir.new(args[:to]).path
- end
+ # Makes sure that every directory structure looks the same
+ Dir.new(args[:to]).path
+ end
+ end
end
\ No newline at end of file