Class: Moviesort::TVShow::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/moviesort/tv_show.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Parser

Returns a new instance of Parser.



32
33
34
# File 'lib/moviesort/tv_show.rb', line 32

def initialize(filename)
  @filename = sanitize(filename)
end

Instance Method Details

#extract_data_from_pattern_match(match) ⇒ Object



46
47
48
# File 'lib/moviesort/tv_show.rb', line 46

def extract_data_from_pattern_match(match)
  [match[1], *match[2..3].map { |s| s.to_i }]
end

#parseObject



36
37
38
39
40
41
42
43
44
# File 'lib/moviesort/tv_show.rb', line 36

def parse
  patterns = [
    /(.*)\sS(\d+)E(\d+)/i, 
    /(.*)\s(\d{1,2})x(\d+)/,
    /(.*)\s(\d{1})(\d{2})/
  ]
  match = patterns.map { |regex| @filename.match(regex) }.compact.first
  extract_data_from_pattern_match(match) if match
end

#sanitize(filename) ⇒ Object



50
51
52
# File 'lib/moviesort/tv_show.rb', line 50

def sanitize(filename)
  filename.gsub(/\./, ' ').gsub(/-/, ' ').squeeze(' ')
end