refactoring code

parent c17274be
......@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
s.authors = ["C3SL - Centro de Computação Cientifica e Software Livre"]
s.email = 'c3sl@c3sl.ufpr.br'
s.files = Dir['lib/*.rb'] +
Dir['lib/dspacerestclient/*.rb'] +
Dir['lib/dspacerest/*.rb'] +
Dir['bin/*']
#s.files = `git ls-files -z`.split("\0")
#s.test_files = `git ls-files -z spec/`.split("\0")
......
# More logical way to require 'rest-client'
# More logical way to require 'dspace-rest-client'
require File.dirname(__FILE__) + '/dspacerestclient'
require File.dirname(__FILE__) + '/dspacerest/auth'
require File.dirname(__FILE__) + '/dspacerest/bitstream'
require File.dirname(__FILE__) + '/dspacerest/collection'
require File.dirname(__FILE__) + '/dspacerest/community'
require File.dirname(__FILE__) + '/dspacerest/item'
\ No newline at end of file
module DSpaceRest
class Auth
attr_reader :token
def initialize(request)
@request = request
end
def login (username, password)
data = JSON.generate({:email => username, :password => password})
response = @request['/login'].post(data)
@token = {:'rest-dspace-token' => response}
end
def logout
response = @request['/logout'].post([], @token)
end
end
end
\ No newline at end of file
class DspaceRestClient
module DSpaceRest
class Bitstream
class BitStream
attr_accessor :id, :name, :type, :bundleName,
:checkSum, :description, :format, :mimeType,
:retrieveLink, :sequenceId, :sizeBytes
:checkSum, :description, :format, :mimeType,
:retrieveLink, :sequenceId, :sizeBytes
def initialize args
@id = args['id']
......@@ -20,6 +20,21 @@ class DspaceRestClient
@sizeBytes = args['sizeBytes']
end
def self.get_by_id(id, request)
response = request["/bitstreams/#{id}"].get
Bitstreams.new(JSON.parse(response))
end
def self.get_all(request)
response = request['/bitstreams'].get
bitStreams = []
JSON.parse(response).each do |bits|
bitStreams << Bitstream.new(bits)
end
bitStreams
end
end
end
class DspaceRestClient
module DSpaceRest
class Collection
attr_accessor :handle, :id, :name, :type,
:copyrightText, :numberItems, :introductoryText,
:shortDescription, :sidebarText
:copyrightText, :numberItems, :introductoryText,
:shortDescription, :sidebarText
def initialize args
@handle = args['handle']
......@@ -18,6 +18,20 @@ class DspaceRestClient
@sidebarText = args['sidebarText']
end
def self.get_by_id(id, request)
response = request["/collections/#{id}"].get
Collection.new(JSON.parse(response))
end
def self.get_all(request)
response = request['/collections'].get
collections = []
JSON.parse(response).each do |coll|
collections << Collection.new(coll)
end
collections
end
end
end
class DspaceRestClient
module DSpaceRest
class Community
attr_accessor :handle, :id, :name, :type,
:copyrightText, :countItems, :introductoryText,
:shortDescription, :sidebarText
:copyrightText, :countItems, :introductoryText,
:shortDescription, :sidebarText
def initialize args
@handle = args['handle']
......@@ -18,6 +18,20 @@ class DspaceRestClient
@sidebarText = args['sidebarText']
end
def self.get_by_id(id, request)
response = request["/communities/#{id}"].get
Community.new(JSON.parse(response))
end
def self.get_all(request)
response = request['/communities'].get
communities = []
JSON.parse(response).each do |comm|
communities << Community.new(comm)
end
communities
end
end
end
class DspaceRestClient
module DSpaceRest
class Item
attr_accessor :handle, :id, :name, :type,
:archived, :lastModified, :withdrawn
:archived, :lastModified, :withdrawn
def initialize args
@handle = args['handle']
......@@ -15,6 +15,20 @@ class DspaceRestClient
@withdrawn = args['withdrawn']
end
def self.get_by_id(id, request)
response = request["/items/#{id}"].get
Item.new(JSON.parse(response))
end
def self.get_all(request)
response = request['/items'].get
items = []
JSON.parse(response).each do |item|
items << Item.new(item)
end
items
end
end
end
require 'rest-client'
require File.dirname(__FILE__) + '/dspacerestclient/community'
require File.dirname(__FILE__) + '/dspacerestclient/collection'
require File.dirname(__FILE__) + '/dspacerestclient/item'
require File.dirname(__FILE__) + '/dspacerestclient/bitstream'
class DspaceRestClient
attr_reader :dspaceurl, :headers, :token, :request
#---------------------------------------------------
def initialize(args)
@dspaceurl = args[:dspaceurl] or raise ArgumentError, "must pass :dspaceurl"
default_headers = {
:content_type => :json,
:accept => :json
}
@headers = args[:headers] || default_headers
@token = args[:token] || {}
@authenticated = false
@request = RestClient::Resource.new(
@dspaceurl,
:verify_ssl => OpenSSL::SSL::VERIFY_NONE,
:headers => @headers,
)
end
#---------------------------------------------------
module DSpaceRest
#---------------------------------------------------
def get_community(id)
response = @request["/communities/#{id}"].get
Community.new(JSON.parse(response))
end
class Client
attr_reader :request
#---------------------------------------------------
def initialize(args)
dspaceurl = args[:dspaceurl] or raise ArgumentError, "must pass :dspaceurl"
def get_communities
response = @request['/communities'].get
communities = []
JSON.parse(response).each do |comm|
communities << Community.new(comm)
# build http header
default_headers = {
:content_type => :json,
:accept => :json
}
headers = args[:headers] || default_headers
@request = RestClient::Resource.new(
dspaceurl,
:verify_ssl => OpenSSL::SSL::VERIFY_NONE,
:headers => headers,
)
end
communities
end
#---------------------------------------------------
#---------------------------------------------------
def get_collection(id)
response = @request["/collections/#{id}"].get
Collection.new(JSON.parse(response))
end
def status
response = @request['/status'].get
end
def get_collections
response = @request['/collections'].get
collections = []
JSON.parse(response).each do |coll|
collections << Collection.new(coll)
def test
response = @request['/test'].get
end
collections
end
#---------------------------------------------------
#---------------------------------------------------
def get_item(id)
response = @request["/items/#{id}"].get
Item.new(JSON.parse(response))
end
def get_items
response = @request['/items'].get
items = []
JSON.parse(response).each do |item|
items << Item.new(item)
def get_community(id)
Community.get_by_id id, @request
end
items
end
#---------------------------------------------------
#---------------------------------------------------
def get_bitstream(id)
response = @request["/bitstreams/#{id}"].get
Bitstreams.new(JSON.parse(response))
end
def get_communities
Community.get_all @request
end
def get_bitstreams
response = @request['/bitstreams'].get
bitstreams = []
JSON.parse(response).each do |bits|
bitstreams << Bitstream.new(bits)
def get_collection(id)
Collection.get_by_id id, @request
end
bitstreams
end
#---------------------------------------------------
#---------------------------------------------------
def login (username, password)
data = JSON.generate({:email=>username,:password=>password})
response = @request['/login'].post(data)
@token = {:'rest-dspace-token' => response}
end
def get_collections
Collection.get_all @request
end
def logout
response = @request['/logout'].post([], @token)
end
def get_item(id)
Item.get_by_id id, @request
end
def status
response = @request['/status'].get
end
def get_items
Item.get_all @request
end
def test
response = @request['test'].get
end
#---------------------------------------------------
def get_bitstream(id)
BitStream.get_by_id id, @request
end
def get_bitstreams
BitStream.get_all @request
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