Commit 10e7d21e authored by Bruno Nocera Zanette's avatar Bruno Nocera Zanette
Browse files

add SchemaRegistryResource (SchemaRegistry & Field)

parent 4d08f50a
......@@ -5,6 +5,7 @@ require "faraday"
require "active_support/all"
require_relative "dspace/version"
require_relative "dspace/resources/schema_registry_resource"
require_relative "dspace/resources/community_resource"
require_relative "dspace/resources/collection_resource"
require_relative "dspace/resources/authentication_resource"
......@@ -18,6 +19,8 @@ require_relative "dspace/community"
require_relative "dspace/collection"
require_relative "dspace/item"
require_relative "dspace/metadata"
require_relative "dspace/schema_registry"
require_relative "dspace/field"
require_relative "dspace/bitstream"
require_relative "dspace/policy"
require_relative "dspace/client"
......
......@@ -29,6 +29,24 @@ module Dspace
colls
end
def self.build_schemaregistry(schemas=[])
return schemas unless schemas.is_a? Array
colls = []
schemas.each do |c|
colls << Dspace::SchemaRegistry.new(c)
end
colls
end
def self.build_fields(fields=[])
return fields unless fields.is_a? Array
colls = []
fields.each do |c|
colls << Dspace::Field.new(c)
end
colls
end
def self.build_bitstreams(bitstreams=[])
return bitstreams unless bitstreams.is_a? Array
colls = []
......
......@@ -27,7 +27,8 @@ module Dspace
collections: ::Dspace::Resources::CollectionResource,
communities: ::Dspace::Resources::CommunityResource,
status: ::Dspace::Resources::StatusResource,
authentication: ::Dspace::Resources::AuthenticationResource
authentication: ::Dspace::Resources::AuthenticationResource,
schema_registry: ::Dspace::Resources::SchemaRegistryResource
}
end
......
module Dspace
class Field
include Dspace::Builders::HashBuilder
attr_accessor :name, :element, :description, :qualifier
attr_reader :id, :parentSchema, :expand
def initialize(args={})
@id = args['fieldId']
@name = args['name']
@element = args['element']
@description = args['description']
@qualifier = args['qualifier']
@parentSchema = args['parentSchema']
@expand = args['expand']
end
def to_h
{
fieldId: @id,
name: @name,
element: @element,
description: @description,
qualifier: @qualifier,
parentSchema: @parentSchema,
expand: @expand
}
end
end
end
module Dspace
module Resources
class SchemaRegistryResource < ResourceKit::Resource
resources do
# default_handler(401) { raise NotAuthorizedError, 'This request requires authentication' }
# default_handler(404) { raise NotFoundError, 'The specified object doesn\'t exist' }
# default_handler(405) { raise MethodNotAllowedError, 'Wrong request method (GET,POST,PUT,DELETE) or wrong data format (JSON/XML)' }
# default_handler(415) { raise UnsupportedMediaTypeError, 'Missing "Content-Type: application/json" or "Content-Type: application/xml" request header' }
# default_handler(500) { raise ServerError, 'Likely a SQLException, IOException, more details in the logs' }
default_handler { |response| raise StandardError, "#{response.inspect}" }
action :all, 'GET /rest/registries/schema' do
query_keys :expand
handler(200) do |response|
Dspace::Builders::ModelBuilder.build_schemaregistry(JSON.parse(response.body))
end
end
action :find, 'GET /rest/registries/schema/:schema_prefix' do
query_keys :expand
handler(200) do |response|
Dspace::SchemaRegistry.new(JSON.parse(response.body))
end
end
action :find_metadata_field, 'GET /rest/registries/metadata-fields/:field_id' do
query_keys :expand
handler(200) do |response|
Dspace::Field.new(JSON.parse(response.body))
end
end
end
end
end
end
module Dspace
class SchemaRegistry
include Dspace::Builders::HashBuilder
attr_accessor :namespace, :prefix, :expand
attr_reader :id, :fields
def initialize(args={})
@id = args['schemaID']
@namespace = args['namespace']
@prefix = args['prefix']
@expand = args['expand']
@fields = Dspace::Builders::ModelBuilder.build_fields(args['fields'])
end
def to_h
{
schemaID: @id,
namespace: @namespace,
prefix: @prefix,
expand: @expand,
fields: @fields
}
end
def add_field(name, element, description)
f = {}
f['name'] = name
f['element'] = element
f['description'] = description
@fields << Dspace::Field.new(f)
@fields
end
def reset_fields
@fields = []
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment