sword2ruby 1.0.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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +30 -0
- data/LICENCE +35 -0
- data/README.md +43 -0
- data/Rakefile +53 -0
- data/doc/Atom/Collection.html +837 -0
- data/doc/Atom/Element.html +197 -0
- data/doc/Atom/Entry.html +1795 -0
- data/doc/Atom/Feed.html +297 -0
- data/doc/Atom/Service.html +363 -0
- data/doc/Atom.html +158 -0
- data/doc/Gemfile.html +125 -0
- data/doc/Object.html +165 -0
- data/doc/REXML/Element.html +493 -0
- data/doc/REXML.html +158 -0
- data/doc/Rakefile.html +123 -0
- data/doc/Sword2Ruby/Atom.html +169 -0
- data/doc/Sword2Ruby/AutoDiscover.html +327 -0
- data/doc/Sword2Ruby/Connection.html +274 -0
- data/doc/Sword2Ruby/DepositReceipt.html +331 -0
- data/doc/Sword2Ruby/Exception.html +168 -0
- data/doc/Sword2Ruby/SwordAccept.html +169 -0
- data/doc/Sword2Ruby/SwordStatementOAIORE.html +277 -0
- data/doc/Sword2Ruby/User.html +272 -0
- data/doc/Sword2Ruby/Utility.html +1101 -0
- data/doc/Sword2Ruby.html +213 -0
- data/doc/created.rid +29 -0
- data/doc/images/add.png +0 -0
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/delete.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_blue.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/transparent.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +118 -0
- data/doc/js/darkfish.js +153 -0
- data/doc/js/jquery.js +18 -0
- data/doc/js/navigation.js +142 -0
- data/doc/js/search.js +94 -0
- data/doc/js/search_index.js +1 -0
- data/doc/js/searcher.js +228 -0
- data/doc/rdoc.css +543 -0
- data/doc/spec/fixtures/example_txt.html +125 -0
- data/doc/table_of_contents.html +267 -0
- data/lib/sword2ruby/auto_discover.rb +71 -0
- data/lib/sword2ruby/collection.rb +261 -0
- data/lib/sword2ruby/connection.rb +37 -0
- data/lib/sword2ruby/constant.rb +35 -0
- data/lib/sword2ruby/deposit_receipt.rb +52 -0
- data/lib/sword2ruby/element.rb +12 -0
- data/lib/sword2ruby/entry.rb +602 -0
- data/lib/sword2ruby/exception.rb +7 -0
- data/lib/sword2ruby/feed.rb +34 -0
- data/lib/sword2ruby/rexml_element.rb +79 -0
- data/lib/sword2ruby/service.rb +79 -0
- data/lib/sword2ruby/sword_accept.rb +10 -0
- data/lib/sword2ruby/sword_statement_oai_ore.rb +38 -0
- data/lib/sword2ruby/user.rb +25 -0
- data/lib/sword2ruby/utility.rb +232 -0
- data/lib/sword2ruby/version.rb +4 -0
- data/lib/sword2ruby.rb +32 -0
- data/spec/auto_discover_spec.rb +30 -0
- data/spec/collection_spec.rb +33 -0
- data/spec/connection_spec.rb +21 -0
- data/spec/end_to_end_spec.rb +235 -0
- data/spec/fixtures/example.txt +3 -0
- data/spec/fixtures/snowflake.png +0 -0
- data/spec/fixtures/zip-test.zip +0 -0
- data/spec/service_spec.rb +38 -0
- data/spec/statement_spec.rb +53 -0
- data/spec/test_constants.rb +32 -0
- data/sword2ruby.gemspec +42 -0
- data/sword2ruby.tmproj +27 -0
- metadata +197 -0
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'rexml/element'
|
2
|
+
|
3
|
+
module Sword2Ruby
|
4
|
+
|
5
|
+
#Overloads for the rdf:Description tag (REXML::Element), used for describing OAI-ORE Sword Statements.
|
6
|
+
#
|
7
|
+
#For more information, see the Sword2 specification: {section 11.3. "OAI-ORE Serialisation"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_oaiore].
|
8
|
+
class ::REXML::Element
|
9
|
+
|
10
|
+
#This method returns the string value of the @rdf:about attribute for the <rdf:Description> element (i.e. for each element in the Sword2Ruby::SwordStatementOAIORE#rdf_descriptions array, usually contained in the OAI-ORE Sword Statement),
|
11
|
+
#or nil if it is not defined.
|
12
|
+
#
|
13
|
+
#For more information, see the Sword2 specification: {section 11.3. "OAI-ORE Serialisation"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_oaiore].
|
14
|
+
def rdf_about
|
15
|
+
self.attributes["rdf:about"]
|
16
|
+
end
|
17
|
+
|
18
|
+
#This method returns the string value of the @rdf:resource attribute for the <sword:originalDepsit> tag (usually contained in the OAI-ORE Sword Statement),
|
19
|
+
#or nil if it is not defined.
|
20
|
+
#
|
21
|
+
#For more information, see the Sword2 specification: {section 11.1.1. "sword:originalDeposit"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_predicates_originaldeposit].
|
22
|
+
def sword_original_deposit
|
23
|
+
Utility.find_element_attribute_value(self.elements, "sword:originalDeposit", "rdf:resource")
|
24
|
+
end
|
25
|
+
|
26
|
+
#This method returns the string value of the @rdf:resource attribute of the <sword:state> tag (usually contained in the OAI-ORE Sword Statement),
|
27
|
+
#or nil if it is not defined.
|
28
|
+
#
|
29
|
+
#For more information, see the Sword2 specification: {section 11.1.2. "sword:state"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_predicates_state].
|
30
|
+
def sword_state
|
31
|
+
Utility.find_element_attribute_value(self.elements, "sword:state", "rdf:resource")
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
#This method returns an array of the string values of the @rdf:resource attribute for the <sword:packaging> tags (usually contained in the OAI-ORE Sword Statement),
|
37
|
+
#or an empty array [ ] if none are defined.
|
38
|
+
#
|
39
|
+
#For more information, see the Sword2 specification: {section 11.1.3. "sword:packaging"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_predicates_packaging].
|
40
|
+
def sword_packagings
|
41
|
+
Utility.find_elements_attribute_value(self.elements, "sword:packaging", "rdf:resource")
|
42
|
+
end
|
43
|
+
|
44
|
+
#This method returns the Time value of the <sword:depositedOn> tag (usually contained in the OAI-ORE Sword Statement),
|
45
|
+
#or nil if it is not defined.
|
46
|
+
#
|
47
|
+
#For more information, see the Sword2 specification: {section 11.1.4. "sword:depositedOn"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_predicates_depositedon].
|
48
|
+
def sword_deposited_on
|
49
|
+
Utility.find_element_time(self.elements, "sword:depositedOn")
|
50
|
+
end
|
51
|
+
|
52
|
+
#This method returns the string value of the <sword:depositedBy> tag (usually contained in the OAI-ORE Sword Statement),
|
53
|
+
#or nil if it is not defined.
|
54
|
+
#
|
55
|
+
#For more information, see the Sword2 specification: {section 11.1.5. "sword:depositedBy"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_predicates_depositedby].
|
56
|
+
def sword_deposited_by
|
57
|
+
Utility.find_element_text(self.elements, "sword:depositedBy")
|
58
|
+
end
|
59
|
+
|
60
|
+
#This method returns the string value of the <sword:depositedOnBehalfOf> tag (usually contained in the OAI-ORE Sword Statement),
|
61
|
+
#or nil if it is not defined.
|
62
|
+
#
|
63
|
+
#For more information, see the Sword2 specification: {section 11.1.6. "sword:depositedOnBehalfOf"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_predicates_obo].
|
64
|
+
def sword_deposited_on_behalf_of
|
65
|
+
Utility.find_element_text(self.elements, "sword:depositedOnBehalfOf")
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
#This method returns the string value of the <sword:stateDescription> tag (usually contained in the OAI-ORE Sword Statement),
|
70
|
+
#or nil if it is not defined.
|
71
|
+
#
|
72
|
+
#For more information, see the Sword2 specification: {section 11.1.7. "sword:stateDescription"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_predicates_description].
|
73
|
+
def sword_state_description
|
74
|
+
Utility.find_element_text(self.elements, "sword:stateDescription")
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'atom/service'
|
2
|
+
|
3
|
+
module Sword2Ruby
|
4
|
+
|
5
|
+
#Extend existing Atom::Service with Sword methods
|
6
|
+
#
|
7
|
+
#For more information, see the Sword2 specification: {section 6.1. "Retrieving a Service Document"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#protocoloperations_retreivingservicedocument].
|
8
|
+
class ::Atom::Service < ::Atom::Element
|
9
|
+
|
10
|
+
#This method returns the URI object of the service document.
|
11
|
+
def service_document_uri
|
12
|
+
base.to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
#This method returns the string value of the <sword:version> tag (usually contained in the Service Document),
|
16
|
+
#or nil if it is not defined.
|
17
|
+
def sword_version
|
18
|
+
Utility.find_element_text(extensions, "sword:version")
|
19
|
+
end
|
20
|
+
|
21
|
+
#This method returns the integer value of the <sword:maxUploadSize> tag (usually contained in the Service Document),
|
22
|
+
#or 0 if it is not defined.
|
23
|
+
def sword_max_upload_size
|
24
|
+
Utility.find_element_integer(extensions, "sword:maxUploadSize")
|
25
|
+
end
|
26
|
+
|
27
|
+
#Retrieves and parses an Atom service document.
|
28
|
+
#===Parameters
|
29
|
+
#:\service_uri:: The URI to the Sword Service Document.
|
30
|
+
#:connection:: (optional) Sword2Ruby::Connection object used to perform the operation. If not supplied, a new Connection object will be created.
|
31
|
+
def initialize(service_uri, connection = Connection.new())
|
32
|
+
Utility.check_argument_class('service_uri', service_uri, String)
|
33
|
+
Utility.check_uri(service_uri)
|
34
|
+
Utility.check_argument_class('connection', connection, Connection)
|
35
|
+
|
36
|
+
|
37
|
+
super()
|
38
|
+
|
39
|
+
@http = connection
|
40
|
+
|
41
|
+
return if service_uri.empty?
|
42
|
+
|
43
|
+
base = URI.parse(service_uri)
|
44
|
+
|
45
|
+
rxml = nil
|
46
|
+
|
47
|
+
res = connection.get(base, "Accept" => "application/atomsvc+xml")
|
48
|
+
res.validate_content_type(["application/atomsvc+xml"])
|
49
|
+
|
50
|
+
if res.is_a? Net::HTTPSuccess
|
51
|
+
service = self.class.parse(res.body, base, self)
|
52
|
+
|
53
|
+
#Update workspaces, collections and their feeds to use the Service's http connection
|
54
|
+
set_http(connection)
|
55
|
+
|
56
|
+
service
|
57
|
+
else
|
58
|
+
raise Sword2Ruby::Exception.new("Failed to do get(#{service_uri}): server returned #{res.code} #{res.message}")
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
private
|
65
|
+
#Update workspaces, collections and their feeds to use the Service's http connection
|
66
|
+
#Unfortunately this is necessary because the atom-tools library does not propagate the
|
67
|
+
#http connection object from Service to Workspace
|
68
|
+
def set_http(connection)
|
69
|
+
workspaces.each do |w|
|
70
|
+
w.http = connection
|
71
|
+
w.collections.each do |c|
|
72
|
+
c.http = connection
|
73
|
+
c.feed.http = connection
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end #class
|
79
|
+
end #module
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Sword2Ruby
|
2
|
+
|
3
|
+
#This class overrides the Atom <app:accept> class and ensures that the "alternate" attribute
|
4
|
+
#is preserved in the tag, required for processing Sword documents.
|
5
|
+
class SwordAccept < ::Atom::Text
|
6
|
+
is_element ATOM_PUBLISHING_PROTOCOL_NAMESPACE, :accept
|
7
|
+
attrb ATOM_PUBLISHING_PROTOCOL_NAMESPACE, :alternate
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rexml/document'
|
2
|
+
|
3
|
+
module Sword2Ruby
|
4
|
+
#This class models the OAI-ORE Sword Statement.
|
5
|
+
#
|
6
|
+
#For more information, see the Sword2 specification: {section 11.3. "OAI-ORE Serialisation"}[http://sword-app.svn.sourceforge.net/viewvc/sword-app/spec/tags/sword-2.0/SWORDProfile.html?revision=377#statement_oaiore].
|
7
|
+
class SwordStatementOAIORE
|
8
|
+
|
9
|
+
#The complete statement document, returned as a REXML::Document.
|
10
|
+
attr_reader :statement_document
|
11
|
+
|
12
|
+
#An array of rdf:descriptions. See the overload methods in REXML::Element for each description's methods.
|
13
|
+
attr_reader :rdf_descriptions
|
14
|
+
|
15
|
+
#Creates a new SwordStatementOAIORE object, using the supplied URI and connection object.
|
16
|
+
#===Parameters
|
17
|
+
#sword_statement_uri:: The URI string to the OAI-ORE Sword Statement.
|
18
|
+
#connection:: Sword2Ruby::Connection object used to perform the operation.
|
19
|
+
def initialize(sword_statement_uri, connection)
|
20
|
+
#Validate parameters
|
21
|
+
Utility.check_argument_class('sword_statement_uri', sword_statement_uri, String)
|
22
|
+
Utility.check_argument_class('connection', connection, Connection)
|
23
|
+
|
24
|
+
response = connection.get(sword_statement_uri)
|
25
|
+
|
26
|
+
if response.is_a? Net::HTTPSuccess
|
27
|
+
@statement_document = REXML::Document.new(response.body)
|
28
|
+
@rdf_descriptions = []
|
29
|
+
@statement_document.elements.each("/rdf:RDF/rdf:Description") do |description|
|
30
|
+
@rdf_descriptions << description
|
31
|
+
end
|
32
|
+
else
|
33
|
+
raise Sword2Ruby::Exception.new("Failed to do get(#{sword_statement_uri}): server returned code #{response.code} #{response.message}")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Sword2Ruby
|
2
|
+
class User
|
3
|
+
|
4
|
+
#Username for authenticating with the Sword server
|
5
|
+
attr_accessor :username
|
6
|
+
|
7
|
+
#On-Behalf-Of username for authenticating with the Sword server
|
8
|
+
attr_accessor :on_behalf_of
|
9
|
+
|
10
|
+
#Password for authenticating with the Sword server
|
11
|
+
attr_accessor :password
|
12
|
+
|
13
|
+
#Creates a new User object.
|
14
|
+
#===Parameters
|
15
|
+
#username:: (optional) string value of the username.
|
16
|
+
#password:: (optional) string value of the password.
|
17
|
+
#on_behalf_of:: (optional) string value indicating the username on whos behalf the actions will be performed.
|
18
|
+
def initialize(username=nil, password=nil, on_behalf_of=nil)
|
19
|
+
@username = username;
|
20
|
+
@password = password;
|
21
|
+
@on_behalf_of = on_behalf_of;
|
22
|
+
end
|
23
|
+
|
24
|
+
end #class
|
25
|
+
end #module
|
@@ -0,0 +1,232 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'digest/md5'
|
3
|
+
|
4
|
+
module Sword2Ruby
|
5
|
+
class Utility
|
6
|
+
|
7
|
+
#Method to cast a (typically) string value into a boolean value
|
8
|
+
#===Parameters
|
9
|
+
#value:: the string value to be cast, e.g. "true" or "no"
|
10
|
+
#nil_value:: (optional) value to return if value=nil. If not supplied, this will default to false. Typically used in circumstances when returning something other than false would be preferable if value=nil.
|
11
|
+
def self.to_boolean(value, nil_value = false)
|
12
|
+
value.downcase! if value.class == String
|
13
|
+
case value
|
14
|
+
when "no","false",false, "0", 0
|
15
|
+
false
|
16
|
+
when "yes","true",true, "1", 1
|
17
|
+
true
|
18
|
+
when nil
|
19
|
+
nil_value
|
20
|
+
else
|
21
|
+
!!value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
#Method to check the supplied argument is the expected class.
|
26
|
+
#===Parameters
|
27
|
+
#name:: the string name of the *argument* parameter, e.g. "update_time" or "username"
|
28
|
+
#argument:: the object to be checked against *expected_class*, e.g. update_time or username
|
29
|
+
#expected_class:: the class used to validate *argument*, e.g. Time or String
|
30
|
+
#It will just return (nothing) if the check passes, or raise an ArgumentError if the check fails.
|
31
|
+
#===Example
|
32
|
+
# # This is fine
|
33
|
+
# on_behalf_of = "joebloggs"
|
34
|
+
# Utility.check_argument_class('on_behalf_of', on_behalf_of, String)
|
35
|
+
#
|
36
|
+
# # This will raise an ArgumentError, because on_behalf_of is not a String
|
37
|
+
# on_behalf_of = 12345
|
38
|
+
# Utility.check_argument_class('on_behalf_of', on_behalf_of, String) #Raises an ArgumentError!
|
39
|
+
def self.check_argument_class(name, argument, expected_class)
|
40
|
+
unless argument.is_a? expected_class
|
41
|
+
raise ArgumentError.new("Argument '#{name}=#{argument}' must be of type '#{expected_class}' (and not of type '#{argument.class}')")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
#Method to check the supplied uri uses one of the URI schemes defined in Sword2Ruby::VALID_URI_SCHEMES.
|
46
|
+
#===Parameters
|
47
|
+
#uri:: the string URI to be checked
|
48
|
+
#It will just return (nothing) if the check passes, or raise an InvalidURIError if the check fails.
|
49
|
+
#It will raise an ArgumentError if *uri* is not a String. It wil raise a URI parse error if *uri* could not be parsed.
|
50
|
+
#===Example
|
51
|
+
# # This is fine
|
52
|
+
# Utility.check_uri('http://www.url.com')
|
53
|
+
#
|
54
|
+
# # This will raise an InvalidURIError, because fishy:// is not a valid URI scheme
|
55
|
+
# Utility.check_uri('fishy://www.url.com')
|
56
|
+
def self.check_uri(uri)
|
57
|
+
self.check_argument_class("uri", uri, String)
|
58
|
+
unless VALID_URI_SCHEMES.include? URI.parse(uri).scheme.downcase.to_sym
|
59
|
+
raise URI::InvalidURIError, "Invalid URI, it must start with either [#{VALID_URI_SCHEMES.join(',')}] (current value: #{uri})"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
#Find link methods
|
64
|
+
|
65
|
+
#Method to return the @href attribute of the Atom::Link found in an array of Atom::Links by its @rel and @type attributes.
|
66
|
+
#===Parameters
|
67
|
+
#links:: an array of Atom::Links
|
68
|
+
#rel:: the string @rel value to search the array for
|
69
|
+
#type:: (optional) the string @type value to search the array for, defaults to nil
|
70
|
+
#It will return the href value of the first link found with matching @rel and @type values, othewise nil.
|
71
|
+
def self.find_link_uri(links, rel, type = nil)
|
72
|
+
links.find(NIL_LAMBDA) {|link| link.rel == rel && link.type == type}.href
|
73
|
+
end
|
74
|
+
|
75
|
+
#Method to return an array of Atom::Links found in an array of Atom::Links by their @rel and @type attributes.
|
76
|
+
#===Parameters
|
77
|
+
#links:: an array of Atom::Links
|
78
|
+
#rel:: the string @rel value to search the array for
|
79
|
+
#type:: (optional) the string @type value to search the array for, defaults to nil
|
80
|
+
#It will return an array of all matching Atom:Links (found with @rel and @type values), othewise an empty array.
|
81
|
+
def self.find_links(links, rel, type = nil)
|
82
|
+
links.find_all{|link| link.rel == rel && link.type == type}
|
83
|
+
end
|
84
|
+
|
85
|
+
#Method to return an array of Atom::Links found in an array of Atom::Links by their @rel attributes.
|
86
|
+
#===Parameters
|
87
|
+
#links:: an array of Atom::Links
|
88
|
+
#rel:: the string @rel value to search the array for
|
89
|
+
#It will return an array of all matching Atom:Links (found with @rel), othewise an empty array.
|
90
|
+
def self.find_links_all_types(links, rel)
|
91
|
+
links.find_all{|link| link.rel == rel}
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
#Find single element methods
|
96
|
+
|
97
|
+
#Method to return the first Atom::Element found in an array of Atom::Elements by its name.
|
98
|
+
#===Parameters
|
99
|
+
#elements:: an array of Atom::Elements
|
100
|
+
#name:: the expanded name of the element to search for, e.g. "sword:state" or "sword:verboseDescription"
|
101
|
+
#It will return the first element found with a matching name, othewise nil.
|
102
|
+
def self.find_element_by_name(elements, name)
|
103
|
+
elements.find(NIL_LAMBDA) {|e| e.fully_expanded_name == name}
|
104
|
+
end
|
105
|
+
|
106
|
+
#Method to return the text value of first Atom::Element found in an array of Atom::Elements by its name.
|
107
|
+
#===Parameters
|
108
|
+
#elements:: an array of Atom::Elements
|
109
|
+
#name:: the expanded name of the element to search for, e.g. "sword:state" or "sword:verboseDescription"
|
110
|
+
#It will return the text value of first element found with a matching name, othewise nil.
|
111
|
+
def self.find_element_text(elements, name)
|
112
|
+
find_element_by_name(elements, name).text
|
113
|
+
end
|
114
|
+
|
115
|
+
#Method to return the integer value of first Atom::Element found in an array of Atom::Elements by its name.
|
116
|
+
#===Parameters
|
117
|
+
#elements:: an array of Atom::Elements
|
118
|
+
#name:: the expanded name of the element to search for, e.g. "sword:state" or "sword:verboseDescription"
|
119
|
+
#It will return the integer value of first element found with a matching name, othewise 0.
|
120
|
+
def self.find_element_integer(elements, name)
|
121
|
+
find_element_text(elements, name).to_i
|
122
|
+
end
|
123
|
+
|
124
|
+
#Method to return the boolean value of first Atom::Element found in an array of Atom::Elements by its name.
|
125
|
+
#===Parameters
|
126
|
+
#elements:: an array of Atom::Elements
|
127
|
+
#name:: the expanded name of the element to search for, e.g. "sword:state" or "sword:verboseDescription"
|
128
|
+
#It will return the boolean value of first element found with a matching name, othewise nil.
|
129
|
+
def self.find_element_boolean(elements, name)
|
130
|
+
self.to_boolean(find_element_text(elements, name), nil)
|
131
|
+
end
|
132
|
+
|
133
|
+
#Method to return the time value of first Atom::Element found in an array of Atom::Elements by its name.
|
134
|
+
#===Parameters
|
135
|
+
#elements:: an array of Atom::Elements
|
136
|
+
#name:: the expanded name of the element to search for, e.g. "sword:state" or "sword:verboseDescription"
|
137
|
+
#It will return the time value of first element found with a matching name, othewise nil.
|
138
|
+
def self.find_element_time(elements, name)
|
139
|
+
value = find_element_text(elements, name)
|
140
|
+
value.nil? ? nil : Time.parse(find_element_text(elements, name))
|
141
|
+
end
|
142
|
+
|
143
|
+
#Method to return the attribute value of first Atom::Element found in an array of Atom::Elements by its name.
|
144
|
+
#===Parameters
|
145
|
+
#elements:: an array of Atom::Elements
|
146
|
+
#name:: the expanded name of the element to search for, e.g. "sword:state" or "sword:verboseDescription"
|
147
|
+
#attribute_name:: the name of the attribute to return the value of, e.g. "href"
|
148
|
+
#It will return the value of the specified attribute of the first element found with a matching name, othewise nil.
|
149
|
+
def self.find_element_attribute_value(elements, name, attribute_name)
|
150
|
+
elements.find(NIL_LAMBDA) {|e| e.fully_expanded_name == name}.attribute(attribute_name).value
|
151
|
+
end
|
152
|
+
|
153
|
+
#Method to return the first Atom::Element found in an array of Atom::Elements by its scheme and term.
|
154
|
+
#===Parameters
|
155
|
+
#elements:: an array of Atom::Elements
|
156
|
+
#scheme:: the scheme of the element to search for, e.g. "http://purl.org/net/sword/terms/"
|
157
|
+
#term:: the term of the element to search for, e.g. "http://purl.org/net/sword/terms/originalDeposit"
|
158
|
+
#It will return the first element found with a matching scheme and term, othewise nil.
|
159
|
+
def self.find_element_by_scheme_and_term(elements, scheme, term)
|
160
|
+
elements.find {|e| e.scheme == scheme && e.term == term}
|
161
|
+
end
|
162
|
+
|
163
|
+
#Method to return the first Atom::Element found in an array of Atom::Elements with a matching @href attribute.
|
164
|
+
#===Parameters
|
165
|
+
#elements:: an array of Atom::Elements
|
166
|
+
#href:: the value of the @href attribute of the element to search for, e.g. "http://purl.org/net/sword/terms/originalDeposit"
|
167
|
+
#It will return the first element found with a matching href attribute, othewise nil.
|
168
|
+
def self.find_element_by_href(elements, href)
|
169
|
+
elements.find {|e| e.href == href}
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
#Find multiple elements
|
174
|
+
|
175
|
+
#Method to return an array of text values of matching Atom::Elements found in an array of Atom::Elements by their name.
|
176
|
+
#===Parameters
|
177
|
+
#elements:: an array of Atom::Elements
|
178
|
+
#name:: the expanded name of the element(s) to search for, e.g. "sword:state" or "sword:verboseDescription"
|
179
|
+
#It will return an array of the text values of elements found with a matching name, othewise an empty array [ ].
|
180
|
+
def self.find_elements_text(elements, name)
|
181
|
+
elements.find_all {|e| e.fully_expanded_name==name}.collect {|e| e.text}
|
182
|
+
end
|
183
|
+
|
184
|
+
#Method to return an array of attribute values of matching Atom::Elements found in an array of Atom::Elements by their name.
|
185
|
+
#===Parameters
|
186
|
+
#elements:: an array of Atom::Elements
|
187
|
+
#name:: the expanded name of the element(s) to search for, e.g. "sword:state" or "sword:verboseDescription"
|
188
|
+
#attribute_name:: the name of the attributes to return the value of, e.g. "href"
|
189
|
+
#It will return an array of the attribute values of elements found with a matching name, othewise an empty array [ ].
|
190
|
+
def self.find_elements_attribute_value(elements, name, attribute_name)
|
191
|
+
elements.find_all {|e| e.fully_expanded_name==name}.collect {|e| e.attribute(attribute_name).value}
|
192
|
+
end
|
193
|
+
|
194
|
+
#Method to return an array of matching Atom::Elements found in an array of Atom::Elements by their namespace.
|
195
|
+
#===Parameters
|
196
|
+
#elements:: an array of Atom::Elements
|
197
|
+
#namespace:: the namespace to search for, e.g. "http://purl.org/dc/terms/"
|
198
|
+
#It will return an array of Atom::Elements found with a matching namespace, othewise an empty array [ ].
|
199
|
+
def self.find_elements_by_namespace(elements, namespace)
|
200
|
+
elements.find_all {|e| e.namespace == namespace}
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
#Method to return an array of matching Atom::Elements found in an array of Atom::Elements by their namespace.
|
205
|
+
#===Parameters
|
206
|
+
#elements:: an array of Atom::Elements
|
207
|
+
#scheme:: the scheme to search for, e.g. "http://purl.org/net/sword/terms/state"
|
208
|
+
#It will return an array of Atom::Elements found with a matching scheme, othewise an empty array [ ].
|
209
|
+
def self.find_elements_by_scheme(elements, scheme)
|
210
|
+
elements.find_all {|e| e.scheme == scheme}
|
211
|
+
end
|
212
|
+
|
213
|
+
#Method to return the filename, MD5 hash and filedata of the supplied filepath.
|
214
|
+
#===Parameters
|
215
|
+
#filepath:: path to an existing file which must be readable to the Ruby process.
|
216
|
+
#Returns an array of the form [filename, md5_digest, data]
|
217
|
+
def self.read_file(filepath)
|
218
|
+
data = nil
|
219
|
+
File.open(filepath,'r') do |file|
|
220
|
+
data = file.gets(nil) #Read entire file, no Base64.encode64()
|
221
|
+
end #file is closed automatically
|
222
|
+
return [File.basename(filepath), Digest::MD5.hexdigest(data), data]
|
223
|
+
end
|
224
|
+
|
225
|
+
#:nodoc:
|
226
|
+
private
|
227
|
+
def initialize()
|
228
|
+
super
|
229
|
+
end
|
230
|
+
|
231
|
+
end #class
|
232
|
+
end #module
|
data/lib/sword2ruby.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "sword2ruby/version"
|
2
|
+
require "sword2ruby/constant"
|
3
|
+
require "sword2ruby/utility"
|
4
|
+
require "sword2ruby/exception"
|
5
|
+
|
6
|
+
require "sword2ruby/user"
|
7
|
+
require "sword2ruby/connection"
|
8
|
+
require "sword2ruby/auto_discover"
|
9
|
+
|
10
|
+
require "sword2ruby/rexml_element"
|
11
|
+
require "sword2ruby/element"
|
12
|
+
require "sword2ruby/entry"
|
13
|
+
require "sword2ruby/sword_accept"
|
14
|
+
require "sword2ruby/deposit_receipt"
|
15
|
+
require "sword2ruby/feed"
|
16
|
+
require "sword2ruby/collection"
|
17
|
+
require "sword2ruby/service"
|
18
|
+
|
19
|
+
require "sword2ruby/sword_statement_oai_ore"
|
20
|
+
|
21
|
+
#Sword2Ruby module
|
22
|
+
module Sword2Ruby
|
23
|
+
|
24
|
+
|
25
|
+
#Extensions to the {Atom module}[https://github.com/bct/atom-tools/wiki] to support Sword2 operations.
|
26
|
+
#These methods are additive to those supplied by the atom-tools gem.
|
27
|
+
#
|
28
|
+
#For more information on the {Atom module}[https://github.com/bct/atom-tools/wiki], please see the {atom-tools documentation}[http://rdoc.info/github/bct/atom-tools/master/frames] for a complete list of attributes and methods.
|
29
|
+
module Atom
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#auto_discover_spec.rb
|
2
|
+
|
3
|
+
require 'test_constants'
|
4
|
+
|
5
|
+
|
6
|
+
describe Sword2Ruby::AutoDiscover do
|
7
|
+
|
8
|
+
it "Testing AutoDiscover on Service html" do
|
9
|
+
autodiscover = Sword2Ruby::AutoDiscover.new(TEST_AUTODISCOVER_SERVICE_HTML_URI)
|
10
|
+
|
11
|
+
#Test that the service_document_uri was found
|
12
|
+
autodiscover.service_document_uri.should_not be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "Testing AutoDiscover on Collection html" do
|
16
|
+
autodiscover = Sword2Ruby::AutoDiscover.new(TEST_AUTODISCOVER_COLLECTION_HTML_URI)
|
17
|
+
|
18
|
+
#Test that the collection deposit_endpoint_uri was found
|
19
|
+
autodiscover.deposit_endpoint_uri.should_not be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "Testing AutoDiscover on Entry html" do
|
23
|
+
autodiscover = Sword2Ruby::AutoDiscover.new(TEST_AUTODISCOVER_ENTRY_HTML_URI)
|
24
|
+
|
25
|
+
#Test that the collection deposit_endpoint_uri was found
|
26
|
+
autodiscover.entry_edit_uris.count.should > 0
|
27
|
+
autodiscover.sword_statement_links.count.should > 0
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#collection_spec.rb
|
2
|
+
|
3
|
+
require 'test_constants'
|
4
|
+
|
5
|
+
describe ::Atom::Collection do
|
6
|
+
|
7
|
+
it "Get the test collection, post an entry to it" do
|
8
|
+
current_collection = ::Atom::Collection.new(TEST_COLLECTION_HREF, TEST_CONNECTION_VALID)
|
9
|
+
current_collection.should_not be_nil
|
10
|
+
current_collection.href.should == TEST_COLLECTION_HREF
|
11
|
+
current_collection.collection_uri.should == TEST_COLLECTION_HREF
|
12
|
+
|
13
|
+
current_feed = current_collection.feed
|
14
|
+
current_feed.update!
|
15
|
+
current_feed.entries.count.should >= 0
|
16
|
+
|
17
|
+
entry = Atom::Entry.new()
|
18
|
+
entry.title = "Test Entry created by Sword2Ruby Collection test"
|
19
|
+
entry.summary = "This entry was created during a test on #{Time.now}"
|
20
|
+
entry.updated = Time.now
|
21
|
+
|
22
|
+
#Generate a slug based on the date and time
|
23
|
+
current_slug = "sword2ruby_test_#{Time.now.strftime("%FT%H-%M-%S")}"
|
24
|
+
|
25
|
+
deposit_receipt = current_collection.post!(:entry=>entry, :slug=>current_slug, :in_progress=>false)
|
26
|
+
|
27
|
+
#There SHOULD be a deposit receipt entry received from the Sword server
|
28
|
+
deposit_receipt.has_entry.should == true
|
29
|
+
deposit_receipt.entry.should_not be_nil
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#connection_spec.rb
|
2
|
+
require 'test_constants'
|
3
|
+
|
4
|
+
|
5
|
+
describe Sword2Ruby::Connection do
|
6
|
+
|
7
|
+
it "initialise without parameters" do
|
8
|
+
connection = Sword2Ruby::Connection.new()
|
9
|
+
connection.user.should == nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "initialise with invalid parameters" do
|
13
|
+
expect{Sword2Ruby::Connection.new(12345)}.to raise_error(ArgumentError);
|
14
|
+
end
|
15
|
+
|
16
|
+
it "try to retrieve malformed URI" do
|
17
|
+
expect{TEST_CONNECTION_VALID.get(TEST_SERVICE_DOCUMENT_URI_MALFORMED)}.to raise_error(URI::InvalidURIError);
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
end
|