Skip to content
Permalink
Browse files

added television class for parsing out just television shows

  • Loading branch information...
briandunn committed Aug 27, 2009
1 parent dafbdaf commit 856768edfba803960726aed8a35a1ceef44c55ef
Showing with 42 additions and 28 deletions.
  1. +0 −28 lib/net_flix/movie.rb
  2. +14 −0 lib/net_flix/television.rb
  3. +28 −0 lib/net_flix/title.rb
@@ -1,33 +1,5 @@
module NetFlix
class Movie < Title
RATING_PREDICATE = %w{ G PG PG-13 R NC-17 NR }.map do |rating|
"@term=\"#{rating}\""
end.join(' or ')

# not every movie has a director!
def directors
@directors ||= ( Nokogiri.parse(fetch_link('directors')) / "/people/person/name/text()" ).to_a.map(&:to_s)
end

def rating
( @xdoc / "//catalog_title/category[#{RATING_PREDICATE}]/@term" ).to_s
end

def release_year
( @xdoc / "//catalog_title/release_year/text()" ).to_s
end

def actors
@actors ||= ActorBuilder.from_movie(@xdoc)
end

def self.find( params )
if params[:id]
new( NetFlix::Request.new(:url => params[:id]).send )
elsif params[:term]
search(params)
end
end

protected
# the nodes that correspond to the constructor argument
@@ -0,0 +1,14 @@
module NetFlix
class Television < Title

def actors
@actors ||= ActorBuilder.from_movie(@xdoc)
end

protected
# the nodes that correspond to the constructor argument
def self.node_xpath
"//catalog_title[not(contains(id/text(),'movies'))]"
end
end
end
@@ -1,10 +1,30 @@
module NetFlix
class Title
RATING_PREDICATE = %w{ G PG PG-13 R NC-17 NR }.map do |rating|
"@term=\"#{rating}\""
end.join(' or ')

def initialize(xml)
@xdoc = xml.is_a?(String) ? Nokogiri.parse( xml ) : xml
end

def actors
@actors ||= ActorBuilder.from_movie(@xdoc)
end

# not every title has a director!
def directors
@directors ||= ( Nokogiri.parse(fetch_link('directors')) / "/people/person/name/text()" ).to_a.map(&:to_s)
end

def rating
( @xdoc / "//catalog_title/category[#{RATING_PREDICATE}]/@term" ).to_s
end

def release_year
( @xdoc / "//catalog_title/release_year/text()" ).to_s
end

# suppported title lengths are :short (the default) and :regular.
def title(length=:short)
( @xdoc / "//catalog_title/title/@#{length}" ).to_s
@@ -66,6 +86,14 @@ def search(params)
parse(NetFlix::Request.new(:url => base_url, :parameters => params).send)
end

def find( params )
if params[:id]
new( NetFlix::Request.new(:url => params[:id]).send )
elsif params[:term]
search(params)
end
end

def parse(xml)
return [] unless xml

0 comments on commit 856768e

Please sign in to comment.
You can’t perform that action at this time.