Module: Propre
- Defined in:
- lib/propre.rb
Class Method Summary collapse
- .ban_chars(arg) ⇒ Object
- .ban_words(arg) ⇒ Object
- .basename_newname_metadata(arg) ⇒ Object
- .find_episode(arg) ⇒ Object
- .find_language(arg) ⇒ Object
- .find_quality(arg) ⇒ Object
- .find_urls(arg) ⇒ Object
- .find_years(arg) ⇒ Object
- .format(newname, metadata) ⇒ Object
- .metadata(arg) ⇒ Object
- .propify(arg) ⇒ Object
- .remove_patterns(arg) ⇒ Object
- .sanitize(arg) ⇒ Object
Class Method Details
.ban_chars(arg) ⇒ Object
47 48 49 |
# File 'lib/propre.rb', line 47 def self.ban_chars(arg) arg.tr('^A-Za-z0-9 ', '').strip end |
.ban_words(arg) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/propre.rb', line 51 def self.ban_words(arg) banned_words = Dictionary.constants.reduce([]) do |sum, e| sum.concat Dictionary.const_get(e) end arg.split.each do |word| banned_words.select do |e| arg.slice! word if word.include? e end end arg.squeeze(' ') end |
.basename_newname_metadata(arg) ⇒ Object
83 84 85 86 |
# File 'lib/propre.rb', line 83 def self.(arg) basename = File.basename(arg, File.extname(arg)) [basename, format(propify(basename), (basename)), (basename)] end |
.find_episode(arg) ⇒ Object
21 22 23 24 |
# File 'lib/propre.rb', line 21 def self.find_episode(arg) arg = arg.match '(s\d{1,2}e\d{2,3})' arg.to_s end |
.find_language(arg) ⇒ Object
26 27 28 |
# File 'lib/propre.rb', line 26 def self.find_language(arg) Dictionary::LANGUAGES.find { |e| arg.include?(e) } || '' end |
.find_quality(arg) ⇒ Object
30 31 32 |
# File 'lib/propre.rb', line 30 def self.find_quality(arg) Dictionary::QUALITY.find { |e| arg.include?(e) } || '' end |
.find_urls(arg) ⇒ Object
11 12 13 14 |
# File 'lib/propre.rb', line 11 def self.find_urls(arg) arg = arg.match '((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)' arg.to_s end |
.find_years(arg) ⇒ Object
16 17 18 19 |
# File 'lib/propre.rb', line 16 def self.find_years(arg) arg = arg.match '((19|20)\d\d)' arg.to_s end |
.format(newname, metadata) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/propre.rb', line 74 def self.format(newname, ) [:newname] = newname [:year] = "(#{[:year]})" unless [:year].empty? format = '%{newname} %{episode} %{year}' formated = format % formated.strip.squeeze(' ') end |
.metadata(arg) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/propre.rb', line 63 def self.(arg) arg = arg.downcase { year: find_years(arg), episode: find_episode(arg).upcase, website: find_urls(arg), language: find_language(arg), quality: find_quality(arg) } end |
.propify(arg) ⇒ Object
7 8 9 |
# File 'lib/propre.rb', line 7 def self.propify(arg) ban_words(ban_chars(sanitize(remove_patterns(arg.downcase)))).split.map(&:capitalize).join(' ') end |
.remove_patterns(arg) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/propre.rb', line 34 def self.remove_patterns(arg) %w(find_urls find_years find_episode find_language find_quality).each do |m| arg.slice! method(m).call(arg) end arg end |
.sanitize(arg) ⇒ Object
41 42 43 44 45 |
# File 'lib/propre.rb', line 41 def self.sanitize(arg) separator = %w(- _ .).each_with_object({}) { |c, s| s[c] = arg.count(c) } .sort_by(&:last).reverse.first.first arg.gsub(separator, ' ').strip.sub(/\[.*?\]/, '') end |