Add DFC Provider engine

This commit is contained in:
François Turbelin
2020-04-14 14:28:19 +02:00
parent d1392d400a
commit d9d218f661
10 changed files with 99 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ gem 'rails_safe_tasks', '~> 1.0'
gem "activerecord-import"
gem "catalog", path: "./engines/catalog"
gem 'dfc_provider', path: './engines/dfc_provider'
gem "order_management", path: "./engines/order_management"
gem 'web', path: './engines/web'

View File

@@ -56,6 +56,11 @@ GIT
rails-i18n
spree_core (>= 1.1)
PATH
remote: engines/dfc_provider
specs:
dfc_provider (0.0.1)
PATH
remote: engines/catalog
specs:
@@ -710,6 +715,7 @@ DEPENDENCIES
delayed_job_web
devise (~> 2.2.5)
devise-encryptable (= 0.2.0)
dfc_provider!
diffy
eventmachine (>= 1.2.3)
factory_bot_rails

View File

@@ -91,4 +91,5 @@ Openfoodnetwork::Application.routes.draw do
# Mount Spree's routes
mount Spree::Core::Engine, :at => '/'
mount DfcProvider::Engine, at: '/'
end

View File

@@ -0,0 +1,26 @@
module Api::DfcProvider
class ProductsController < ActionController::Base
before_filter :set_enterprise
def index
products = @enterprise.inventory_variants
.includes(:product, :inventory_items)
products_json = DfcProvider::ProductSerializer
.new(@enterprise, products, base_url)
.serialized_json
render json: products_json
end
private
def set_enterprise
@enterprise = ::Enterprise.find(params[:enterprise_id])
end
def base_url
"#{root_url}api/dfc_provider"
end
end
end

View File

@@ -0,0 +1,28 @@
module DfcProvider
class ProductSerializer
def initialize(enterprise, products, base_url)
@enterprise = enterprise
@products = products
@base_url = base_url
end
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
end
end
end

View File

@@ -0,0 +1,9 @@
DfcProvider::Engine.routes.draw do
namespace :api do
namespace :dfc_provider do
resources :enterprises, only: :none do
resources :products, only: %i[index]
end
end
end
end

View File

@@ -0,0 +1,16 @@
$:.push File.expand_path("../lib", __FILE__)
# Maintain your gem's version:
require "dfc_provider/version"
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = 'dfc_provider'
s.version = DfcProvider::VERSION
s.authors = ['Admin OFF']
s.email = ['admin@openfoodfrance.org']
s.summary = 'Provdes an API stack implementing DFC semantic specifications'
s.files = Dir["{app,config,db,lib}/**/*"] + ['MIT-LICENSE', 'Rakefile', 'README.rdoc']
s.test_files = Dir['test/**/*']
end

View File

@@ -0,0 +1,4 @@
require "dfc_provider/engine"
module DfcProvider
end

View File

@@ -0,0 +1,5 @@
module DfcProvider
class Engine < ::Rails::Engine
isolate_namespace DfcProvider
end
end

View File

@@ -0,0 +1,3 @@
module DfcProvider
VERSION = "0.0.1"
end