Sha256: 1443b49fa9455854e5921bcab147301a68f2310b4568d6fda5ada5e9eba49a3c

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'active_support/all'
require 'embeddable/version'

module Embeddable
  extend ActiveSupport::Concern

  SERVICES = {
    youtube: [
      %r{^https?://(?:(?:www|m)\.)?youtube\.com/watch\?v=([^&]+)},
      %r{^https?://(?:(?:www|m)\.)?youtu\.be/([^?]+)}
    ],
    vimeo: [
      %r{^https?://(?:www\.)?vimeo\.com/([^\?]+)},
    ],
    dailymotion: [
      %r{^https?://(?:www\.)?dailymotion\.com/video/([^\?]+)},
    ],
    veoh: [
      %r{^https?://(?:www\.)?veoh\.com/watch/([^\?]+)},
    ],
    vippy: [
      %r{https:\/\/vippy.co\/play\/.+\/([^\?\s]+)"}
    ],
    liveleak: [
      %r{^https?://(?:www\.)?liveleak\.com/view\?i=([^\?]+)},
    ]
  }

  module ClassMethods
    def embeddable(name, options = {})
      source = options.fetch :from

      define_method "#{name}_type" do
        url = send(source)
        return if url.blank?

        SERVICES.map do |service, patterns|
          service if patterns.any? { |pattern| url[pattern] }
        end.compact.first
      end

      define_method "#{name}_id" do
        url = send(source)
        return if url.blank?

        SERVICES.map do |service, patterns|
          patterns.map { |pattern| url[pattern, 1] }
        end.flatten.compact.first
      end

      SERVICES.each do |service, pattern|

        define_method "#{name}_on_#{service}?" do
          send("#{name}_type") == service
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
embeddable-0.0.5 lib/embeddable.rb