class TVDBApi

Public Class Methods

new(api_key,opts = {}) click to toggle source
# File lib/tvdb_api.rb, line 9
def initialize(api_key,opts = {})
  @lang = opts[:lang] || 'en'
  @api_key = api_key
  @tries = 3
end

Public Instance Methods

[](*args) click to toggle source
# File lib/tvdb_api.rb, line 119
def [](*args)
  get args
end
get(path,query=nil, options={}) click to toggle source
# File lib/tvdb_api.rb, line 99
def get(path,query=nil, options={})
  if path.respond_to? :join
    path = path.join("/")
  end
  path = to_full path
  if query
    options[:query] = query
  end
  ex = nil
  @tries.times do
    begin
      res = self.class.get(path,options)
      return res
    rescue => e
      ex = e
    end
  end
  raise ex
end
get_episode_by_air_date(series_id, air_date, lang = @lang) click to toggle source
# File lib/tvdb_api.rb, line 29
def get_episode_by_air_date(series_id, air_date, lang = @lang)
  if air_date.respond_to? :to_time
    air_date = air_date.to_time
  end
  if air_date.respond_to? :strftime
    air_date = air_date.strftime('%F')
  end

  get 'GetEpisodeByAirDate.php', 
    apikey: @api_key, language: lang,
    seriesid: series_id, airdate: air_date
end
get_retings_for_user(account_id, series_id = nil) click to toggle source
# File lib/tvdb_api.rb, line 42
def get_retings_for_user(account_id, series_id = nil)
  query = { apikey: @api_key, accountid: account_id }
  query[:seriesid] = series_id if series_id
  get 'GetRatingsForUser.php', query
end
get_series(series_name, lang = @lang) click to toggle source
# File lib/tvdb_api.rb, line 15
def get_series(series_name, lang = @lang)
  get 'GetSeries.php', seriesname: series_name, language: lang
end
get_series_by_remote_id(type, id, lang = @lang) click to toggle source
# File lib/tvdb_api.rb, line 19
def get_series_by_remote_id(type, id, lang = @lang)
  query = {language: lang}
  case type
  when :imdb then query[:imdbid] = id
  when :zap2it then query[:zap2it] = id
  else raise "Invalid remote id type '#{type}'"
  end
  get 'GetSeriesByRemoteID.php', query
end
to_full(path) click to toggle source
# File lib/tvdb_api.rb, line 71
def to_full(path)
  if path !~ /\.php$/
    path = @api_key+'/'+path

    if path =~ /\/\d+(\/all)?\/?$/
      path += '/' + @lang + '.xml'
    end

    if path =~ /(.*\/updates\/)(?!updates_)(.*)/
      path = $1+"updates_"+$2
    end

    if path =~ /\/[^\/\.]*$/
      path += '.xml'
    end
  end

  if path[0] != '/'
    if path !~ /^?api\//
      path = '/api/'+path
    else
      path = '/' + path 
    end
  end

  path
end
user_favorites(account_id, change = nil, series_id = nil) click to toggle source
# File lib/tvdb_api.rb, line 52
def user_favorites(account_id, change = nil, series_id = nil)
  query = {accountid: account_id}
  if change
    if [:add,:remove].include?(change) && series_id
      query[:type] = change
      query[:seriesid] = series_id
    else
      raise ArgumentError.new("change may only be :add or :remove and series_id must be set if change is set")
    end
  end
  get 'User_Favorites.php', query
end
user_preferred_language(account_id) click to toggle source
# File lib/tvdb_api.rb, line 48
def user_preferred_language(account_id)
  get 'User_PreferredLanguage.php', accountid: account_id
end
user_rating(account_id, item_type, item_id, rating) click to toggle source
# File lib/tvdb_api.rb, line 65
def user_rating(account_id, item_type, item_id, rating)
  get 'User_Rating.php',
    accountid: account_id, itemtype: item_type,
    itemid: itemid, rating: rating
end