Class: OpenGraphCache

Inherits:
ApplicationRecord show all
Defined in:
app/models/open_graph_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create_by(opts) ⇒ Object


29
30
31
32
33
# File 'app/models/open_graph_cache.rb', line 29

def self.find_or_create_by(opts)
  cache = OpenGraphCache.find_or_initialize_by(opts)
  cache.fetch_and_save_opengraph_data! unless cache.persisted?
  cache if cache.persisted? # Make this an after create callback and drop this method ?
end

Instance Method Details

#fetch_and_save_opengraph_data!Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/open_graph_cache.rb', line 35

def fetch_and_save_opengraph_data!
  uri = URI.parse(url.start_with?("http") ? url : "http://#{url}")
  uri.normalize!
  object = OpenGraphReader.fetch!(uri)

  return unless object

  self.title = object.og.title.truncate(255)
  self.ob_type = object.og.type
  self.image = object.og.image.url
  self.url = object.og.url
  self.description = object.og.description
  if object.og.video.try(:secure_url) && secure_video_url?(object.og.video.secure_url)
    self.video_url = object.og.video.secure_url
  end

  self.save
rescue OpenGraphReader::NoOpenGraphDataError, OpenGraphReader::InvalidObjectError
end

#imageObject


21
22
23
24
25
26
27
# File 'app/models/open_graph_cache.rb', line 21

def image
  if AppConfig.privacy.camo.proxy_opengraph_thumbnails?
    Diaspora::Camo.image_url(self[:image])
  else
    self[:image]
  end
end

#secure_video_url?(url) ⇒ Boolean

Returns:

  • (Boolean)

55
56
57
# File 'app/models/open_graph_cache.rb', line 55

def secure_video_url?(url)
  SECURE_OPENGRAPH_VIDEO_URLS.any? {|u| u =~ url }
end