class ShowFeature::Processor

The main processor class is in charge of the parsing of tv show filename

Constants

PATTERN

Pattern used for the parsing process

Attributes

episode[R]
name[R]
parsed[R]
raw_name[R]
season[R]
team[R]

Public Class Methods

new(raw_name) click to toggle source

Creates a new Processor instance for raw_name parsing

An ArgumentError is raised if raw_name‘s mime type does not match the type of a video file

# File lib/showfeature/processor.rb, line 20
def initialize(raw_name)
  raise ShowFeature::ArgumentError, "#{raw_name} does not match a video or subtitle file type" unless
    [:video_file_type?, :subtitles_file_type?].any? {|f| ShowFeature::Processor.send f,raw_name }
  @raw_name = raw_name
  @parsed = false
end
subtitles_file_type?(str) click to toggle source

Checks if str‘s mime type matches a subtitles file type

# File lib/showfeature/processor.rb, line 72
def self.subtitles_file_type?(str)
  (str =~ /\.(srt|sub)$/) ? true : false
end
video_file_type?(str) click to toggle source

Checks if str‘s mime type matches a video file type

# File lib/showfeature/processor.rb, line 60
def self.video_file_type?(str)
  MIME::Types.of(str).any? do |x| x.to_s =~/^video/ end
end

Public Instance Methods

parse() click to toggle source

Parses the tv show filename looking for relevant element

# File lib/showfeature/processor.rb, line 29
def parse
  proc = Proc.new do |position, proc|
    tmp = @raw_name[PATTERN,position]
    tmp.nil? ? nil : proc.call(tmp)
  end
  @name = proc.call(1, lambda{|x| x.downcase.gsub('.',' ')})
  @season = proc.call(2, lambda{|x| "%02d" % x[/(\d+).?(\d{2}$)/,1].to_i})
  @episode = proc.call(2, lambda{|x| "%02d" % x[/(\d+).?(\d{2}$)/,2].to_i})
  @team = proc.call(3, lambda{|x| x.downcase})
  @parsed = true
end
parsed?() click to toggle source

Checks if the tv show filename has already been parsed

# File lib/showfeature/processor.rb, line 43
def parsed?
  @parsed
end
replace(arg) click to toggle source
# File lib/showfeature/processor.rb, line 82
def replace(arg)
  raise ShowFeature::TypeError, 'argument must be of type String or Hash' unless 
    [String, Hash].any?{|type| arg.is_a? type}
  raise ShowFeature::NotParsedError, 'showfeature cannot complete replace if show is not parsed' unless parsed?
  if arg.is_a? String
    if video_file_type?
      @raw_name = arg if ShowFeature::Processor.video_file_type?(arg)
    elsif subtitle_file_type?
      @raw_name = arg if ShowFeature::Processor.subtitles_file_type?(arg)
    end
  else
    hash = {
      :name =>@name, 
      :season => @season, 
      :episode => @episode, 
      :team => @team }.merge(arg) {|k,v1,v2| k.eql?(:episode) ? "%02d"%v2 : v2}
    hash.each do |key, value|
      @raw_name.sub! self.send("#{key}").gsub(' ','.'), value
      instance_variable_set("@#{key}", value)
    end
  end
end
subtitles_file_type?() click to toggle source

Checks if the mime type of the current processed show matches a subtitles file type

# File lib/showfeature/processor.rb, line 78
def subtitles_file_type?
  (@raw_name =~ /\.(srt|sub)$/) ? true : false
end
to_hsh() click to toggle source

Returns hash filled with all parsed element

# File lib/showfeature/processor.rb, line 49
def to_hsh
  hash = Hash.new
  hash.merge!( { :name =>@name, 
                :season => @season,
                :episode => @episode,
                :team => @team } ) unless [@name,@season,@episode, @team].all? {|x| x.eql?nil}
  hash
end
video_file_type?() click to toggle source

Checks if the mime type of the current processed show matches a video file type

# File lib/showfeature/processor.rb, line 66
def video_file_type?
  MIME::Types.of(@raw_name).any? do |x| x.to_s =~/^video/ end
end