Move GeneratedImage to Images::GenerateSocialmage (#11691)
This commit is contained in:
parent
54937fa5a7
commit
195e228307
6 changed files with 60 additions and 52 deletions
|
|
@ -4,7 +4,7 @@ module SocialImageHelper
|
|||
SOCIAL_PREVIEW_MIGRATION_DATETIME = Time.zone.parse("2019-04-22T00:00:00Z")
|
||||
|
||||
def user_social_image_url(user)
|
||||
return GeneratedImage.new(user).social_image unless use_new_social_url?(user)
|
||||
return Images::GenerateSocialImage.call(user) unless use_new_social_url?(user)
|
||||
|
||||
if user.is_a?(Organization)
|
||||
organization_social_preview_url(user, format: :png)
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
class GeneratedImage
|
||||
attr_reader :resource
|
||||
|
||||
def initialize(resource)
|
||||
@resource = resource
|
||||
end
|
||||
|
||||
def social_image
|
||||
if resource.class.name.include?("Article")
|
||||
article_image
|
||||
elsif resource.instance_of?(User)
|
||||
optimize_image "/user/#{resource.id}?bust=#{resource.profile_image_url}"
|
||||
elsif resource.instance_of?(Organization)
|
||||
optimize_image "/organization/#{resource.id}?bust=#{resource.profile_image_url}"
|
||||
elsif resource.class.name.include?("Tag")
|
||||
optimize_image "/tag/#{@resource.id}?bust=#{@resource.pretty_name}"
|
||||
end
|
||||
end
|
||||
|
||||
def article_image
|
||||
return resource.social_image if resource.social_image.present?
|
||||
return resource.main_image if resource.main_image.present?
|
||||
return resource.video_thumbnail_url if resource.video_thumbnail_url.present?
|
||||
|
||||
path = "/article/#{resource.id}?bust=#{resource.comments_count}-#{resource.title}-#{resource.published}"
|
||||
optimize_image(path)
|
||||
end
|
||||
|
||||
def optimize_image(path)
|
||||
options = {
|
||||
height: 400,
|
||||
width: 800,
|
||||
gravity: "north",
|
||||
crop: "fill",
|
||||
type: "url2png",
|
||||
flags: nil,
|
||||
quality: nil,
|
||||
fetch_format: nil
|
||||
}
|
||||
|
||||
Images::Optimizer.call("https://dev.to/social_previews#{path}", options)
|
||||
end
|
||||
end
|
||||
51
app/services/images/generate_social_image.rb
Normal file
51
app/services/images/generate_social_image.rb
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
module Images
|
||||
class GenerateSocialImage
|
||||
def self.call(resource)
|
||||
new(resource).call
|
||||
end
|
||||
|
||||
def initialize(resource)
|
||||
@resource = resource
|
||||
end
|
||||
|
||||
def call
|
||||
if resource.class.name.include?("Article")
|
||||
article_image
|
||||
elsif resource.instance_of?(User)
|
||||
optimize_image "/user/#{resource.id}?bust=#{resource.profile_image_url}"
|
||||
elsif resource.instance_of?(Organization)
|
||||
optimize_image "/organization/#{resource.id}?bust=#{resource.profile_image_url}"
|
||||
elsif resource.class.name.include?("Tag")
|
||||
optimize_image "/tag/#{@resource.id}?bust=#{@resource.pretty_name}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :resource
|
||||
|
||||
def article_image
|
||||
return resource.social_image if resource.social_image.present?
|
||||
return resource.main_image if resource.main_image.present?
|
||||
return resource.video_thumbnail_url if resource.video_thumbnail_url.present?
|
||||
|
||||
path = "/article/#{resource.id}?bust=#{resource.comments_count}-#{resource.title}-#{resource.published}"
|
||||
optimize_image(path)
|
||||
end
|
||||
|
||||
def optimize_image(path)
|
||||
options = {
|
||||
height: 400,
|
||||
width: 800,
|
||||
gravity: "north",
|
||||
crop: "fill",
|
||||
type: "url2png",
|
||||
flags: nil,
|
||||
quality: nil,
|
||||
fetch_format: nil
|
||||
}
|
||||
|
||||
Images::Optimizer.call("https://dev.to/social_previews#{path}", options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -29,7 +29,7 @@ module Articles
|
|||
cache_key = "article-social-img-#{article}-#{article.updated_at.rfc3339}-#{article.comments_count}"
|
||||
|
||||
Rails.cache.fetch(cache_key, expires_in: 1.hour) do
|
||||
src = GeneratedImage.new(article).social_image
|
||||
src = Images::GenerateSocialImage.call(article)
|
||||
return src if src.start_with? "https://res.cloudinary.com/"
|
||||
|
||||
Images::Optimizer.call(src, width: "1000", height: "500", crop: "imagga_scale")
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ describe SocialImageHelper do
|
|||
|
||||
url = helper.user_social_image_url(user)
|
||||
|
||||
expect(url).to eq GeneratedImage.new(user).social_image
|
||||
expect(url).to eq Images::GenerateSocialImage.call(user)
|
||||
end
|
||||
|
||||
it "returns social preview path for newer decorated users" do
|
||||
|
|
@ -56,7 +56,7 @@ describe SocialImageHelper do
|
|||
|
||||
url = helper.article_social_image_url(article)
|
||||
|
||||
expect(url).to eq GeneratedImage.new(article).social_image
|
||||
expect(url).to eq Images::GenerateSocialImage.call(article)
|
||||
end
|
||||
|
||||
it "returns social preview path for newer decorated articles" do
|
||||
|
|
|
|||
|
|
@ -1,25 +1,25 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe GeneratedImage, type: :labor do
|
||||
RSpec.describe Images::GenerateSocialImage, type: :labor do
|
||||
let(:user) { create(:user) }
|
||||
let(:article) { create(:article, user_id: user.id) }
|
||||
|
||||
it "returns the social image url if there is a social image" do
|
||||
article.social_image = Faker::Avatar.image
|
||||
expect(described_class.new(article).social_image).to eq(article.social_image)
|
||||
expect(described_class.call(article)).to eq(article.social_image)
|
||||
end
|
||||
|
||||
it "returns the main image if there is a main image" do
|
||||
article.main_image = Faker::Avatar.image
|
||||
article.social_image = nil
|
||||
expect(described_class.new(article).social_image).to eq(article.main_image)
|
||||
expect(described_class.call(article)).to eq(article.main_image)
|
||||
end
|
||||
|
||||
it "returns article social image" do
|
||||
article.main_image = nil
|
||||
article.social_image = nil
|
||||
article.cached_tag_list = "discuss, hello, goodbye"
|
||||
expect(described_class.new(article).social_image).to include(
|
||||
expect(described_class.call(article)).to include(
|
||||
"article/#{article.id}",
|
||||
"image/url2png",
|
||||
"c_fill,g_north,h_400,w_800/",
|
||||
|
|
@ -29,7 +29,7 @@ RSpec.describe GeneratedImage, type: :labor do
|
|||
it "creates various generated images of different title lengths" do
|
||||
[25, 49, 79, 99, 105].each do |n|
|
||||
article.assign_attributes(title: "0" * n, main_image: nil, cached_tag_list: "discuss, hello")
|
||||
expect(described_class.new(article).social_image.include?(article.title)).to eq(true)
|
||||
expect(described_class.call(article).include?(article.title)).to eq(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue