Sha256: 08c4b43e76a1e124fca68e05dd3ce1badc0004890e1413edb54a3fe57f8f63e9

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require 'where_to/location'
require 'yaml'

module WhereTo
  class Locator
    attr_accessor :series_title, :airdate, :season, :season_airdate
    attr_accessor :episode_title, :episode_number, :episode_quality, :episode_extension
    FORMAT_FILE = 'lib/where_to/format.yml'

    def initialize(hash = {})
      load_values_from hash
      @format_file = YAML.load_file FORMAT_FILE
    end
  
    def locate(hash = {})
      load_values_from hash
      validate!

      output = @format_file['folder_format']
      output.gsub! '%series_title',   series_title
      output.gsub! '%season_number',  season.to_s
      output.gsub! '%season_airdate', season_airdate.to_s

      if episode_title and episode_number
        formatter = WhereTo::EpisodeFormatter.new params
        output << formatter.format!
      end

      WhereTo::Location.new output
    end

    def validate!
      raise 'A season airdate is required to locate an episode' if season_airdate == nil
      raise 'A season number is required to locate an episode' if season.nil?
      true
    end

    def params
      _params = {}
      _params[:series_title]   = series_title
      _params[:season_number]  = season
      _params[:episode_title]  = episode_title
      _params[:episode_number] = episode_number
      _params[:quality]        = episode_quality
      _params[:extension]      = episode_extension
      _params
    end

    private

    def set_unless_nil(key, value)
      send("#{key}=".to_sym, value) unless value.nil?
    end

    def load_values_from(hash = {})
      set_unless_nil :series_title,   hash[:series_title]
      set_unless_nil :airdate,        hash[:airdate]
      set_unless_nil :season,         hash[:season]
      set_unless_nil :season_airdate, hash[:season_airdate]
      set_unless_nil :episode_title,  hash[:episode_title]
      set_unless_nil :episode_number, hash[:episode_number]
      set_unless_nil :episode_quality, hash[:episode_quality]
      set_unless_nil :episode_extension, hash[:episode_extension]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
where_to-0.8.1 lib/where_to/locator.rb
where_to-0.8.0 lib/where_to/locator.rb