Class: OEmbedCache

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) find_or_create_by_url(url)



14
15
16
17
18
19
# File 'app/models/o_embed_cache.rb', line 14

def self.find_or_create_by_url(url)
 cache = OEmbedCache.find_or_initialize_by_url(url)
 return cache if cache.persisted?
 cache.fetch_and_save_oembed_data!
 cache
end

Instance Method Details

- (Object) fetch_and_save_oembed_data!



21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/o_embed_cache.rb', line 21

def fetch_and_save_oembed_data!
  begin
    response = OEmbed::Providers.get(self.url, {:maxwidth => 420, :maxheight => 420, :frame => 1, :iframe => 1})
  rescue => e
    # noop
  else
    self.data = response.fields
    self.data['trusted_endpoint_url'] = response.provider.endpoint
    self.save
  end
end

- (Boolean) from_trusted?

Returns:

  • (Boolean)


37
38
39
# File 'app/models/o_embed_cache.rb', line 37

def from_trusted?
  SECURE_ENDPOINTS.include?(self.data['trusted_endpoint_url'])
end

- (Boolean) is_trusted_and_has_html?

Returns:

  • (Boolean)


33
34
35
# File 'app/models/o_embed_cache.rb', line 33

def is_trusted_and_has_html?
  self.from_trusted? and self.data.has_key?('html')
end

- (Object) options_hash(prefix = 'thumbnail_')



41
42
43
44
45
46
47
48
# File 'app/models/o_embed_cache.rb', line 41

def options_hash(prefix = 'thumbnail_')
  return nil unless self.data.has_key?(prefix + 'url')
  {
    :height => self.data.fetch(prefix + 'height', ''),
    :width => self.data.fetch(prefix + 'width', ''),
    :alt => self.data.fetch('title', ''),
  }
end