docbrown/app/lib/html_css_to_image.rb
Michael Kohl 6dfabd578f
Rename SiteConfig to Settings::General (#13573)
* Rename SiteConfig

* More renaming

* Update spec

* Update mandatory settings mapping

* More renaming

* e2e test fixes

* You have a rename, and you have a rename

* Spec fix

* More changes

* Temporarily disable specs

* After-merge update

* Undo rename for migration

* undo rename of DUS

* Fix DUS

* Fix merge problem

* Remove redundant DUS

* Fix specs

* Remove unused code

* Change wrong class name

* More cleanup

* Re-add missing values to constant

* Fix constant

* Fix spec

* Remove obsolete fields

* Add accidentally removed field

* Update spec

* Move methods from Settings::General to ForemInstance

* Remove unneeded model

* Change mentions of 'site config'
2021-05-21 14:45:37 +02:00

32 lines
962 B
Ruby

module HtmlCssToImage
AUTH = { username: ApplicationConfig["HCTI_API_USER_ID"],
password: ApplicationConfig["HCTI_API_KEY"] }.freeze
CACHE_EXPIRATION = 6.weeks
def self.url(html:, css: nil, google_fonts: nil)
image = HTTParty.post("https://hcti.io/v1/image",
body: { html: html, css: css, google_fonts: google_fonts },
basic_auth: AUTH)
image["url"] || fallback_image
end
def self.fetch_url(html:, css: nil, google_fonts: nil)
cache_key = "htmlcssimage/#{html}/#{css}/#{google_fonts}"
cached_url = Rails.cache.read(cache_key)
return cached_url if cached_url.present?
image_url = url(html: html, css: css, google_fonts: google_fonts)
unless image_url == fallback_image
Rails.cache.write(cache_key, image_url, expires_in: CACHE_EXPIRATION)
end
image_url
end
def self.fallback_image
Settings::General.main_social_image.to_s
end
end