Class: Cinch::Plugins::Vimeo

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/vimeo.rb

Constant Summary collapse

VIMEO_VIDEO_REGEXP =
/https?:\/\/(?:[a-zA-Z]{2,3}\.)?(?:vimeo\.com\/)(\d+)/i
VIMEO_API_VIDEO_URL =
"http://vimeo.com/api/v2/video/%s.json"

Instance Method Summary collapse

Instance Method Details

#listen_to_channel(m) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cinch/plugins/vimeo.rb', line 15

def listen_to_channel(m)
  return if is_channel_disabled?(m.channel)
  return unless m.message.match(VIMEO_VIDEO_REGEXP)
  videos = m.message.scan(VIMEO_VIDEO_REGEXP).flatten.reject(&:'nil?').uniq

  videos.each {|v|
    response = HTTParty.get(VIMEO_API_VIDEO_URL % v, headers: {'User-Agent' => "HTTParty/#{HTTParty::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION}"})
    #raise StandardError, '%s - %s (%d)' % [response['error']['message'], v, response['error']['code']] if response['error']
    video = response[0]
    m.reply "#{Format(:bold,'Vimeo »')} #{Format(:purple,'%<title>s')} (%<length>s) · by %<uploader>s on %<uploaded>s · #{Format(:green,'☝%<likes>s')} · %<views>s views" % {
      title: video['title'],
      uploader: video['user_name'],
      uploaded: Time.parse(video['upload_date']).strftime('%F'),
      length: seconds_to_time(video['duration'].to_i),
      likes: commify_numbers(video['stats_number_of_likes'].to_i),
      views: commify_numbers(video['stats_number_of_plays'].to_i)
    }
  }
rescue => e
  m.reply "#{Format(:bold,'Vimeo »')} #{e.message}"
end