thibaudgg / video_info

Get video info from Dailymotion, Vimeo, Wistia, and YouTube URLs.

Home Page:https://rubygems.org/gems/video_info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add persistence so video are saved in db

olivattaque opened this issue · comments

Hi !

First thanks for your work, i'm using your gem in some of my projects :)
However I always implement persistence myself because I don't wan't to ask youtube everytime i go from one page to another and because video posts are the main purpose of my project so they are seen a lot.

However it present some problems : I have to do it every time in new projects, I have to maintain myself a method for getting the embed code depending on the provider, and I don't benefit from improvements you guys make on the gem (like new attributes etc).

Do you have any plan on adding persistence as an option for you gem ?
Thanks again for your work !

My class looks something like that :

class VideoInformation < ActiveRecord::Base
  def self.create_from_video_info(video_info, url)
    if VideoInformation.exists?(:video_url => url)
      video_information = VideoInformation.find_by_video_url(url)
    else
      video_info = VideoInformation.new(url)
      if video_info.available?
        video_information = self.new
        video_information.video_id = video_info.video_id
        video_information.provider = video_info.provider
        video_information.video_url = url
        video_information.title = video_info.title
        video_information.description = video_info.description
        video_information.keywords = video_info.keywords
        video_information.duration = video_info.duration
        video_information.thumbnail_small = video_info.thumbnail_small
        video_information.thumbnail_large = video_info.thumbnail_large
      else
        video_information = nil
      end
    end
    video_information
  end
end

How I use it :

video_information = VideoInformation.create_from_video_info(video_info, video_post_params[:video_url])
      if video_information.save
        @video_post = current_user.video_posts.build(:video_information_id => video_information.id)

And the helper to display embed code :

module VideoPostsHelper
  def show_embed_video(video_information, options={})
    width = options[:width] || 640
    height = options[:height] || 360
    frameborder = options[:frameborder] || 0
    if video_information.provider == "YouTube"
      %{<iframe width="#{width}" height="#{height}" src='//www.youtube.com/embed/#{video_information.video_id}' frameborder="#{frameborder}" allowfullscreen></iframe>}
    elsif video_information.provider == "Vimeo"
      %{<iframe src="http://player.vimeo.com/video/#{video_information.video_id}" width="#{width}" height="#{height}" frameborder="#{frameborder}" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>}
    end  
  end
end

I think this is out of the scope of this gem. You seem to be using Rails, but there are tons of other ways people talk to a database in Ruby besides ActiveRecord. It wouldn't be feasible to support everything, and wouldn't be reasonable just to support Rails.

That being said, I'd love to help make video_info easier for your use case.

I'm not terribly familiar with Rails, but what if we could provide the VideoInfo object as a Module, so you can just have include video_info or extend video_info in your VideoInformation class? Then you should be able to avoid all the assignment statements like video_information.video_id = video_info.video_id. Would that help?

Regarding the embed code helper, is there any reason you can't use #embed_code provided by video_info?

I think it's better to implement such functionality in another gem (something like video_info_rails).

ˋvideo_info-rails` please :-)

On 25 Mar 2016, at 07:08, Alexander Maslov notifications@github.com wrote:

I think it's better to implement such functionality in another gem (something like video_info_rails).


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub

Thank you for your kind and encouraging answers 👍

For embed_code I didn't want to store it in the database as it is suppose to be the same for all instance (with just the embed url changing, same for embed_url as it's just the id changing) and I can't access to it otherwise that means creating a VideoInfo on top of me getting my VideoInformation which doesn't make sense.

As I'm pretty new to ruby on rails I won't be able to tell you which solution would better fit my usecase :/
Maybe extend could be useful but I haven't use it yet so I really can't tell.
Another gem like video_info-rails would be nice to use I guess ^^

Actually the problematic for embed_code is pretty much the same for thumbnails also (static but depending on providers).
In my case I have to save them otherwise that would mean recoding the purpose of your gem into my application which is not ideal.
That's may be another good reason for video_info-rails so I can use the methods embed_url, thumbnail_small etc. from your classes

I'm going to close this issue. Actual persistence or Rails-specific support is outside the scope of the project.

Thinking back, the module idea I proposed isn't really doable because of how Ruby modules work (I forgot; whoops!)

A separate gem for Rails specific support may be started by someone, but I don't think I have the time/energy to consider doing it myself nor do I know what would belong in it outside of native ActiveRecord support.

That being said, I'd really like to make video_info less clunky for your use case. I just don't immediately see any good way to do it. If you have any ideas that won't tie it down to a framework, feel free to post them here (or just open up a new issue).