10000 Add extensions to DataPackage for Manyfold-specific data by Floppy · Pull Request #3923 · manyfold3d/manyfold · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add extensions to DataPackage for Manyfold-specific data #3923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 4, 2025
22 changes: 22 additions & 0 deletions app/deserializers/data_package/collection_deserializer.rb
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module DataPackage
class CollectionDeserializer < BaseDeserializer
def deserialize
return unless @object
attributes = {
name: @object["title"],
caption: @object["caption"],
notes: @object["description"]
}
begin
route_options = Rails.application.routes.recognize_path(@object["path"])
if route_options[:controller] == "collections"
attributes[:id] = Collection.find_param(route_options[:id]).id
end
rescue ActionController::RoutingError, ActiveRecord::RecordNotFound
end
attributes[:links_attributes] = @object["links"]&.map { |it| LinkDeserializer.new(it).deserialize } || []
attributes[:links_attributes] << {url: @object["path"]} unless attributes.has_key?(:id)
attributes.compact
end
end
end
11 changes: 8 additions & 3 deletions app/deserializers/data_package/creator_deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ module DataPackage
class CreatorDeserializer < BaseDeserializer
def deserialize
return unless @object && @object["roles"]&.include?("creator")
attributes = {name: @object["title"]}
attributes = {
name: @object["title"],
caption: @object["caption"],
notes: @object["description"]
}
begin
route_options = Rails.application.routes.recognize_path(@object["path"])
if route_options[:controller] == "creators"
attributes[:id] = Creator.find_param(route_options[:id]).id
end
rescue ActionController::RoutingError, ActiveRecord::RecordNotFound
end
attributes[:links_attributes] = [{url: @object["path"]}] unless attributes.has_key?(:creator_id)
attributes
attributes[:links_attributes] = @object["links"]&.map { |it| LinkDeserializer.new(it).deserialize } || []
attributes[:links_attributes] << {url: @object["path"]} unless attributes.has_key?(:id)
attributes.compact
end
end
end
10 changes: 10 additions & 0 deletions app/deserializers/data_package/link_deserializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module DataPackage
class LinkDeserializer < BaseDeserializer
def deserialize
return unless @object
{
url: @object["path"]
}.compact
end
end
end
18 changes: 14 additions & 4 deletions app/deserializers/data_package/model_deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ class ModelDeserializer < BaseDeserializer
def deserialize
{
name: @object["title"],
caption: @object["description"].split("\n\n", 2).first,
notes: @object["description"].split("\n\n", 2).last,
links_attributes: [{url: @object["homepage"]}],
caption: @object["caption"],
notes: @object["description"],
links_attributes: parse_links,
preview_file: @object["image"],
tag_list: @object["keywords"],
sensitive: @object["sensitive"],
license: @object.dig("licenses", 0, "name"),
model_files: @object["resources"]&.map { |it| ModelFileDeserializer.new(it).deserialize },
creator: CreatorDeserializer.new(@object["contributors"]&.find { |it| it["roles"].include?("creator") }).deserialize
creator: CreatorDeserializer.new(@object["contributors"]&.find { |it| it["roles"].include?("creator") }).deserialize,
collection: CollectionDeserializer.new(@object.dig("collections", 0)).deserialize
}.compact
end

private

def parse_links
links = (@object["links"] || []).map { |it| LinkDeserializer.new(it).deserialize }
links << {url: @object["homepage"]} if @object["homepage"]
links
end
end
end
8 changes: 6 additions & 2 deletions app/deserializers/data_package/model_file_deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ class ModelFileDeserializer < BaseDeserializer
def deserialize
{
filename: @object["path"],
mime_type: @object["mediatype"]
}
mime_type: @object["mediatype"],
caption: @object["caption"],
notes: @object["description"],
presupported: @object["presupported"],
y_up: (@object["up"] == "+y")
}.compact
end
end
end
13 changes: 13 additions & 0 deletions app/serializers/data_package/collection_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module DataPackage
class CollectionSerializer < BaseSerializer
def serialize
{
title: @object.name,
path: Rails.application.routes.url_helpers.url_for(@object),
caption: @object.caption,
description: @object.notes,
links: @object.links.map { |it| LinkSerializer.new(it).serialize }
}.compact
end
end
end
7 changes: 5 additions & 2 deletions app/serializers/data_package/creator_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ def serialize
{
title: @object.name,
path: Rails.application.routes.url_helpers.url_for(@object),
roles: ["creator"]
}
roles: ["creator"],
caption: @object.caption,
description: @object.notes,
links: @object.links.map { |it| LinkSerializer.new(it).serialize }
}.compact
end
end
end
9 changes: 9 additions & 0 deletions app/serializers/data_package/link_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module DataPackage
class LinkSerializer < BaseSerializer
def serialize
{
path: @object.url
}.compact
end
end
end
8 changes: 6 additions & 2 deletions app/serializers/data_package/model_file_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ def serialize
{
name: @object.basename.parameterize,
path: @object.filename,
mediatype: @object.mime_type
}
mediatype: @object.mime_type,
caption: @object.caption,
description: @object.notes,
up: @object.y_up ? "+y" : "+z",
presupported: @object.presupported
}.compact
end
end
end
9 changes: 7 additions & 2 deletions app/serializers/data_package/model_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module DataPackage
class ModelSerializer < BaseSerializer
def serialize
{
"$schema": "https://manyfold.app/profiles/0.0/datapackage.json",
name: @object.name.parameterize,
title: @object.name,
description: [@object.caption, @object.notes].compact.join("\n\n"),
caption: @object.caption,
description: @object.notes,
homepage: Rails.application.routes.url_helpers.url_for(@object),
image: @object.preview_file&.is_image? ? @object.preview_file.filename : nil,
keywords: @object.tag_list,
Expand All @@ -15,7 +17,10 @@ def serialize
}.compact
] : nil),
resources: @object.model_files.filter_map { |it| ModelFileSerializer.new(it).serialize },
contributors: @object.creator ? [CreatorSerializer.new(@object.creator).serialize] : nil
sensitive: @object.sensitive,
contributors: @object.creator ? [CreatorSerializer.new(@object.creator).serialize] : nil,
collections: @object.collection ? [CollectionSerializer.new(@object.collection).serialize] : nil,
links: @object.links.map { |it| LinkSerializer.new(it).serialize }
}.compact
end
end
Expand Down
70 changes: 70 additions & 0 deletions spec/deserializers/data_package/collection_deserializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require "rails_helper"

RSpec.describe DataPackage::CollectionDeserializer do
context "when parsing a Data Package" do
subject(:deserializer) { described_class.new(object) }

let(:output) { deserializer.deserialize }

context "with a valid collection linked to this server" do
let(:collection) { create(:collection) }
let(:object) do
{
"title" => collection.name,
"path" => "http://localhost:3214/collections/#{collection.to_param}",
"caption" => "caption goes here",
"description" => "description goes here",
"links" => [
{
"path" => "http://example.com"
}
]
}
end

it "parses name" do
expect(output[:name]).to eq collection.name
end

it "matches collection ID" do
expect(output[:id]).to eq collection.id
end

it "does not add main detected path as link" do
expect(output[:links_attributes]).not_to include({url: "http://localhost:3214/collections/#{collection.to_param}"})
end

it "parses links" do
expect(output[:links_attributes]).to include({url: "http://example.com"})
end

it "parses notes" do
expect(output[:notes]).to eq "description goes here"
end

it "parses caption" do
expect(output[:caption]).to eq "caption goes here"
end
end

context "with a valid collection hosted elsewhere" do
let(:object) do
{
"title" => "Bruce Wayne",
"path" => "http://example.com/bruce-wayne",
"roles" => ["collection"]
}
end

it "parses name" do
expect(output[:name]).to eq "Bruce Wayne"
end

it "includes main path as link" do
expect(output[:links_attributes]).to include({
url: "http://example.com/bruce-wayne"
})
end
end
end
end
27 changes: 22 additions & 5 deletions spec/deserializers/data_package/creator_deserializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
{
"title" => creator.name,
"path" => "http://localhost:3214/creators/#{creator.to_param}",
"roles" => ["creator"]
"roles" => ["creator"],
"caption" => "caption goes here",
"description" => "description goes here",
"links" => [
{
"path" => "http://example.com"
}
]
}
end

Expand All @@ -24,11 +31,21 @@
expect(output[:id]).to eq creator.id
end

it "parses links"
it "does not add main detected path as link" do
expect(output[:links_attributes]).not_to include({url: "http://localhost:3214/creators/#{creator.to_param}"})
end

it "parses links" do
expect(output[:links_attributes]).to include({url: "http://example.com"})
end

it "parses notes"
it "parses notes" do
expect(output[:notes]).to eq "description goes here"
end

it "parses caption"
it "parses caption" do
expect(output[:caption]).to eq "caption goes here"
end
end

context "with a valid creator hosted elsewhere" do
Expand All @@ -44,7 +61,7 @@
expect(output[:name]).to eq "Bruce Wayne"
end

it "extracts links" do
it "includes main path as link" do
expect(output[:links_attributes]).to include({
url: "http://example.com/bruce-wayne"
})
Expand Down
18 changes: 18 additions & 0 deletions spec/deserializers/data_package/link_deserializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "rails_helper"

RSpec.describe DataPackage::LinkDeserializer do
context "when parsing a Data Package" do
subject(:deserializer) { described_class.new(object) }

let(:output) { deserializer.deserialize }
let(:object) do
{
"path" => "http://example.com"
}
end

it "parses url" do
expect(output[:url]).to eq "http://example.com"
end
end
end
36 changes: 31 additions & 5 deletions spec/deserializers/data_package/model_deserializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"homepage" => "https://example.com",
"image" => "images/pic.png",
"keywords" => ["fantasy", "wizard"],
"description" => "caption\n\nand multiline\nnote",
"caption" => "caption",
"description" => "multiline\nnote",
"licenses" => [
{
"name" => "MIT",
Expand All @@ -31,7 +32,19 @@
"path" => "http://localhost:3214/creators/bruce-wayne",
"roles" => ["creator"]
}
]
],
"collections" => [
{
"title" => "Wonderful Toys",
"path" => "http://localhost:3214/collections/abc123"
}
],
"links" => [
{
"path" => "https://example.com/other-link"
}
],
"sensitive" => true
}
end

Expand All @@ -44,14 +57,16 @@
end

it "parses notes" do
expect(output[:notes]).to eq "and multiline\nnote"
expect(output[:notes]).to eq "multiline\nnote"
end

it "parses homepage link" do
expect(output[:links_attributes]).to include({url: "https://example.com"})
end

it "parses other links"
it "parses other links" do
expect(output[:links_attributes]).to include({url: "https://example.com/other-link"})
end

it "parses preview image" do
expect(output[:preview_file]).to eq "images/pic.png"
Expand All @@ -78,6 +93,17 @@
expect(output.dig(:creator, :links_attributes, 0, :url)).to eq "http://localhost:3214/creators/bruce-wayne"
end

it "parses collection"
it "parses collection ID if collection exists" do
collection = create(:collection, name: "Wonderful Toys", public_id: "abc123")
expect(output.dig(:collection, :id)).to eq collection.id
end

it "parses collection link if collection doesn't exist" do
expect(output.dig(:collection, :links_attributes, 0, :url)).to eq "http://localhost:3214/collections/abc123"
end

it "parses sensitive flag" do
expect(output[:sensitive]).to be true
end
end
end
Loading
Loading
0