Sha256: 9d1b6b399331ee17ac00384cb1df24244ff906da740f4961b52529f62da058b0

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

module WhereTo
  class EpisodeFormatter
    attr_accessor :series_title, :season_number, :episode_title, :episode_number
    attr_accessor :quality, :extension
    DEFAULT_EXTENSION = '.mkv'

    def initialize(hash = {}, series_title: hash[:series_title], season_number: hash[:season_number], episode_title: hash[:episode_title], episode_number: hash[:episode_number], extension: hash[:extension], quality: hash[:quality])
      set_unless_nil :series_title, series_title
      set_unless_nil :season_number, season_number
      set_unless_nil :episode_title, episode_title
      set_unless_nil :episode_number, episode_number
      set_unless_nil :quality, quality
      set_unless_nil :extension, extension, default: DEFAULT_EXTENSION
    end

    def format!
      validate!
      formatted = []
      formatted << series_title.downcase.gsub(' ', '.')
      formatted << "SxxEyy".gsub('xx', season_number.to_s.rjust(2, '0')).gsub('yy', episode_number.to_s.rjust(2, '0'))
      formatted << episode_title.downcase.gsub(' ', '.')
      formatted << quality.downcase if quality
      formatted << extension.gsub('.', '') # remove . so the join is easier
      formatted.join('.')
    end

    def validate!
      raise 'A series title is required to format an episode title.' unless series_title
      raise 'A season number is required to format an episode title.' unless season_number
      raise 'An episode title is required to format an episode title.' unless episode_title
      raise 'An episode number is required to format an episode title.' unless episode_number
      true
    end

    private

    def set_unless_nil(key, value, default: nil)
      if value
        send("#{key}=".to_sym, value)
      elsif default
        send("#{key}=".to_sym, default)
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
where_to-0.9.4 lib/where_to/episode_formatter.rb
where_to-0.9.3 lib/where_to/episode_formatter.rb
where_to-0.9.2 lib/where_to/episode_formatter.rb
where_to-0.9.1 lib/where_to/episode_formatter.rb
where_to-0.9.0 lib/where_to/episode_formatter.rb
where_to-0.8.1 lib/where_to/episode_formatter.rb
where_to-0.8.0 lib/where_to/episode_formatter.rb