Class: Video

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/video.rb

Instance Method Summary collapse

Instance Method Details

#extract_youtube_codeObject

Atualmente validando URLs nos formatos: www.youtube.com/watch?v=9bZkp7q19f0 TODO adicionar formato youtu.be/9bZkp7q19f0



38
39
40
41
42
43
44
# File 'app/models/video.rb', line 38

def extract_youtube_code
  url = self.url

  url = URI(url)
  query = CGI.parse(url.query)
  query['v'].first
end

#iframe(params = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/models/video.rb', line 46

def iframe(params = {})
  defaults = {
    width: '100%',
    height: '400'
  }
  params = defaults.merge(params)
  "<iframe width='#{params[:width]}' height='#{params[:height]}' src='http://www.youtube-nocookie.com/embed/#{self.youtube_code}?rel=0' frameborder='0' allowfullscreen></iframe>"
end

#insert_youtube_dataObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/video.rb', line 16

def insert_youtube_data
  if self.errors.blank?
    yt_code = self.extract_youtube_code

    video = Youtube::Video.find(scope: yt_code, params: {v: '2'})
    video = video.entry.first

    self.title        = video.title
    self.description  = video.group.description
    self.seconds      = video.group.duration.seconds
    self.views        = video.statistics.viewCount
    self.likes        = video.rating.last.numLikes
    self.dislikes     = video.rating.last.numDislikes
    self.published_at = video.published.to_time
    self.youtube_code = yt_code
    self.category     = video.group.category
  end
end

#thumb(type = :p) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/video.rb', line 55

def thumb(type = :p)
  thumbs = {
    # 120x90
    p: 'http://i.ytimg.com/vi/:code/default.jpg',
    # 320x180
    m: 'http://i.ytimg.com/vi/:code/mqdefault.jpg',
    # 480x360
    g: 'http://i.ytimg.com/vi/:code/hqdefault.jpg',
    # 640x480
    gg: 'http://i.ytimg.com/vi/:code/sddefault.jpg'
  }

  thumbs[type].sub(':code', self.youtube_code)
end