Class: NetFlix::Title
- Inherits:
-
Object
- Object
- NetFlix::Title
- Defined in:
- lib/net_flix/title.rb
Direct Known Subclasses
Constant Summary collapse
- RATING_PREDICATE =
%w{ G PG PG-13 R NC-17 NR }.map do || "@term=\"#{rating}\"" end.join(' or ')
Class Method Summary collapse
- .autocomplete(params) ⇒ Object
- .base_url ⇒ Object
- .find(params) ⇒ Object
- .index(file_name) ⇒ Object
- .parse(xml) ⇒ Object
- .search(params) ⇒ Object
Instance Method Summary collapse
- #actors ⇒ Object
-
#directors ⇒ Object
not every title has a director!.
- #id ⇒ Object
- #images ⇒ Object
-
#initialize(xml) ⇒ Title
constructor
A new instance of Title.
- #is_movie? ⇒ Boolean
- #movie ⇒ Object
- #rating ⇒ Object
- #release_year ⇒ Object
- #synopsis ⇒ Object
-
#title(length = :short) ⇒ Object
suppported title lengths are :short (the default) and :regular.
- #to_s ⇒ Object
Constructor Details
#initialize(xml) ⇒ Title
Returns a new instance of Title.
7 8 9 |
# File 'lib/net_flix/title.rb', line 7 def initialize(xml) @xdoc = xml.is_a?(String) ? Nokogiri.parse( xml ) : xml end |
Class Method Details
.autocomplete(params) ⇒ Object
75 76 77 78 79 |
# File 'lib/net_flix/title.rb', line 75 def autocomplete(params) (Nokogiri.parse( NetFlix::Request.new(:url => base_url << '/autocomplete', :parameters => params).send ) / '//title/@short').to_a.map(&:to_s) end |
.base_url ⇒ Object
71 72 73 |
# File 'lib/net_flix/title.rb', line 71 def base_url 'http://api.netflix.com/catalog/titles' end |
.find(params) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/net_flix/title.rb', line 89 def find( params ) if params[:id] new( NetFlix::Request.new(:url => params[:id]).send ) elsif params[:term] search(params) end end |
.index(file_name) ⇒ Object
81 82 83 |
# File 'lib/net_flix/title.rb', line 81 def index(file_name) NetFlix::Request.new( :url => NetFlix::Title.base_url + '/index').write_to_file(file_name) end |
.parse(xml) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/net_flix/title.rb', line 97 def parse(xml) return [] unless xml nxml = Nokogiri.XML(xml) (nxml / node_xpath).map do |data| self.new(data.to_s) end end |
.search(params) ⇒ Object
85 86 87 |
# File 'lib/net_flix/title.rb', line 85 def search(params) parse(NetFlix::Request.new(:url => base_url, :parameters => params).send) end |
Instance Method Details
#actors ⇒ Object
11 12 13 |
# File 'lib/net_flix/title.rb', line 11 def actors @actors ||= ActorBuilder.from_movie(@xdoc) end |
#directors ⇒ Object
not every title has a director!
16 17 18 |
# File 'lib/net_flix/title.rb', line 16 def directors @directors ||= ( Nokogiri.parse(fetch_link('directors')) / "/people/person/name/text()" ).to_a.map(&:to_s) end |
#id ⇒ Object
45 46 47 48 49 50 |
# File 'lib/net_flix/title.rb', line 45 def id @id ||= begin node = @xdoc.search('id').first node.content if node end end |
#images ⇒ Object
33 34 35 |
# File 'lib/net_flix/title.rb', line 33 def images HashWithIndifferentAccess.new(Crack::XML.parse(@xdoc.xpath('//catalog_title/box_art').to_s)['box_art']) end |
#is_movie? ⇒ Boolean
56 57 58 |
# File 'lib/net_flix/title.rb', line 56 def is_movie? id =~ /movies\/(\d+)/ end |
#movie ⇒ Object
52 53 54 |
# File 'lib/net_flix/title.rb', line 52 def movie @movie ||= NetFlix::Movie.new(NetFlix::Request.new(:url => id ).send) if is_movie? end |
#rating ⇒ Object
20 21 22 |
# File 'lib/net_flix/title.rb', line 20 def ( @xdoc / "//catalog_title/category[#{RATING_PREDICATE}]/@term" ).to_s end |
#release_year ⇒ Object
24 25 26 |
# File 'lib/net_flix/title.rb', line 24 def release_year ( @xdoc / "//catalog_title/release_year/text()" ).to_s end |
#synopsis ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/net_flix/title.rb', line 37 def synopsis @synopsis ||= begin Crack::XML.parse(fetch_link('synopsis'))['synopsis'] rescue '' end end |
#title(length = :short) ⇒ Object
suppported title lengths are :short (the default) and :regular.
29 30 31 |
# File 'lib/net_flix/title.rb', line 29 def title(length=:short) ( @xdoc / "//catalog_title/title/@#{length}" ).to_s end |
#to_s ⇒ Object
60 61 62 |
# File 'lib/net_flix/title.rb', line 60 def to_s title || 'unknown title' end |