Module: YoutubeTitles
- Included in:
- Comment, StatusMessage
- Defined in:
- lib/youtube_titles.rb
Constant Summary
- YOUTUBE_ID_REGEX =
/(?:https?:\/\/)(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})(?:\S*(#[^ ]+)|\S+)?/im unless defined? YOUTUBE_ID_REGEX
Class Method Summary (collapse)
Instance Method Summary (collapse)
Class Method Details
+ (Object) included(model)
2 3 4 5 6 7 8 |
# File 'lib/youtube_titles.rb', line 2 def self.included(model) model.class_eval do before_save do get_youtube_title text end end if model.respond_to?(:before_save) end |
Instance Method Details
- (Object) get_youtube_title(text)
21 22 23 24 25 26 27 28 29 |
# File 'lib/youtube_titles.rb', line 21 def get_youtube_title text youtube_match = text.enum_for(:scan, YOUTUBE_ID_REGEX).map { Regexp.last_match } return if youtube_match.empty? self.youtube_titles ||= {} youtube_match.each do |match_data| self.youtube_titles[match_data[1]] = CGI::escape(youtube_title_for(match_data[1])) end end |
- (Object) youtube_title_for(video_id)
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/youtube_titles.rb', line 10 def youtube_title_for video_id http = Net::HTTP.new('gdata.youtube.com', 80) path = "/feeds/api/videos/#{video_id}?v=2" resp, data = http.get(path, nil) title = data.match(/<title>(.*)<\/title>/) unless title.nil? title = title.to_s[7..-9] end title || I18n.t('application.helper.video_title.unknown') end |