Class: Helios::Press::Api::PostsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/helios/press/api/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /api/press/posts Upsert a post by external_id. Payload shape: {

"external_id":       "helios-blog-post-draft-4821",
"title":             "...",
"slug":              "...",
"keywords":          "...",
"description":       "...",
"body_html":         "<p>Content with <img src=\"https://...\"> tags...</p>",
"published":         true,
"images":            [
  { "reference_key": "hero1", "url": "https://...", "alt": "...", "caption": "..." }
]

}

Images in body_html are automatically downloaded, stored via ActiveStorage, and their src attributes rewritten to local proxy URLs. Two modes:

1. Explicit: use src="helios://image/<reference_key>" in body_html and
   provide the download URL in the images array.
2. Auto: any src="https://..." in body_html is downloaded automatically.


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/helios/press/api/posts_controller.rb', line 25

def create
  external_id = params[:external_id].to_s
  return render json: { error: "external_id is required" }, status: :bad_request if external_id.blank?

  post = Post.find_or_initialize_by(external_id: external_id)

  post.assign_attributes(
    name: params[:title],
    slug: params[:slug].presence,
    keywords: params[:keywords],
    description: params[:description],
    published: ActiveModel::Type::Boolean.new.cast(params[:published])
  )

  post.save!

  if params[:body_html].present?
    ingestor = ImageIngestor.new(post)
    ingestor.call(
      body_html: params[:body_html],
      images_payload: params[:images] || []
    )
  end

  render json: {
    ok: true,
    id: post.id,
    slug: post.slug
  }, status: :ok
rescue ActiveRecord::RecordInvalid => e
  render json: { errors: e.record.errors.full_messages }, status: :unprocessable_entity
end