lib/video_info/providers/vimeo.rb in video_info-2.4.2 vs lib/video_info/providers/vimeo.rb in video_info-2.5.0

- old
+ new

@@ -7,54 +7,122 @@ def provider 'Vimeo' end - %w[title description thumbnail_small thumbnail_medium thumbnail_large].each do |method| + def api_key + VideoInfo.provider_api_keys[:vimeo] + end + + %w[description].each do |method| define_method(method) { _video[method] } end %w[duration width height].each do |method| define_method(method) { _video[method].to_i } end + def title + _video['name'] + end + + def author + _video['user']['name'] + end + + def author_thumbnail_id + author_uri = _video['user']['pictures']['uri'] + @author_thumbnail_id ||= _parse_picture_id(author_uri) + end + + def author_thumbnail(width = 75) + "https://i.vimeocdn.com/portrait/#{author_thumbnail_id}_#{width}x#{width}.jpg" + end + + def thumbnail_id + @thumbnail_id ||= _parse_picture_id(_video['pictures']['uri']) + end + + def thumbnail(width = 200, height = nil) + base_uri = "https://i.vimeocdn.com/video/#{thumbnail_id}" + height ? base_uri + "_#{width}x#{height}.jpg" : base_uri + "_#{width}.jpg" + end + + def thumbnail_small + thumbnail(100, 75) + end + + def thumbnail_medium + thumbnail(200, 150) + end + + def thumbnail_large + thumbnail(640) + end + def keywords - _video['tags'] + keywords_array.join(', ') end + def keywords_array + _video['tags'].map { |t| t['tag'] } + end + def embed_url "//player.vimeo.com/video/#{video_id}" end def date - Time.parse(_video['upload_date'], Time.now.utc).utc + Time.parse(_video['created_time'], Time.now.utc).utc end def view_count - _video['stats_number_of_plays'].to_i + _video['stats']['plays'].to_i end private + def _clean_options(options) + headers = [super, _authorization_headers, _api_version_headers] + headers.inject(&:merge) + end + + def _api_version + '3.2' + end + + def _authorization_headers + { 'Authorization' => "bearer #{api_key}" } + end + + def _api_version_headers + { 'Accept' => "application/vnd.vimeo.*+json;version=#{_api_version}" } + end + def _video - data && data.first + data end def _url_regex - /.*\.com\/(?:(?:groups\/[^\/]+\/videos\/)|(?:ondemand|channels)(?:(?:\/less\/)|(?:\/\w*\/))|(?:video\/))?([0-9]+).*$/ + /.*\.com&& + |\/(?:(?:groups\/[^\/]+\/videos\/) + |(?:ondemand|channels)(?:(?:\/less\/) + |(?:user[0-9]+\/review\/)?([0-9]+).* + |(?:\/\w*\/))|(?:video\/))?([0-9]+).*$ + /x end def _api_base - 'vimeo.com' + 'api.vimeo.com' end def _api_path - "/api/v2/video/#{video_id}.json" + "/videos/#{video_id}" end def _api_url - "http://#{_api_base}#{_api_path}" + "https://#{_api_base}#{_api_path}" end def _default_iframe_attributes {} end @@ -62,9 +130,13 @@ def _default_url_attributes { title: 0, byline: 0, portrait: 0, autoplay: 0 } + end + + def _parse_picture_id(uri) + /\/pictures\/(\d+)/.match(uri)[1] end end end end