mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add DFC Provider engine
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -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'
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -91,4 +91,5 @@ Openfoodnetwork::Application.routes.draw do
|
||||
|
||||
# Mount Spree's routes
|
||||
mount Spree::Core::Engine, :at => '/'
|
||||
mount DfcProvider::Engine, at: '/'
|
||||
end
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
9
engines/dfc_provider/config/routes.rb
Normal file
9
engines/dfc_provider/config/routes.rb
Normal 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
|
||||
16
engines/dfc_provider/dfc_provider.gemspec
Normal file
16
engines/dfc_provider/dfc_provider.gemspec
Normal 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
|
||||
4
engines/dfc_provider/lib/dfc_provider.rb
Normal file
4
engines/dfc_provider/lib/dfc_provider.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
require "dfc_provider/engine"
|
||||
|
||||
module DfcProvider
|
||||
end
|
||||
5
engines/dfc_provider/lib/dfc_provider/engine.rb
Normal file
5
engines/dfc_provider/lib/dfc_provider/engine.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
module DfcProvider
|
||||
class Engine < ::Rails::Engine
|
||||
isolate_namespace DfcProvider
|
||||
end
|
||||
end
|
||||
3
engines/dfc_provider/lib/dfc_provider/version.rb
Normal file
3
engines/dfc_provider/lib/dfc_provider/version.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
module DfcProvider
|
||||
VERSION = "0.0.1"
|
||||
end
|
||||
Reference in New Issue
Block a user