Class: EventMachine::Sofa::TVRage::Show

Inherits:
Object
  • Object
show all
Extended by:
EventMachine::Sofa::TVRage
Includes:
Mapping
Defined in:
lib/em-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: NotFound

Constant Summary collapse

Base_Uri =
'services.tvrage.com'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}, &block) ⇒ 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

  • &block (Block)

    Called back with the show with parsed info unless fibered

  • 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)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/em-sofa/tvrage/show.rb', line 133

def initialize(id, options = {}, &block)
  raise RuntimeError.new("id is required") unless (@show_id = id)
  return TVRage::Request.fibered(method(__method__), id, options) if defined? Fiber and Fiber.respond_to? :current and not block
  raise ArgumentError, "No block given for completion callback" unless block
  klass = self.class
  if options[:greedy]
    return block.call(self) if @greedy
    klass.full_info(@show_id) do |full_info|
      next block.call(nil) unless full_info
      update_with_mapping(@greedy = full_info)
      block.call(self)
    end
  elsif options[:info]
    update_with_mapping(options[:info])
    block.call(self)
  else
    klass.info(@show_id) do |info|
      next block.call(nil) unless info
      update_with_mapping(info)
      block.call(self)
    end
  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:



126
127
128
# File 'lib/em-sofa/tvrage/show.rb', line 126

def greedy
  @greedy
end

Class Method Details

.by_name(name, options = {}, &block) ⇒ EM:TVRage::Request

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

Parameters:

  • name (String)

    The name of the show to search for

  • &block (Block)

    Called back with the show with id parsed from the Quickinfo search unless fibered

  • 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:

  • (EM:TVRage::Request)

    TVRage request object or result Show object if fibered

Raises:

  • (ArgumentError)

See Also:



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

def by_name(name, options = {}, &block)
  return Request.fibered(method(__method__), name, options) if defined? Fiber and Fiber.respond_to? :current and not block
  raise ArgumentError, "No block given for completion callback" unless block
  Request.new(Base_Uri, '/tools/quickinfo.php', block, :parse_element => 'pre', :show => name) do |request, xml|
    options[:info] = parsed_quickinfo(xml)
    Show.new(options[:info]['showid'], options) {|show| request.succeed show }
  end
end

.episode_list(sid, &block) ⇒ EM:TVRage::Request

Gets the episode list for a Show.

Parameters:

  • sid (String)

    The show's id

  • &block (Block)

    Called back with the parsed XML when successful unless fibered

Returns:

  • (EM:TVRage::Request)

    TVRage request object or result hash if fibered

Raises:

  • (ArgumentError)

See Also:



44
45
46
47
48
# File 'lib/em-sofa/tvrage/show.rb', line 44

def episode_list(sid, &block)
  return Request.fibered(method(__method__), sid) if defined? Fiber and Fiber.respond_to? :current and not block
  raise ArgumentError, "No block given for completion callback" unless block
  Request.new(Base_Uri, '/feeds/episode_list.php', block, :return_element => 'Show', :sid => sid)
end

.full_info(sid, &block) ⇒ EM:TVRage::Request

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

Parameters:

  • sid (String)

    The show's id

  • &block (Block)

    Called back with the parsed XML when successful unless fibered

Returns:

  • (EM:TVRage::Request)

    TVRage request object or result hash if fibered

Raises:

  • (ArgumentError)

See Also:



32
33
34
35
36
# File 'lib/em-sofa/tvrage/show.rb', line 32

def full_info(sid, &block)
  return fibered_async(__method__, sid, options) if defined? Fiber and Fiber.respond_to? :current and not block
  raise ArgumentError, "No block given for completion callback" unless block
  Request.new(Base_Uri, '/feeds/full_show_info.php', block, :return_element => 'Show', :sid => sid)
end

.info(sid, &block) ⇒ EM:TVRage::Request

Gets the info for a Show.

Parameters:

  • sid (String)

    The show's id

  • &block (Block)

    Called back with the parsed XML when successful unless fibered

Returns:

  • (EM:TVRage::Request)

    TVRage request object or result hash if fibered

Raises:

  • (ArgumentError)

See Also:



20
21
22
23
24
# File 'lib/em-sofa/tvrage/show.rb', line 20

def info(sid, &block)
  return fibered_async(__method__, sid, options) if defined? Fiber and Fiber.respond_to? :current and not block
  raise ArgumentError, "No block given for completion callback" unless block
  Request.new(Base_Uri, '/feeds/showinfo.php', block, :return_element => 'Showinfo', :sid => sid)
end

.parsed_quickinfo(raw_info) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/em-sofa/tvrage/show.rb', line 67

def parsed_quickinfo(raw_info)
  info = Hash[raw_info.split(/\n/).collect {|line| k, v = line.split /@/; [k.gsub(' ', '').downcase, v] }]
  info['showlink'] = info.delete('showurl')
  info.each_key do |key|
    case key.to_sym
      when :latestepisode, :nextepisode
        info[key] = Hash[[:number, :name, :date].zip(info[key].split /\^/)]
        info[key][:date] = Date.parse(info[key][:date])
      when :genres
        info[key] = info[key].split ' | '
    end
  end
end

Instance Method Details

#episode_list(&block) ⇒ Object

Parameters:

  • &block (Block)

    Called back with the list of episodes

Raises:

  • (ArgumentError)


175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/em-sofa/tvrage/show.rb', line 175

def episode_list(&block)
  if defined? Fiber and Fiber.respond_to? :current and not block
    f = Fiber.current
    method(__method__).call {|data| f.resume(data) }
    return Fiber.yield
  end
  raise ArgumentError, "No block given for completion callback" unless block
  season_list do |seasons|
    block.call(seasons.collect { |season| season.episodes }.flatten)
  end
  true
end

#season_list(&block) ⇒ Object

Parameters:

  • &block (Block)

    Called back with the list of seasons

Raises:

  • (ArgumentError)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/em-sofa/tvrage/show.rb', line 158

def season_list(&block)
  if defined? Fiber and Fiber.respond_to? :current and not block
    f = Fiber.current
    method(__method__).call {|data| f.resume(data) }
    return Fiber.yield
  end
  raise ArgumentError, "No block given for completion callback" unless block
  puts "@season_list = #@season_list" if @season_list
  return block.call(@season_list) if @season_list
  self.class.episode_list(@show_id) do |episode_list|
    update_with_mapping(episode_list)
    block.call(@season_list)
  end
  true
end