The job runs when the record is created and when the title or excerpt changes. It does not run on the HTML request, and attaching the file does not change those columns, so the callback does not enqueue itself again. Map author and published_at to your own schema. create_blog_post_image sets template to blog_post. Product, event, and the other helpers are on /docs.
image_type: "jpeg" is the documented claim for the delivered format. The JSON body does not include a content type, so the sample sets image/jpeg to match that claim. Attach only when status is completed and url is present. A 202 means the render is still going. Leave the current attachment and retry the job. The processing response sets Retry-After to 30.
iat is the record's updated_at. The docs say a present iat keeps the hosted cache for 24 hours from that timestamp. Omit it and the hosted cache does not expire on its own. Changing the title already changes the payload, so the next call is a different image either way. The API key stays in OG_PILOT_API_KEY. The browser never sees it.
To draw the card yourself, keep the same model, job, and attach call. Replace the gem call with the bytes from your library or browser. This page does not name that library, and it does not publish how long that render takes or what it costs to run.
Service, job, and invalidation
# Gemfile â version from /docs
gem "og_pilot_ruby", "~> 0.4.1"
# bundle install
# bin/rails g og_pilot_ruby:install
# Set OG_PILOT_API_KEY and OG_PILOT_DOMAIN on the server.
# The gem signs the JWT. Do not add a hand-rolled signer.
# app/models/post.rb
class Post < ApplicationRecord
has_one_attached :og_image
after_commit :refresh_og_image_later, if: :og_card_fields_changed?
private
def og_card_fields_changed?
previously_new_record? || saved_change_to_title? || saved_change_to_excerpt?
end
def refresh_og_image_later
RefreshPostOgImageJob.perform_later(self)
end
end
# app/jobs/refresh_post_og_image_job.rb
class RefreshPostOgImageJob < ApplicationJob
queue_as :default
def perform(post)
Posts::OgImage.attach(post)
end
end
# app/services/posts/og_image.rb
require "net/http"
require "stringio"
require "uri"
module Posts
class OgImage
def self.attach(post)
new(post).attach
end
def initialize(post)
@post = post
end
def attach
metadata = OgPilotRuby.create_blog_post_image(
title: post.title,
description: post.excerpt.to_s,
author_name: post.author&.name,
publish_date: post.published_at&.to_date&.iso8601,
path: "/posts/#{post.to_param}",
image_type: "jpeg",
iat: post.updated_at.to_i,
json: true
)
return unless metadata.is_a?(Hash)
return unless metadata["status"] == "completed"
return if metadata["url"].blank?
bytes = download(metadata["url"])
return if bytes.blank?
post.og_image.attach(
io: StringIO.new(bytes),
filename: "og-#{post.id}.jpg",
content_type: "image/jpeg"
)
end
private
attr_reader :post
def download(url, redirects = 3)
uri = URI.parse(url)
return unless uri.is_a?(URI::HTTPS)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.get(uri.request_uri)
end
if response.is_a?(Net::HTTPRedirection) && redirects.positive? && response["location"].present?
return download(URI.join(uri, response["location"]).to_s, redirects - 1)
end
response.body if response.is_a?(Net::HTTPSuccess)
end
end
end