class Tvdbjson::Episode

Attributes

episode_number[RW]
first_aired[RW]
id[RW]
name[RW]
overview[RW]
season_number[RW]
series_id[RW]

Public Class Methods

all_episodes(options = {}, authentication) click to toggle source
# File lib/tvdbjson/episode.rb, line 18
def self.all_episodes(options = {}, authentication)
  raise Tvdbjson::RequiredSeriesIdMissingError if !options[:series_id]
  raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)

  hash = {
    :method       => "GET",
    :uri          => "/series/#{options[:series_id]}/episodes",
    :params       => { "page" => 1 }
  }

  # Create an empty array for all episodes
  arr = []

  # Receive the first page of results
  response = send_authenticated_request(hash, authentication)

  # Store the Page count to retrieve additional pages in the future
  page_count = get_page_count_from_response(response)

  # Whilst we have the response object for page 1, let's add it to our array
  arr.concat( Episode.from_response(response,options[:series_id]) )

  # Now to process the additional pages (starting with page 2)
  (2..page_count).each do |page_number|
    hash = {
      :method       => "GET",
      :uri          => "/series/#{options[:series_id]}/episodes",
      :params       => { "page" => page_number }
    }
    response = send_authenticated_request(hash, authentication)
    arr.concat( Episode.from_response(response,options[:series_id]) )
  end

  arr

end
from_response(response, series_id) click to toggle source
# File lib/tvdbjson/episode.rb, line 70
def self.from_response(response, series_id)
  results_array = []

  begin
    response.parsed_response["data"].each do |record|
      hash                  = {}
      hash[:id]             = record["id"]
      hash[:name]           = record["episodeName"]
      hash[:series_id]      = series_id
      hash[:episode_number] = record["airedEpisodeNumber"]
      hash[:season_number]  = record["airedSeasonID"]
      hash[:first_aired]    = record["firstAired"]
      hash[:overview]       = record["overview"]

      result = Episode.new(hash)
      results_array << result
    end
  rescue NoMethodError
    # Means an empty result set was found, don't do anything special
    # here as this method will return an empty array.
  end
  results_array
end
get_page_count_from_response(response) click to toggle source
# File lib/tvdbjson/episode.rb, line 94
def self.get_page_count_from_response(response)
  response.parsed_response["links"]["last"]
end
new(options = {}) click to toggle source
# File lib/tvdbjson/episode.rb, line 8
def initialize(options = {})
  @id             = options[:id]
  @name           = options[:name]
  @series_id      = options[:series_id]
  @episode_number = options[:episode_number]
  @season_number  = options[:season_number]
  @first_aired    = options[:first_aired]
  @overview       = options[:overview]
end
search_by_season_and_episode(options = {}, authentication) click to toggle source
# File lib/tvdbjson/episode.rb, line 55
def self.search_by_season_and_episode(options = {}, authentication)
  raise Tvdbjson::RequiredSeriesIdMissingError if !options[:series_id]
  raise Tvdbjson::RequiredSearchParamMissingError if !options[:season_number]
  raise Tvdbjson::RequiredSearchParamMissingError if !options[:episode_number]
  raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)

  hash = {
    :method       => "GET",
    :uri          => "/series/#{options[:series_id]}/episodes/query",
    :after_action => "Tvdbjson::Episode.from_response(response, #{options[:series_id]})",
    :params       => { "airedSeason" => options[:season_number], "airedEpisode" => options[:episode_number] }
  }
  send_authenticated_request(hash, authentication)
end