Class: OpenGraphCache

Inherits:
ActiveRecord::Base
  • Object
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_url(url) ⇒ Object


24
25
26
27
28
# File 'app/models/open_graph_cache.rb', line 24

def self.find_or_create_by_url(url)
  cache = OpenGraphCache.find_or_initialize_by_url(url)
  cache.fetch_and_save_opengraph_data! unless cache.persisted?
  cache if cache.persisted?
end

Instance Method Details

#fetch_and_save_opengraph_data!Object


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/open_graph_cache.rb', line 30

def fetch_and_save_opengraph_data!
  response = OpenGraph.new(self.url)

  return if response.blank? || response.type.blank?

  self.title = response.title
  self.ob_type = response.type
  self.image = response.images[0]
  self.url = response.url
  self.description = response.description

  self.save
end