Class: FilmAffinity::Movie

Inherits:
Object
  • Object
show all
Defined in:
lib/filmaffinity/movie.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, title = nil) ⇒ Movie

Returns a new instance of Movie.



4
5
6
7
8
9
# File 'lib/filmaffinity/movie.rb', line 4

def initialize(id, title = nil)
  @id = id
  @title = title if title
  @json_parser = JsonMovieParser.new
  @poster_manager = PosterManager.new
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/filmaffinity/movie.rb', line 3

def id
  @id
end

#titleObject (readonly)

Returns the value of attribute title.



3
4
5
# File 'lib/filmaffinity/movie.rb', line 3

def title
  @title
end

Instance Method Details

#castObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/filmaffinity/movie.rb', line 75

def cast
  actors = []
  node = document_html.search(Constants.tag(:cast))
  node.each do |actor|
    actors << actor.at(Constants.tag(:cast_each)).content.strip
  end
  actors
rescue
  []
end

#companyObject



57
58
59
60
61
# File 'lib/filmaffinity/movie.rb', line 57

def company
  document_html.at(Constants.tag(:company)).next_sibling.next_sibling.content
rescue
  nil
end

#countryObject



37
38
39
40
41
42
# File 'lib/filmaffinity/movie.rb', line 37

def country
  raw_country = document_html.at(Constants.tag(:country)).next_sibling.content
  raw_country.gsub(/\A[[:space:]]+|[[:space:]]+\z/, '') if raw_country
rescue
  nil
end

#directorObject



44
45
46
47
48
49
# File 'lib/filmaffinity/movie.rb', line 44

def director
  raw_director = document_html.at(Constants.tag(:director)).content
  raw_director.strip if raw_director
rescue
  nil
end

#document_htmlObject



11
12
13
# File 'lib/filmaffinity/movie.rb', line 11

def document_html
  @document_html ||= Nokogiri::HTML(generate_html)
end

#durationObject



31
32
33
34
35
# File 'lib/filmaffinity/movie.rb', line 31

def duration
  document_html.at(Constants.tag(:duration)).content[/\d+/].to_i
rescue
  nil
end

#generate_htmlObject



15
16
17
# File 'lib/filmaffinity/movie.rb', line 15

def generate_html
  open(Constants.urls[:movie] % id)
end

#genresObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/filmaffinity/movie.rb', line 86

def genres
  genres = []
  node = document_html.at(Constants.tag(:genre)).next_sibling.next_sibling
  raw_genres = node.search('a')
  raw_genres.each do |raw_genre|
    genres << raw_genre.content.strip
  end
  genres
rescue
  []
end

#musicObject



51
52
53
54
55
# File 'lib/filmaffinity/movie.rb', line 51

def music
  document_html.at(Constants.tag(:music)).next_sibling.next_sibling.content
rescue
  nil
end

#photographyObject



69
70
71
72
73
# File 'lib/filmaffinity/movie.rb', line 69

def photography
  document_html.at(Constants.tag(:photography)).next_sibling.next_sibling.content
rescue
  nil
end

#posterObject



111
112
113
114
115
116
# File 'lib/filmaffinity/movie.rb', line 111

def poster
  poster_url = document_html.at(Constants.tag(:poster))['src']
  @poster_manager.load_poster(poster_url) if poster_url
rescue
  nil
end

#poster_bigObject



118
119
120
121
122
123
# File 'lib/filmaffinity/movie.rb', line 118

def poster_big
  poster_url = document_html.at(Constants.tag(:poster_big))['href']
  @poster_manager.load_poster(poster_url) if poster_url
rescue
  nil
end

#ratingObject



104
105
106
107
108
109
# File 'lib/filmaffinity/movie.rb', line 104

def rating
  raw_rating = document_html.at(Constants.tag(:rating)).content.strip
  raw_rating.tr(',', '.').to_f if raw_rating
rescue
  nil
end

#scriptObject



63
64
65
66
67
# File 'lib/filmaffinity/movie.rb', line 63

def script
  document_html.at(Constants.tag(:script)).next_sibling.next_sibling.content
rescue
  nil
end

#sinopsisObject



98
99
100
101
102
# File 'lib/filmaffinity/movie.rb', line 98

def sinopsis
  document_html.at(Constants.tag(:sinopsis)).content
rescue
  nil
end

#to_jsonObject



125
126
127
# File 'lib/filmaffinity/movie.rb', line 125

def to_json
  @json_parser.to_json self
end

#yearObject



25
26
27
28
29
# File 'lib/filmaffinity/movie.rb', line 25

def year
  document_html.at(Constants.tag(:year)).content[/\d+/].to_i
rescue
  nil
end