dspace_rest_client 1.2.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +11 -0
  3. data/.rbenv +1 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +13 -0
  6. data/README.md +58 -0
  7. data/Rakefile +1 -0
  8. data/bin/console +14 -0
  9. data/bin/setup +7 -0
  10. data/dspace_rest_client.gemspec +30 -0
  11. data/lib/dspace.rb +31 -0
  12. data/lib/{dspacerest → dspace}/bitstream.rb +4 -5
  13. data/lib/{dspacerest → dspace}/builders/hash_builder.rb +10 -6
  14. data/lib/{dspacerest → dspace}/builders/model_builder.rb +7 -7
  15. data/lib/dspace/builders/tempfile_builder.rb +26 -0
  16. data/lib/dspace/client.rb +78 -0
  17. data/lib/{dspacerest → dspace}/collection.rb +6 -13
  18. data/lib/{dspacerest → dspace}/community.rb +6 -8
  19. data/lib/{dspacerest → dspace}/item.rb +11 -19
  20. data/lib/{dspacerest → dspace}/metadata.rb +1 -2
  21. data/lib/{dspacerest → dspace}/policy.rb +2 -4
  22. data/lib/dspace/resources/authentication_resource.rb +22 -0
  23. data/lib/dspace/resources/bitstream_resource.rb +63 -0
  24. data/lib/dspace/resources/collection_resource.rb +50 -0
  25. data/lib/dspace/resources/community_resource.rb +78 -0
  26. data/lib/dspace/resources/item_resource.rb +66 -0
  27. data/lib/dspace/resources/status_resource.rb +19 -0
  28. data/lib/dspace/version.rb +3 -0
  29. data/spec/lib/dspace/resources/authentication_resource_spec.rb +34 -0
  30. data/spec/lib/dspace/resources/bitstream_resource_spec.rb +45 -0
  31. data/spec/lib/dspace/resources/collection_resource_spec.rb +50 -0
  32. data/spec/lib/dspace/resources/community_resource_spec.rb +81 -0
  33. data/spec/lib/dspace/resources/item_resource_spec.rb +69 -0
  34. data/spec/spec_helper.rb +5 -0
  35. metadata +171 -43
  36. data/lib/dspace_client.rb +0 -64
  37. data/lib/dspace_rest_client.rb +0 -17
  38. data/lib/dspacerest/repositories/abstract_repository.rb +0 -37
  39. data/lib/dspacerest/repositories/bitstream_repository.rb +0 -62
  40. data/lib/dspacerest/repositories/collection_repository.rb +0 -78
  41. data/lib/dspacerest/repositories/community_repository.rb +0 -76
  42. data/lib/dspacerest/repositories/dspace_repository.rb +0 -27
  43. data/lib/dspacerest/repositories/item_repository.rb +0 -80
  44. data/lib/dspacerest/strategies/uploads/curl_strategy.rb +0 -27
  45. data/lib/dspacerest/strategies/uploads/rest_strategy.rb +0 -17
@@ -1,64 +0,0 @@
1
- require 'rest-client'
2
-
3
- class DspaceClient
4
- attr_reader :rest_client, :url
5
-
6
- def initialize(url, authenticated_token = nil)
7
- @url = url
8
-
9
- if (authenticated_token.nil?)
10
- @rest_client = build_rest_client url
11
- else
12
- @rest_client = build_rest_client url, rest_dspace_token: authenticated_token
13
- end
14
- end
15
-
16
- def repository
17
- @dspace_repository ||= build_repository @rest_client
18
- end
19
-
20
- def login(username, password)
21
- user = JSON.generate({
22
- email: username,
23
- password: password
24
- })
25
-
26
- # send login request to server and receive the token
27
- authenticated_token = @rest_client['/login'].post user
28
-
29
- # overwrite the rest_client and dspace_repository
30
- @rest_client = build_rest_client @url, rest_dspace_token: authenticated_token
31
- @dspace_repository = build_repository @rest_client
32
-
33
- authenticated_token
34
- end
35
-
36
- def logout
37
- response = JSON.parse @rest_client['/logout'].post []
38
- end
39
-
40
- def status
41
- response = JSON.parse @rest_client['/status'].get
42
- end
43
-
44
- def test
45
- response = JSON.parse(@rest_client['/test'].get)
46
- end
47
-
48
- private
49
-
50
- def build_repository(rest_client)
51
- DSpaceRest::Repositories::DspaceRepository.new rest_client
52
- end
53
-
54
- def build_rest_client(url, headers={})
55
- RestClient::Resource.new(url,
56
- verify_ssl: OpenSSL::SSL::VERIFY_NONE,
57
- headers: headers.merge(
58
- content_type: :json,
59
- accept: :json
60
- )
61
- )
62
- end
63
-
64
- end
@@ -1,17 +0,0 @@
1
- # More logical way to require 'dspace-rest-client'
2
- require File.dirname(__FILE__) + '/dspace_client'
3
- require File.dirname(__FILE__) + '/dspacerest/bitstream'
4
- require File.dirname(__FILE__) + '/dspacerest/collection'
5
- require File.dirname(__FILE__) + '/dspacerest/community'
6
- require File.dirname(__FILE__) + '/dspacerest/item'
7
- require File.dirname(__FILE__) + '/dspacerest/metadata'
8
- require File.dirname(__FILE__) + '/dspacerest/repositories/abstract_repository'
9
- require File.dirname(__FILE__) + '/dspacerest/repositories/bitstream_repository'
10
- require File.dirname(__FILE__) + '/dspacerest/repositories/collection_repository'
11
- require File.dirname(__FILE__) + '/dspacerest/repositories/community_repository'
12
- require File.dirname(__FILE__) + '/dspacerest/repositories/dspace_repository'
13
- require File.dirname(__FILE__) + '/dspacerest/repositories/item_repository'
14
- require File.dirname(__FILE__) + '/dspacerest/strategies/uploads/curl_strategy'
15
- require File.dirname(__FILE__) + '/dspacerest/strategies/uploads/rest_strategy'
16
- require File.dirname(__FILE__) + '/dspacerest/builders/hash_builder'
17
- require File.dirname(__FILE__) + '/dspacerest/builders/model_builder'
@@ -1,37 +0,0 @@
1
- module DSpaceRest
2
- module Repositories
3
- class AbstractRepository
4
- attr_reader :rest_client
5
-
6
- def initialize(rest_client)
7
- @rest_client = rest_client
8
- end
9
-
10
- protected
11
-
12
- def build_query_string(params)
13
-
14
- query_string=""
15
-
16
- if query_parameters.include? 'limit'
17
- query_string << "limit=#{params[:limit]}&"
18
- end
19
-
20
- if query_parameters.include? 'offset'
21
- query_string << "offset=#{params[:offset]}&"
22
- end
23
-
24
- expand_string = ""
25
- params[:expand].each do |expand|
26
- if expandable_properties.include? expand
27
- expand_string << "#{expand},"
28
- end
29
- end
30
- query_string << "expand=#{expand_string}"
31
-
32
- query_string
33
- end
34
-
35
- end
36
- end
37
- end
@@ -1,62 +0,0 @@
1
- module DSpaceRest
2
- module Repositories
3
- class BitstreamRepository < AbstractRepository
4
-
5
- # Bitstreams are files. They have a filename, size (in bytes), and a file format.
6
- # Typically in DSpace, the Bitstream will the "full text" article, or some other media.
7
- # Some files are the actual file that was uploaded (tagged with bundleName:ORIGINAL),
8
- # others are DSpace-generated files that are derivatives or renditions, such as text-extraction, or thumbnails.
9
- # You can download files/bitstreams.
10
- # DSpace doesn't really limit the type of files that it takes in, so this could be PDF, JPG, audio, video, zip,
11
- # or other.
12
- # Also, the logo for a Collection or a Community, is also a Bitstream.
13
-
14
- # √ GET /bitstreams - Return all bitstreams in DSpace.
15
- # √ GET /bitstreams/{bitstream id} - Return bitstream.
16
- # GET /bitstreams/{bitstream id}/policy - Return bitstream policies.
17
- # √ GET /bitstreams/{bitstream id}/retrieve - Return data of bitstream.
18
- # POST /bitstreams/{bitstream id}/policy - Add policy to item. You must post a ResourcePolicy
19
- # PUT /bitstreams/{bitstream id}/data - Update data/file of bitstream. You must put the data
20
- # √ PUT /bitstreams/{bitstream id} - Update metadata of bitstream. You must put a Bitstream, does not alter the file/data
21
- # DELETE /bitstreams/{bitstream id} - Delete bitstream from DSpace.
22
- # DELETE /bitstreams/{bitstream id}/policy/{policy_id} - Delete bitstream policy.
23
-
24
- def get_bitstream_by_id(id, params)
25
- query_string = build_query_string(params)
26
- response = rest_client["/bitstreams/#{id}?#{query_string}"].get
27
- DSpaceRest::Bitstream.new(JSON.parse(response))
28
- end
29
-
30
- def get_all_bitstreams(params)
31
- query_string = build_query_string(params)
32
- response = rest_client["/bitstreams?#{query_string}"].get
33
- bit_streams = []
34
- JSON.parse(response).each do |bits|
35
- bit_streams << DSpaceRest::Bitstream.new(bits)
36
- end
37
- bit_streams
38
- end
39
-
40
- def retrieve_data(bitstream)
41
- response = rest_client["/bitstreams/#{bitstream.id}/retrieve"].get
42
- end
43
-
44
- def update(bitstream)
45
- valid_keys=[:name, :description, :sequenceId]
46
- form = JSON.generate(bitstream.to_h.select { |k, v| valid_keys.include? k })
47
- response = rest_client["/bitstreams/#{bitstream.id}"].put form
48
- end
49
-
50
- private
51
-
52
- def expandable_properties
53
- ["parent","policies","all"]
54
- end
55
-
56
- def query_parameters
57
- ["limit","offset"]
58
- end
59
-
60
- end
61
- end
62
- end
@@ -1,78 +0,0 @@
1
- module DSpaceRest
2
- module Repositories
3
- class CollectionRepository < AbstractRepository
4
- # Collections in DSpace are containers of Items
5
-
6
- # √ GET /communities/{communityId}/collections - Returns array of collections of community.
7
- # √ GET /collections - Return all collections of DSpace in array.
8
- # √ GET /collections/{collectionId} - Return collection with id.
9
- # √ GET /collections/{collectionId}/items - Return all items of collection.
10
- # √ POST /communities/{communityId}/collections - Create new collections in community. You must post Collection.
11
- # √ POST /collections/{collectionId}/items - Create posted item in collection. You must post an Item
12
- # POST /collections/find-collection - Find collection by passed name.
13
- # PUT /collections/{collectionId} - Update collection. You must put Collection.
14
- # DELETE /collections/{collectionId} - Delete collection from DSpace.
15
- # DELETE /collections/{collectionId}/items/{itemId} - Delete item in collection.
16
- # DELETE /communities/{communityId}/collections/{collectionId} - Delete collection in community.
17
-
18
- def get_collection_items(collection, params)
19
- query_string = build_query_string(params)
20
- response = rest_client["/collections/#{collection.id}/items?#{query_string}"].get
21
- items = []
22
- JSON.parse(response).each do |item|
23
- items << DSpaceRest::Item.new(item)
24
- end
25
- items
26
- end
27
-
28
- def get_collection_by_id(id, params)
29
- query_string = build_query_string(params)
30
- response = rest_client["/collections/#{id}?#{query_string}"].get
31
- DSpaceRest::Collection.new(JSON.parse(response))
32
- end
33
-
34
- def get_all_collections(params)
35
- query_string = build_query_string(params)
36
- response = rest_client["/collections?#{query_string}"].get
37
- collections = []
38
- JSON.parse(response).each do |coll|
39
- collections << DSpaceRest::Collection.new(coll)
40
- end
41
- collections
42
- end
43
-
44
- def get_collections_of(community, params)
45
- query_string = build_query_string(params)
46
- response = rest_client["/communities/#{community.id}/collections?#{query_string}"].get
47
- collections = []
48
- JSON.parse(response).each do |coll|
49
- collections << DSpaceRest::Collection.new(coll)
50
- end
51
- collections
52
- end
53
-
54
- def create_collection_for(community, collection)
55
- form = JSON.generate(collection.to_h)
56
- response = rest_client["/communities/#{community.id}/collections"].post form
57
- DSpaceRest::Collection.new(JSON.parse(response))
58
- end
59
-
60
- def create_item_for(collection, item)
61
- form = JSON.generate({"metadata" => item.to_h[:metadata]})
62
- response = rest_client["/collections/#{collection.id}/items"].post form
63
- DSpaceRest::Item.new(JSON.parse(response))
64
- end
65
-
66
- private
67
-
68
- def expandable_properties
69
- ["parentCommunityList","parentCommunity","items","license","logo","all"]
70
- end
71
-
72
- def query_parameters
73
- ["limit","offset"]
74
- end
75
-
76
- end
77
- end
78
- end
@@ -1,76 +0,0 @@
1
- module DSpaceRest
2
- module Repositories
3
- class CommunityRepository < AbstractRepository
4
- # Communities in DSpace are used for organization and hierarchy, and are containers that hold sub-Communities and Collections.
5
-
6
- # √ GET /communities - Returns array of all communities in DSpace.
7
- # √ GET /communities/top-communities - Returns array of all top communities in DSpace
8
- # √ GET /communities/{communityId} - Returns community
9
- # √ GET /communities/{communityId}/communities - Returns array of subcommunities of community.
10
- # √ POST /communities - Create new community at top level. You must post community.
11
- # √ POST /communities/{communityId}/communities - Create new subcommunity in community. You must post Community.
12
- # PUT /communities/{communityId} - Update community. You must put Community
13
- # DELETE /communities/{communityId} - Delete community.
14
- # DELETE /communities/{communityId}/communities/{communityId2} - Delete subcommunity in community.
15
-
16
- def get_community_by_id(id, params)
17
- query_string = build_query_string(params)
18
- response = rest_client["/communities/#{id}?#{query_string}"].get
19
- DSpaceRest::Community.new(JSON.parse(response))
20
- end
21
-
22
- def get_all_communities(params)
23
- query_string = build_query_string(params)
24
- response = rest_client["/communities?#{query_string}"].get
25
- communities = []
26
- JSON.parse(response).each do |comm|
27
- communities << DSpaceRest::Community.new(comm)
28
- end
29
- communities
30
- end
31
-
32
- def get_top_communities(params)
33
- query_string = build_query_string(params)
34
- response = rest_client["/communities/top-communities?#{query_string}"].get
35
- communities = []
36
- JSON.parse(response).each do |comm|
37
- communities << DSpaceRest::Community.new(comm)
38
- end
39
- communities
40
- end
41
-
42
- def get_subcommunities_of(community, params)
43
- query_string = build_query_string(params)
44
- response = rest_client["/communities/#{community.id}/communities?#{query_string}"].get
45
- communities = []
46
- JSON.parse(response).each do |comm|
47
- communities << DSpaceRest::Community.new(comm)
48
- end
49
- communities
50
- end
51
-
52
- def create_community(community)
53
- form = JSON.generate(community.to_h)
54
- response = rest_client["/communities"].post form
55
- end
56
-
57
- def create_subcommunity_of(community, subcommunity)
58
- form = JSON.generate(subcommunity.to_h)
59
- response = rest_client["/communities/#{community.id}/communities"].post form
60
-
61
- DSpaceRest::Community.new(JSON.parse(response))
62
- end
63
-
64
- private
65
-
66
- def expandable_properties
67
- ["parentCommunity","collections","subCommunities","logo","all"]
68
- end
69
-
70
- def query_parameters
71
- ["limit","offset"]
72
- end
73
-
74
- end
75
- end
76
- end
@@ -1,27 +0,0 @@
1
- module DSpaceRest
2
- module Repositories
3
- class DspaceRepository < AbstractRepository
4
-
5
- def bitstream_repository
6
- BitstreamRepository.new rest_client
7
- end
8
-
9
- def collection_repository
10
- CollectionRepository.new rest_client
11
- end
12
-
13
- def community_repository
14
- CommunityRepository.new rest_client
15
- end
16
-
17
- def item_repository
18
- ItemRepository.new rest_client
19
- end
20
-
21
- def metadata_repository
22
- MetadataRepository.new rest_client
23
- end
24
-
25
- end
26
- end
27
- end
@@ -1,80 +0,0 @@
1
- module DSpaceRest
2
- module Repositories
3
- class ItemRepository < AbstractRepository
4
- # Items in DSpace represent a "work" and combine metadata and files, known as Bitstreams
5
-
6
- # √ GET /items - Return list of items.
7
- # √ GET /items/{item id} - Return item.
8
- # √ GET /items/{item id}/metadata - Return item metadata.
9
- # √ GET /items/{item id}/bitstreams - Return item bitstreams.
10
- # POST /items/find-by-metadata-field - Find items by metadata entry. You must post a MetadataEntry. DS-2501 - wrong SQL in REST /items/find-by-metadata-field CLOSED
11
- # √ POST /items/{item id}/metadata - Add metadata to item. You must post an array of MetadataEntry
12
- # √ POST /items/{item id}/bitstreams - Add bitstream to item. You must post a Bitstream
13
- # √ PUT /items/{item id}/metadata - Update metadata in item. You must put a MetadataEntry
14
- # √ DELETE /items/{item id} - Delete item.
15
- # DELETE /items/{item id}/metadata - Clear item metadata.
16
- # DELETE /items/{item id}/bitstreams/{bitstream id} - Delete item bitstream.
17
-
18
-
19
- def get_all_items(params)
20
- query_string = build_query_string(params)
21
- response = rest_client["/items?#{query_string}"].get
22
- items = []
23
- JSON.parse(response).each do |item|
24
- items << DSpaceRest::Item.new(item)
25
- end
26
- items
27
- end
28
-
29
- def get_item_by_id(id, params)
30
- query_string = build_query_string(params)
31
- response = rest_client["/items/#{id}?#{query_string}"].get
32
- DSpaceRest::Item.new(JSON.parse(response))
33
- end
34
-
35
- def get_metadata_of(item)
36
- response = rest_client["/items/#{item.id}/metadata"].get
37
- metadata = []
38
- JSON.parse(response).each do |m|
39
- metadata << DSpaceRest::Metadata.new(m)
40
- end
41
- metadata
42
- end
43
-
44
- def get_bitstreams_of(item, params)
45
- query_string = build_query_string(params)
46
- response = rest_client["/items/#{item.id}/bitstreams?#{query_string}"].get
47
- bitstreams = []
48
- JSON.parse(response).each do |bits|
49
- bitstreams << DSpaceRest::Bitstream.new(bits)
50
- end
51
- bitstreams
52
- end
53
-
54
- def create_metadata_for(item)
55
- form = JSON.generate(self.to_h["metadata"])
56
- response = rest_client["/items/#{item.id}/metadata"].put form
57
- end
58
-
59
- def create_bitstream_for(item, file, upload_strategy)
60
- response = upload_strategy.upload("/items/#{item.id}/bitstreams?name=#{file.name}&description=#{file.description}", file.path)
61
- DSpaceRest::Bitstream.new(JSON.parse(response))
62
- end
63
-
64
- def delete(item)
65
- response = rest_client["/items/#{item.id}"].delete
66
- end
67
-
68
- private
69
-
70
- def expandable_properties
71
- ["metadata","parentCollection","parentCollectionList","parentCommunityList","bitstreams","all"]
72
- end
73
-
74
- def query_parameters
75
- ["limit","offset"]
76
- end
77
-
78
- end
79
- end
80
- end