Class: Sofa::TVRage::Show

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Mapping
Defined in:
lib/sofa/tvrage/show.rb

Overview

This class holds the XML information of a single Show as per the TVRage API. It's also the root point for communicating with the API.

See Also:

Defined Under Namespace

Classes: ShowNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mapping

included

Constructor Details

#initialize(id, options = {}) ⇒ Show

Returns a new instance of Show, loading and then mapping info from the TVRage API.

Parameters:

  • id (String)

    The show_id as per the TVRage API

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :greedy (Boolean)

    Whether or not to eager load the Season and Episode info

Raises:

  • (RuntimeError)


116
117
118
119
120
121
122
123
124
# File 'lib/sofa/tvrage/show.rb', line 116

def initialize(id, options = {})
  raise RuntimeError.new("id is required") unless (@show_id = id)
  klass = self.class
  if options[:greedy]
    update_with_mapping(@greedy = klass.full_info(@show_id))
  else
    update_with_mapping(klass.info(@show_id))
  end
end

Instance Attribute Details

#greedyHash

Stores all the info that was greedy-loaded

Returns:

  • (Hash)

    The full show info (including seasons and episodes)

See Also:



110
111
112
# File 'lib/sofa/tvrage/show.rb', line 110

def greedy
  @greedy
end

Class Method Details

.by_name(name, options = {}) ⇒ Show

Finds the Show by name using TVRage's Quickinfo API.

Parameters:

  • name (String)

    The name of the show to search for

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :greedy (Boolean)

    Whether or not to eager load the Season and Episode info

Returns:

  • (Show)

    The show with id parsed from the Quickinfo search

Raises:

See Also:



59
60
61
62
63
64
65
# File 'lib/sofa/tvrage/show.rb', line 59

def by_name(name, options = {})
  html = get('/tools/quickinfo.php', :query => {:show => name}, :format => :html)
  show_info = Crack::XML.parse(html)["pre"]
  raise ShowNotFound unless show_info
  id = show_info.split("\n").first.gsub(%r{^Show ID@}, '').strip
  Show.new(id, options)
end

.episode_list(sid) ⇒ Hash

Gets the episode list for a Show.

Parameters:

  • sid (String)

    The show's id

Returns:

  • (Hash)

    The parsed XML

See Also:



47
48
49
50
# File 'lib/sofa/tvrage/show.rb', line 47

def episode_list(sid)
  xml = get('/feeds/episode_list.php', :query => {:sid => sid})
  xml["Show"]
end

.full_info(sid) ⇒ Hash

Gets the full show info (info + season list + episode list) for a Show.

Parameters:

  • sid (String)

    The show's id

Returns:

  • (Hash)

    The parsed XML

See Also:



37
38
39
40
# File 'lib/sofa/tvrage/show.rb', line 37

def full_info(sid)
  xml = get('/feeds/full_show_info.php', :query => {:sid => sid})
  xml["Show"]
end

.info(sid) ⇒ Hash

Gets the info for a Show.

Parameters:

  • sid (String)

    The show's id

Returns:

  • (Hash)

    The parsed XML

See Also:



27
28
29
30
# File 'lib/sofa/tvrage/show.rb', line 27

def info(sid)
  xml = get('/feeds/showinfo.php', :query => {:sid => sid})
  xml["Showinfo"]
end

.listHash

Gets the list of all shows

Returns:

  • (Hash)

    The parsed XML

See Also:



17
18
19
20
# File 'lib/sofa/tvrage/show.rb', line 17

def list
  xml = get('/feeds/show_list.php', query: {})
  xml["shows"]['show']
end

Instance Method Details

#episode_listArray

Returns The list of episodes.

Returns:

  • (Array)

    The list of episodes



133
134
135
# File 'lib/sofa/tvrage/show.rb', line 133

def episode_list
  season_list.map { |season| season.episodes }.flatten
end

#season_listArray

Returns The list of seasons.

Returns:

  • (Array)

    The list of seasons



127
128
129
130
# File 'lib/sofa/tvrage/show.rb', line 127

def season_list
  update_with_mapping(self.class.episode_list(@show_id)) unless @season_list
  @season_list
end