Cosmetics

This commit is contained in:
François Turbelin
2020-04-15 22:43:23 +02:00
parent d9d218f661
commit b152e532d2
7 changed files with 60 additions and 34 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
DfcProvider::Engine.routes.draw do
namespace :api do
namespace :dfc_provider do

View File

@@ -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/**/*']

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
require "dfc_provider/engine"
module DfcProvider

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module DfcProvider
class Engine < ::Rails::Engine
isolate_namespace DfcProvider

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module DfcProvider
VERSION = "0.0.1"
VERSION = '0.0.1'
end