Class: TitleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/net_flix/builders/title_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TitleBuilder

Returns a new instance of TitleBuilder.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/net_flix/builders/title_builder.rb', line 18

def initialize(data)
  @data = data
  @title = NetFlix::Title.new

  set_actors
  set_delivery_formats
  set_genres
  set_id
  set_release_year
  set_title
  set_web_page
  set_box_art
end

Class Method Details

.from_xml(xml) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/net_flix/builders/title_builder.rb', line 3

def self.from_xml(xml)
  return [] unless xml

  nxml = Nokogiri.XML(xml)
  
  titles(nxml).map do |data|
    TitleBuilder.new(data).title
  end
end

.titles(noko_xml) ⇒ Object



13
14
15
16
# File 'lib/net_flix/builders/title_builder.rb', line 13

def self.titles(noko_xml)
  titles = noko_xml.xpath('//catalog_title')
  titles.empty? ? noko_xml.xpath('//title_index_item') : titles 
end

Instance Method Details

#set_actorsObject



60
61
62
# File 'lib/net_flix/builders/title_builder.rb', line 60

def set_actors
  @title.actors = ActorBuilder.from_movie(@data)
end

#set_box_artObject



64
65
66
67
68
# File 'lib/net_flix/builders/title_builder.rb', line 64

def set_box_art
  box_arts = @data.search('box_art')
  @title.box_art = {}
  %w{ small medium large }.each { |size| @title.box_art[size] = box_arts.attr(size).value }
end

#set_delivery_formatsObject



52
53
54
# File 'lib/net_flix/builders/title_builder.rb', line 52

def set_delivery_formats
  @title.delivery_formats = FormatBuilder.from_movie(@data)
end

#set_genresObject



56
57
58
# File 'lib/net_flix/builders/title_builder.rb', line 56

def set_genres
  @title.genres = @data.search('category[@scheme="http://api.netflix.com/categories/genres"]').map{|f| f['label'] }
end

#set_idObject



32
33
34
35
# File 'lib/net_flix/builders/title_builder.rb', line 32

def set_id
  node = @data.search('id').first
  @title.id = node.content if node
end

#set_release_yearObject



37
38
39
40
# File 'lib/net_flix/builders/title_builder.rb', line 37

def set_release_year
  node = @data.search('release_year').first
  @title.release_year = node.content if node
end

#set_titleObject



42
43
44
45
# File 'lib/net_flix/builders/title_builder.rb', line 42

def set_title
  node = @data.search('title').first
  @title.title = node['regular'] || node.content if node
end

#set_web_pageObject



47
48
49
50
# File 'lib/net_flix/builders/title_builder.rb', line 47

def set_web_page
  node = @data.search('link[@title="web page"]').first
  @title.web_page = node['href'] if node
end

#titleObject



70
71
72
# File 'lib/net_flix/builders/title_builder.rb', line 70

def title
  @title
end