docbrown/app/view_objects/articles/social_image.rb
Anna Buianova 4c9a40be41 Dispatch events to the webhook endpoints #3715 (#3872)
* Start with webhooks: table and model

* Start with webhooks api

* Start with webhooks api

* Webhook::Event class and events list

* Remove commented callbacks

* Start with sending events to webhook endpoints

* A couple of tests for Articles::Creator

* Send event to webhook endpoint on article destroy

* Dispatch event on article update

* Dispatch event when an article updated from admin

* Spec for the webhook job

* One more test for the dispatch event job

* Integration-like spec for event dispatching to webhook endpoints when creating an article

* Use Addressable::URI to parse endpoint url

* Add oj as a faster fast_jsonapi backend

* Renamed serializers

* Move article serialization out of a model

* Don't allow to create a webhook event with invalid type

* Add fields for ArticleSerializer

* Fix webhook event job specs

* Specify timeout when dispatching events

* Fix webhook job spec

* Change webhook events queue name

* Change serialized article fields for the dispatchable events

* Include user data when serializing an article for webhook event

* Moved decorating out of Webhook::DispatchEvent, fixed most specs

* Fix route in ArticleSerializer

* Move Article decoration to Webhook::PayloadAdapter

* Refactor image url helpers to avoid including helpers into serializer

* Fix specs

* Default url options for production
2019-09-07 13:17:45 -04:00

63 lines
1.9 KiB
Ruby

module Articles
class SocialImage
include Rails.application.routes.url_helpers
include CloudinaryHelper
def initialize(article)
@article = article
end
SOCIAL_PREVIEW_MIGRATION_DATETIME = Time.zone.parse("2019-04-22T00:00:00Z")
def url
image = user_defined_image
if image.present?
return cl_image_path(image,
type: "fetch",
width: "1000",
height: "500",
crop: "imagga_scale",
quality: "auto",
flags: "progressive",
fetch_format: "auto",
sign_url: true)
end
return legacy_article_social_image unless use_new_social_url?
article_social_preview_url(article, format: :png)
end
private
attr_reader :article
def legacy_article_social_image
cache_key = "article-social-img-#{article}-#{article.updated_at}-#{article.comments_count}"
Rails.cache.fetch(cache_key, expires_in: 1.hour) do
src = GeneratedImage.new(article).social_image
return src if src.start_with? "https://res.cloudinary.com/"
cl_image_path(src,
type: "fetch",
width: "1000",
height: "500",
crop: "imagga_scale",
quality: "auto",
flags: "progressive",
fetch_format: "auto",
sign_url: true)
end
end
def use_new_social_url?
article.updated_at > SOCIAL_PREVIEW_MIGRATION_DATETIME
end
def user_defined_image
return article.social_image if article.social_image.present?
return article.main_image if article.main_image.present?
return article.video_thumbnail_url if article.video_thumbnail_url.present?
end
end
end