Commit 843b5bb2 authored by Bruno Nocera Zanette's avatar Bruno Nocera Zanette
Browse files

add QueryReportResource & ItemFilter/Report Class

parent 0f84f283
......@@ -5,6 +5,7 @@ require "faraday"
require "active_support/all"
require_relative "dspace/version"
require_relative "dspace/resources/query_report_resource"
require_relative "dspace/resources/hierarchy_resource"
require_relative "dspace/resources/schema_registry_resource"
require_relative "dspace/resources/community_resource"
......@@ -25,6 +26,7 @@ require_relative "dspace/schema"
require_relative "dspace/metadata_field"
require_relative "dspace/bitstream"
require_relative "dspace/policy"
require_relative "dspace/item_filter"
require_relative "dspace/client"
module Dspace
......
......@@ -74,6 +74,24 @@ module Dspace
colls
end
def self.build_reports(reports=[])
return reports unless reports.is_a? Array
colls = []
reports.each do |c|
colls << Dspace::Report.new(c)
end
colls
end
def self.build_item_filters(item_filters=[])
return item_filters unless item_filters.is_a? Array
colls = []
item_filters.each do |c|
colls << Dspace::ItemFilter.new(c)
end
colls
end
def self.models2hash(list)
if list.is_a? Array
list.map do |m|
......
......@@ -12,7 +12,7 @@ module Dspace
def connection
Faraday.new(connection_options) do |req|
# req.response :logger
req.response :logger
req.request :multipart
req.request :url_encoded
req.use(Faraday::Response::Logger, @logger) unless @logger.nil?
......@@ -29,7 +29,8 @@ module Dspace
status: ::Dspace::Resources::StatusResource,
authentication: ::Dspace::Resources::AuthenticationResource,
schema_registry: ::Dspace::Resources::SchemaRegistryResource,
hierarchy: ::Dspace::Resources::HierarchyResource
hierarchy: ::Dspace::Resources::HierarchyResource,
query_report: ::Dspace::Resources::QueryReportResource
}
end
......
module Dspace
class ItemFilter
attr_reader :category, :filter_name, :title
def initialize(args={})
@category = args['category']
@filter_name = args['filter-name']
@title = args['title']
end
def to_h
{category: @category, 'filter-name': @filter_name, title: @title}
end
end
end
module Dspace
class Report
attr_reader :nickname, :url
def initialize(args={})
@nickname = args['nickname']
@url = args['url']
end
def to_h
{nickname: @nickname, url: @url}
end
end
end
module Dspace
module Resources
class QueryReportResource < ResourceKit::Resource
resources do
default_handler(400) { raise InvalidTokenError, 'Invalid access token.' }
default_handler(403) { raise InvalidCredentialsError, 'Wrong Dspace credentials.' }
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}" }
# GET /reports
# => Return a list of report tools built on the rest api
# TODO: Fix response error
action :reports,
'GET /rest/reports' do
handler(200) do |response|
Dspace::Builders::ModelBuilder.build_reports(JSON.parse(response.body))
end
end
# GET /reports/{nickname}
# => Return a redirect to a specific report
# TODO: Fix response error
action :reports_by_nickname,
'GET /rest/reports/:nickname' do
handler(200) do |response|
response.body
end
end
# GET /filters
# => Return a list of use case filters available for quality control reporting
action :filters,
'GET /rest/filters' do
handler(200) do |response|
Dspace::Builders::ModelBuilder.build_item_filters(JSON.parse(response.body))
end
end
# GET /filtered-collections
# => Return collections and item counts based on pre-defined filters
# TODO: Fix query keys
# TODO: Test it!
action :filtered_collections,
'GET /rest/filtered-collections' do
handler(200) do |response|
Dspace::Builders::ModelBuilder.build_collections(JSON.parse(response.body)['collections'])
end
end
# GET /filtered-collections/{collection_id}
# => Return items and item counts for a collection based on pre-defined filters
# TODO: Fix query keys
# TODO: Test it!
action :filtered_collections_by_id,
'GET /filtered-collections/:collection_id' do
handler(200) do |response|
Dspace::Builders::ModelBuilder.build_items(JSON.parse(response.body)['items'])
end
end
# GET /filtered-items
# => Retrieve a set of items based on a metadata query and a set of filters
action :filtered_items,
'GET /rest/filtered-items' do
query_keys :'collSel[]', :'query_field[]', :'query_op[]', :'query_val[]', :limit, :offset, :expand, :filters
handler(200) do |response|
Dspace::Builders::ModelBuilder.build_items(JSON.parse(response.body)['items'])
end
end
end
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