Fall back to configured social image instead of hardcoded (#9747)
* Fall back to configured social image instead of hardcoded * Move from constant to method for config inclusion * Fix spec * Fix test
This commit is contained in:
parent
b8f6779ade
commit
d822d8d7d9
3 changed files with 11 additions and 8 deletions
|
|
@ -2,8 +2,6 @@ module HtmlCssToImage
|
|||
AUTH = { username: ApplicationConfig["HCTI_API_USER_ID"],
|
||||
password: ApplicationConfig["HCTI_API_KEY"] }.freeze
|
||||
|
||||
FALLBACK_IMAGE = "https://thepracticaldev.s3.amazonaws.com/i/g355ol6qsrg0j2mhngz9.png".freeze
|
||||
|
||||
CACHE_EXPIRATION = 6.weeks
|
||||
|
||||
def self.url(html:, css: nil, google_fonts: nil)
|
||||
|
|
@ -11,7 +9,7 @@ module HtmlCssToImage
|
|||
body: { html: html, css: css, google_fonts: google_fonts },
|
||||
basic_auth: AUTH)
|
||||
|
||||
image["url"] || FALLBACK_IMAGE
|
||||
image["url"] || fallback_image
|
||||
end
|
||||
|
||||
def self.fetch_url(html:, css: nil, google_fonts: nil)
|
||||
|
|
@ -21,10 +19,14 @@ module HtmlCssToImage
|
|||
return cached_url if cached_url.present?
|
||||
|
||||
image_url = url(html: html, css: css, google_fonts: google_fonts)
|
||||
unless image_url == FALLBACK_IMAGE
|
||||
unless image_url == fallback_image
|
||||
Rails.cache.write(cache_key, image_url, expires_in: CACHE_EXPIRATION)
|
||||
end
|
||||
|
||||
image_url
|
||||
end
|
||||
|
||||
def self.fallback_image
|
||||
SiteConfig.main_social_image.to_s
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
<meta property="og:type" content="article" />
|
||||
<meta property="og:url" content="<%= app_url(@page.path) %>" />
|
||||
<meta property="og:title" content="<%= @page.title %> — <%= community_qualified_name %>" />
|
||||
<meta property="og:image" content="<%= @page.social_image_url || "https://thepracticaldev.s3.amazonaws.com/i/g355ol6qsrg0j2mhngz9.png" %>" />
|
||||
<meta property="og:image" content="<%= @page.social_image_url || SiteConfig.main_social_image %>" />
|
||||
<meta property="og:description" content="<%= @page.description %>" />
|
||||
<meta property="og:site_name" content="<%= community_qualified_name %>" />
|
||||
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
<meta name="twitter:site" content="@<%= SiteConfig.social_media_handles["twitter"] %>">
|
||||
<meta name="twitter:title" content="<%= @page.title %> — <%= community_qualified_name %>">
|
||||
<meta name="twitter:description" content="<%= @page.description %>">
|
||||
<meta name="twitter:image:src" content="<%= @page.social_image_url || "https://thepracticaldev.s3.amazonaws.com/i/g355ol6qsrg0j2mhngz9.png" %>">
|
||||
<meta name="twitter:image:src" content="<%= @page.social_image_url || SiteConfig.main_social_image %>">
|
||||
<% end %>
|
||||
|
||||
<% if @page.template == "contained" %>
|
||||
|
|
|
|||
|
|
@ -17,12 +17,13 @@ RSpec.describe HtmlCssToImage, type: :lib do
|
|||
body: '{ "error": "Plan limit exceeded" }',
|
||||
headers: { "Content-Type" => "application/json" })
|
||||
|
||||
expect(described_class.url(html: "test")).to eq described_class::FALLBACK_IMAGE
|
||||
expect(described_class.url(html: "test")).to eq described_class.fallback_image
|
||||
end
|
||||
end
|
||||
|
||||
describe ".fetch_url" do
|
||||
before do
|
||||
allow(SiteConfig).to receive(:main_social_image).and_return("https://testimage.com/image.png")
|
||||
allow(Rails.cache).to receive(:write)
|
||||
allow(Rails.cache).to receive(:read)
|
||||
end
|
||||
|
|
@ -57,7 +58,7 @@ RSpec.describe HtmlCssToImage, type: :lib do
|
|||
body: '{ "error": "Plan limit exceeded" }',
|
||||
headers: { "Content-Type" => "application/json" })
|
||||
|
||||
expect(described_class.fetch_url(html: "test")).to eq described_class::FALLBACK_IMAGE
|
||||
expect(described_class.fetch_url(html: "test")).to eq described_class.fallback_image
|
||||
expect(Rails.cache).not_to have_received(:write)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue