Commit 75895b3a authored by Bruno Nocera Zanette's avatar Bruno Nocera Zanette

Merge branch 'add-report-endpoints' into 'master'

Add Report Endpoints



See merge request !2
parents 1b7b9adb bd817c94
......@@ -5,10 +5,10 @@ require 'dspace/version'
Gem::Specification.new do |spec|
spec.date = '2016-03-14'
spec.name = "dspace_rest_client"
spec.name = 'dspace_rest_client'
spec.version = Dspace::VERSION
spec.authors = ["Mauricio Giacomini Girardello", "Lucas Ernesto Kindinger", "Bruno N. Zanette", "Mateus Rambo Strey"]
spec.email = ["mauriciogiacomini4@gmail.com", "kindingerlek@hotmail.com"]
spec.authors = ['Mauricio Giacomini Girardello', 'Lucas Ernesto Kindinger', 'Bruno N. Zanette', 'Mateus Rambo Strey']
spec.email = ['mauriciogiacomini4@gmail.com', 'mateus.rstrey@gmail.com' 'kindingerlek@hotmail.com']
spec.summary = 'DSpace REST API Client for Ruby'
spec.description = 'DSpace REST-API Client for Ruby! Implements all DSpace REST-API endpoints requests.'
spec.homepage = 'https://gitlab.c3sl.ufpr.br/c3sl/dspace-rest-client'
......@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
spec.test_files = `git ls-files -z spec/`.split("\0")
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']
spec.add_runtime_dependency "json", '~> 1.8', '>= 1.8.3'
spec.add_runtime_dependency "resource_kit", "~> 0.1.4"
......@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "net-http-persistent", '~> 2.9', '>= 2.9.4'
spec.add_runtime_dependency "activesupport", '>= 4.2.0'
spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
end
end
\ No newline at end of file
......@@ -5,6 +5,8 @@ require "faraday"
require "active_support/all"
require_relative "dspace/version"
require_relative "dspace/resources/report_resource"
require_relative "dspace/resources/hierarchy_resource"
require_relative "dspace/resources/schema_registry_resource"
require_relative "dspace/resources/community_resource"
require_relative "dspace/resources/collection_resource"
......@@ -15,6 +17,7 @@ require_relative "dspace/resources/item_resource"
require_relative "dspace/builders/hash_builder"
require_relative "dspace/builders/model_builder"
require_relative "dspace/builders/tempfile_builder"
require_relative "dspace/site"
require_relative "dspace/community"
require_relative "dspace/collection"
require_relative "dspace/item"
......@@ -23,6 +26,8 @@ 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/report"
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|
......
......@@ -28,7 +28,9 @@ module Dspace
communities: ::Dspace::Resources::CommunityResource,
status: ::Dspace::Resources::StatusResource,
authentication: ::Dspace::Resources::AuthenticationResource,
schema_registry: ::Dspace::Resources::SchemaRegistryResource
schema_registry: ::Dspace::Resources::SchemaRegistryResource,
hierarchy: ::Dspace::Resources::HierarchyResource,
report: ::Dspace::Resources::ReportResource
}
end
......
......@@ -20,8 +20,17 @@ module Dspace
@short_description = args['shortDescription'] || args['short_description']
@sidebar_text = args['sidebarText'] || args['sidebar_text']
@count_items = args['countItems']
@sub_communities = Dspace::Builders::ModelBuilder.build_communities(args['subcommunities']) unless args['subcommunities'].nil?
@collections = Dspace::Builders::ModelBuilder.build_collections(args['collections']) unless args['collections'].nil?
# subcommunities = All community endpoints
# community = Hierarchy endpoint
sub_communities = args['subcommunities'] || args['community']
@sub_communities = Dspace::Builders::ModelBuilder.build_communities(sub_communities) unless sub_communities.nil?
# collections = All community/collection endpoints
# collection = Hierarchy endpoint
collections = args['collections'] || args['collection']
@collections = Dspace::Builders::ModelBuilder.build_collections(collections) unless collections.nil?
@expand = args['expand']
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 HierarchyResource < 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}" }
action :all, 'GET /rest/hierarchy' do
handler(200) do |response|
Dspace::Site.new(JSON.parse(response.body))
end
end
end
end
end
end
module Dspace
module Resources
class ReportResource < 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
query_keys :limit, :offset, :expand, :filters, :collection
handler(200) do |response|
Dspace::Builders::ModelBuilder.build_collections(JSON.parse(response.body))
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 /rest/filtered-collections/:collection_id' do
query_keys :limit, :offset, :expand, :filters, :collection
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
module Dspace
class Site
include Dspace::Builders::HashBuilder
attr_reader :id, :name, :handle, :community_list
def initialize(args={})
@id = args['id'] || args['uuid']
@name = args['name']
@handle = args['handle']
@community_list = Dspace::Builders::ModelBuilder.build_communities(args['community']) unless args['community'].nil?
end
def to_h
{
uuid: @id,
name: @name,
handle: @handle,
community: obj2hash(@community_list),
}
end
end
end
module Dspace
VERSION = "2.2.11"
VERSION = "2.2.13"
end
require 'spec_helper'
RSpec.describe Dspace::Resources::HierarchyResource, resource_kit: true do
subject(:resource) { Dspace::Resources::HierarchyResource }
it 'get all hierarchy' do
expect(resource).to have_action(:all).that_handles(200).at_path('/rest/hierarchy').with_verb(:get) do |handled|
expect(handled).to be_kind_of(Dspace::Site)
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