cache open graph response (#17157)

* Trigger Build

* Cache the HTML response for quicker previews

* remove unnecessary comments
This commit is contained in:
Dwight Scott 2022-04-07 13:53:20 -04:00 committed by GitHub
parent e4446a778c
commit dfdae481e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,9 +9,11 @@ class OpenGraph
attr_reader :page, :tags
DEFAULT_METHODS = %i[description title url].freeze
CACHE_EXPIRY_IN_MINUTES = 15
def initialize(url)
@page = MetaInspector.new(url)
html = fetch_html(url)
@page = MetaInspector.new(url, document: html)
@tags = meta_tags
end
@ -80,6 +82,12 @@ class OpenGraph
private
def fetch_html(url)
Rails.cache.fetch("#{url}_open_graph_html", expires_in: CACHE_EXPIRY_IN_MINUTES.minutes) do
Net::HTTP.get(URI(url))
end
end
def group(data)
data.each_with_object({}) do |(key, value), hash|
group_key = key.split(":").first