From d822d8d7d99ce1b9a1c65e50bf8e5a9be3185bda Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Tue, 27 Oct 2020 08:54:45 -0400 Subject: [PATCH] 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 --- app/lib/html_css_to_image.rb | 10 ++++++---- app/views/pages/show.html.erb | 4 ++-- spec/lib/html_css_to_image_spec.rb | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/lib/html_css_to_image.rb b/app/lib/html_css_to_image.rb index d9da9bbef..5561ccdf7 100644 --- a/app/lib/html_css_to_image.rb +++ b/app/lib/html_css_to_image.rb @@ -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 diff --git a/app/views/pages/show.html.erb b/app/views/pages/show.html.erb index e8d818cf1..5defd7507 100644 --- a/app/views/pages/show.html.erb +++ b/app/views/pages/show.html.erb @@ -7,7 +7,7 @@ - " /> + @@ -15,7 +15,7 @@ "> - "> + <% end %> <% if @page.template == "contained" %> diff --git a/spec/lib/html_css_to_image_spec.rb b/spec/lib/html_css_to_image_spec.rb index 87b1e14fb..5037449df 100644 --- a/spec/lib/html_css_to_image_spec.rb +++ b/spec/lib/html_css_to_image_spec.rb @@ -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