Class: Api::V1::Openai::ProductsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/openai/products_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



2
3
4
5
6
7
8
9
10
11
12
# File 'app/controllers/api/v1/openai/products_controller.rb', line 2

def index
  if locale = get_locale
    catalog = Catalog.locale_to_catalog(locale)
    response.headers['Content-Type'] = 'application/json'
    self.response_body = OpenaiFeed.full_json(catalog).to_json
  else
    response.headers['Content-Type'] = 'application/json'
    self.status = 404
    self.response_body = { error: 'Locale not set' }.to_json
  end
end

#showObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/api/v1/openai/products_controller.rb', line 14

def show
  if locale = get_locale
    catalog = Catalog.locale_to_catalog(locale)
    catalog_item_id = params[:id]

    openai_feed = OpenaiFeed.find_by(catalog_id: catalog.id, catalog_item_id: catalog_item_id)

    if openai_feed
      response.headers['Content-Type'] = 'application/json'
      self.response_body = {
        feed_info: {
          title: "#{catalog.company.legal_name} Product Feed - Item #{catalog_item_id}",
          description: 'Individual product data for OpenAI Agentic Commerce',
          last_updated: openai_feed.updated_at.iso8601,
          catalog_item_id: catalog_item_id,
          locale: locale
        },
        product: openai_feed.json_data
      }.to_json
    else
      response.headers['Content-Type'] = 'application/json'
      self.status = 404
      self.response_body = {
        error: 'Product not found',
        catalog_item_id: catalog_item_id,
        locale: locale
      }.to_json
    end
  else
    response.headers['Content-Type'] = 'application/json'
    self.status = 404
    self.response_body = { error: 'Locale not set' }.to_json
  end
end