Sha256: 5ae35f8a03101a5f220bc0c49b732d2481bde1a59affb588cdcbc979f1d3b4b2

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'tvdb_party'

module WhereTo
  class TVDB 
    attr_accessor :series_title, :episode_number, :season, :episode_title, :season_airdate, :series_id
    attr_reader :api_key

    def initialize(params = {})
      load_values_from params
      @api_key = WhereTo.configuration.tvdb_api_key
      @db = TvdbParty::Search.new api_key
    end

    def lookup!
      validate!
      results = @db.search(series_title).first unless series_id
      series  = @db.get_series_by_id (series_id || results['seriesid'])
      episode = series.get_episode season, episode_number
      @episode_title  = episode.name
      @season_airdate = episode.air_date.year 

      updated = {}
      updated[:episode_title]  = episode_title
      updated[:season_airdate] = season_airdate
      updated
    rescue URI::InvalidURIError
      raise 'You need to configure your TVDB API key before looking up episode information'
    end

    def validate!
      raise 'A series title is required to lookup episode information' if series_title.nil?
      raise 'An episode number is required to lookup episode information' if episode_number.nil?
      raise 'A season number is required to lookup episode information' if season.nil?
      true
    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_number]
      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

1 entries across 1 versions & 1 rubygems

Version Path
where_to-0.9.6 lib/where_to/tvdb.rb