Class: BasicYoutube::Video

Inherits:
YoutubeObject show all
Includes:
HTTParty
Defined in:
lib/basic_youtube/video.rb

Constant Summary collapse

VALID_METHODS =
[:comments, :id, :published, :updated, :category, :title, :content, :link,
:where, {:group => [:category, :content, :description, :keywords,
:player, :thumbnail, :title, {:duration=>:seconds}]}, :rating, {:statistics =>
[:favorite_count, :view_count]}]

Instance Method Summary collapse

Methods inherited from YoutubeObject

#recursive_hash_access

Constructor Details

#initialize(video) ⇒ Video

Returns a new instance of Video.



12
13
14
15
16
# File 'lib/basic_youtube/video.rb', line 12

def initialize video
  @dynamic_methods = {}
  @id = video.respond_to?(:keys) ? video["id"].split("/").last : video
  @entry = (video if video.respond_to?(:keys))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object (private)



73
74
75
76
77
78
79
# File 'lib/basic_youtube/video.rb', line 73

def method_missing method
  (entry;@dynamic_methods[method]) if @dynamic_methods.blank?

  return self.class.call_youtube(@dynamic_methods[method].split("videos/").last)["feed"]["entry"] if [:raw_video_responses,:raw_related_videos].include? method
  return send("raw_#{method}").map{|v| BasicYoutube::Video.new(v)} if [:video_responses,:related_videos].include? method
  @dynamic_methods[method] || super
end

Instance Method Details

#authorObject



38
39
40
# File 'lib/basic_youtube/video.rb', line 38

def author
  BasicYoutube::Channel.new(entry["author"]["name"])
end

#average_ratingObject



53
54
55
# File 'lib/basic_youtube/video.rb', line 53

def average_rating
  average = rating["average"].to_f
end

#dislikesObject



48
49
50
51
# File 'lib/basic_youtube/video.rb', line 48

def dislikes
  total = rating["numRaters"].to_i
  return total-likes
end

#dynamic_methodsObject



22
23
24
25
# File 'lib/basic_youtube/video.rb', line 22

def dynamic_methods
  h={};@dynamic_methods.each {|k,v| h[v.class].nil? ? h[v.class]=[k] : h[v.class] << k}
  return h
end

#entryObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/basic_youtube/video.rb', line 27

def entry
  @entry ||= self.class.call_youtube(@id)["entry"]
  if @dynamic_methods.blank?
    VALID_METHODS.each {|key| recursive_hash_access @dynamic_methods, @entry, key}
    define_links
  end
  return @entry
rescue MultiXml::ParseError
  raise InvalidVideo
end

#hoursObject



61
62
63
# File 'lib/basic_youtube/video.rb', line 61

def hours
  minutes/60
end

#idObject



18
19
20
# File 'lib/basic_youtube/video.rb', line 18

def id
  @id
end

#likesObject



42
43
44
45
46
# File 'lib/basic_youtube/video.rb', line 42

def likes
  average = rating["average"].to_f
  total = rating["numRaters"].to_i
  return (total*(average-1)/4).round
end

#minutesObject



57
58
59
# File 'lib/basic_youtube/video.rb', line 57

def minutes
  seconds.to_f/60
end