mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Cosmetics
This commit is contained in:
@@ -1,26 +1,32 @@
|
||||
module Api::DfcProvider
|
||||
class ProductsController < ActionController::Base
|
||||
before_filter :set_enterprise
|
||||
# frozen_string_literal: true
|
||||
|
||||
def index
|
||||
products = @enterprise.inventory_variants
|
||||
.includes(:product, :inventory_items)
|
||||
# Controller used to provide the API products for the DFC application
|
||||
module Api
|
||||
module DfcProvider
|
||||
class ProductsController < Api::BaseController
|
||||
before_filter :set_enterprise
|
||||
|
||||
products_json = DfcProvider::ProductSerializer
|
||||
.new(@enterprise, products, base_url)
|
||||
.serialized_json
|
||||
def index
|
||||
products = @enterprise.
|
||||
inventory_variants.
|
||||
includes(:product, :inventory_items)
|
||||
|
||||
render json: products_json
|
||||
end
|
||||
products_json = ::DfcProvider::ProductSerializer.
|
||||
new(@enterprise, products, base_url).
|
||||
serialized_json
|
||||
|
||||
private
|
||||
render json: products_json
|
||||
end
|
||||
|
||||
def set_enterprise
|
||||
@enterprise = ::Enterprise.find(params[:enterprise_id])
|
||||
end
|
||||
private
|
||||
|
||||
def base_url
|
||||
"#{root_url}api/dfc_provider"
|
||||
def set_enterprise
|
||||
@enterprise = ::Enterprise.find(params[:enterprise_id])
|
||||
end
|
||||
|
||||
def base_url
|
||||
"#{root_url}api/dfc_provider"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Serializer used to render the products passed
|
||||
# into JSON-LD format based on DFC ontology
|
||||
module DfcProvider
|
||||
class ProductSerializer
|
||||
def initialize(enterprise, products, base_url)
|
||||
@@ -9,20 +13,26 @@ module DfcProvider
|
||||
def serialized_json
|
||||
{
|
||||
"@context" =>
|
||||
{
|
||||
"DFC" => "http://datafoodconsortium.org/ontologies/DFC_FullModel.owl#",
|
||||
"@base" => @base_url
|
||||
},
|
||||
"@id" => "/enterprises/#{@enterprise.id}/products",
|
||||
"DFC:supplies" => @products.map do |variant|
|
||||
{
|
||||
"DFC:description" => variant.name,
|
||||
"DFC:quantity" => variant.total_on_hand,
|
||||
"@id" => variant.id,
|
||||
"DFC:hasUnit" => {"@id" => "/unit/#{variant.unit_description.presence || 'piece' }"}
|
||||
}
|
||||
end
|
||||
}.to_json
|
||||
{
|
||||
"DFC" => "http://datafoodconsortium.org/ontologies/DFC_FullModel.owl#",
|
||||
"@base" => @base_url
|
||||
},
|
||||
"@id" => "/enterprises/#{@enterprise.id}/products",
|
||||
"DFC:supplies" => serialized_products
|
||||
}.to_json
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def serialized_products
|
||||
@products.map do |variant|
|
||||
{
|
||||
"DFC:description" => variant.name,
|
||||
"DFC:quantity" => variant.total_on_hand,
|
||||
"@id" => variant.id,
|
||||
"DFC:hasUnit" => { "@id" => "/unit/#{variant.unit_description.presence || 'piece'}" }
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
DfcProvider::Engine.routes.draw do
|
||||
namespace :api do
|
||||
namespace :dfc_provider do
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
$:.push File.expand_path("../lib", __FILE__)
|
||||
# frozen_string_literal: true
|
||||
|
||||
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
||||
|
||||
# Maintain your gem's version:
|
||||
require "dfc_provider/version"
|
||||
@@ -9,7 +11,7 @@ Gem::Specification.new do |s|
|
||||
s.version = DfcProvider::VERSION
|
||||
s.authors = ['Admin OFF']
|
||||
s.email = ['admin@openfoodfrance.org']
|
||||
s.summary = 'Provdes an API stack implementing DFC semantic specifications'
|
||||
s.summary = 'Provides an API stack implementing DFC semantic specifications'
|
||||
|
||||
s.files = Dir["{app,config,db,lib}/**/*"] + ['MIT-LICENSE', 'Rakefile', 'README.rdoc']
|
||||
s.test_files = Dir['test/**/*']
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "dfc_provider/engine"
|
||||
|
||||
module DfcProvider
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DfcProvider
|
||||
class Engine < ::Rails::Engine
|
||||
isolate_namespace DfcProvider
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DfcProvider
|
||||
VERSION = "0.0.1"
|
||||
VERSION = '0.0.1'
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user