Class: Video

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/video.rb

Instance Method Summary collapse

Instance Method Details

#embed_html(maxwidth = nil) ⇒ Object



24
25
26
# File 'app/models/video.rb', line 24

def embed_html(maxwidth = nil)
  oembed_cached(maxwidth).html
end

#oembed_cached(maxwidth = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/video.rb', line 28

def oembed_cached(maxwidth = nil)
  Rails.cache.fetch(oembed_cache_key(maxwidth), :expires_in => 1.hour) do
    options = {}
    options["maxwidth"] = maxwidth if maxwidth

    provider = OEmbed::Providers.find(url)

    # If embedly is the chosen provider and is configured, set an api key in the options
    if provider && provider.endpoint.include?("embed.ly") && SpreeEmbeddedVideos::Config.embedly_api_key
      options["key"] = SpreeEmbeddedVideos::Config.embedly_api_key
    end

    response = nil
    if provider
      response = provider.get(url, options)
    end
    response.nil? ? nil : OpenStruct.new(response.fields).freeze
  end
end