ruby-fedora 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +0,0 @@
1
- COPYING.LESSER.txt
2
- COPYING.txt
3
- History.txt
4
- License.txt
5
- Manifest.txt
6
- PostInstall.txt
7
- README.rdoc
8
- README.txt
9
- lib/fedora/base.rb
10
- lib/fedora/connection.rb
11
- lib/fedora/datastream.rb
12
- lib/fedora/fedora_object.rb
13
- lib/fedora/formats.rb
14
- lib/fedora/generic_search.rb
15
- lib/fedora/repository.rb
16
- lib/fedora/repository.rb.orig
17
- lib/ruby-fedora.rb
18
- lib/util/class_level_inheritable_attributes.rb
19
- tasks/rspec.rake
@@ -1,7 +0,0 @@
1
-
2
- For more information on rfed-regem, see http://rfed-regem.rubyforge.org
3
-
4
- NOTE: Change this information in PostInstall.txt
5
- You can also delete it if you don't want it.
6
-
7
-
@@ -1,48 +0,0 @@
1
- = rfed-regem
2
-
3
- * FIX (url)
4
-
5
- == DESCRIPTION:
6
-
7
- FIX (describe your package)
8
-
9
- == FEATURES/PROBLEMS:
10
-
11
- * FIX (list of features or problems)
12
-
13
- == SYNOPSIS:
14
-
15
- FIX (code sample of usage)
16
-
17
- == REQUIREMENTS:
18
-
19
- * FIX (list of requirements)
20
-
21
- == INSTALL:
22
-
23
- * FIX (sudo gem install, anything else)
24
-
25
- == LICENSE:
26
-
27
- (The MIT License)
28
-
29
- Copyright (c) 2009 FIXME full name
30
-
31
- Permission is hereby granted, free of charge, to any person obtaining
32
- a copy of this software and associated documentation files (the
33
- 'Software'), to deal in the Software without restriction, including
34
- without limitation the rights to use, copy, modify, merge, publish,
35
- distribute, sublicense, and/or sell copies of the Software, and to
36
- permit persons to whom the Software is furnished to do so, subject to
37
- the following conditions:
38
-
39
- The above copyright notice and this permission notice shall be
40
- included in all copies or substantial portions of the Software.
41
-
42
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,248 +0,0 @@
1
- require 'facets/hash/symbolize_keys'
2
-
3
- require 'fedora/base'
4
- require 'fedora/connection'
5
- require 'fedora/formats'
6
- require 'fedora/fedora_object'
7
- require 'fedora/datastream'
8
-
9
- module Fedora
10
- NAMESPACE = "fedora:info/"
11
- ALL_FIELDS = [
12
- :pid, :label, :fType, :cModel, :state, :ownerId, :cDate, :mDate, :dcmDate,
13
- :bMech, :title, :creator, :subject, :description, :contributor,
14
- :date, :type, :format, :identifier, :source, :language, :relation, :coverage, :rights
15
- ]
16
-
17
- class Repository
18
-
19
- attr_accessor :repository_name, :base_url, :fedora_version, :pid_namespace, :pid_delimiter
20
-
21
- def self.flush
22
- Thread.current[:repo]=nil
23
- end
24
- def self.register(url, surrogate=nil)
25
- repo = Fedora::Repository.new(url, surrogate)
26
- repo = Thread.current[:repo]
27
- attributes = repo.describe_repository
28
- repo.repository_name = attributes["repositoryName"].first
29
- repo.base_url = attributes["repositoryBaseURL"].first
30
- repo.fedora_version = attributes["repositoryVersion"].first
31
- repo.pid_namespace = attributes["repositoryPID"].first["PID-namespaceIdentifier"].first
32
- repo.pid_delimiter = attributes["repositoryPID"].first["PID-delimiter"].first
33
- Thread.current[:repo]=repo
34
- repo
35
- end
36
- def self.instance
37
- raise "did you register a repo?" unless Thread.current[:repo]
38
- Thread.current[:repo]
39
- end
40
- class StringResponse < String
41
- attr_reader :content_type
42
-
43
- def initialize(s, content_type)
44
- super(s)
45
- @content_type = content_type
46
- end
47
- end
48
-
49
- attr_accessor :fedora_url
50
-
51
- def initialize(fedora_url, surrogate=nil)
52
- @fedora_url = fedora_url.is_a?(URI) ? fedora_url : URI.parse(fedora_url)
53
- @surrogate = surrogate
54
- @connection = nil
55
- end
56
-
57
- # Fetch the raw content of either a fedora object or datastream
58
- def fetch_content(object_uri)
59
- response = connection.raw_get("#{url_for(object_uri)}?format=xml")
60
- StringResponse.new(response.body, response.content_type)
61
- end
62
-
63
-
64
-
65
- # Find fedora objects with http://www.fedora.info/wiki/index.php/API-A-Lite_findObjects
66
- #
67
- # == Parameters
68
- # query<String>:: the query string to be sent to Fedora.
69
- # options<Hash>:: see below
70
- #
71
- # == Options<Hash> keys
72
- # limit<String|Number>:: set the maxResults parameter in fedora
73
- # select<Symbol|Array>:: the fields to returned. To include all fields, pass :all as the value.
74
- # The field "pid" is always included.
75
- #
76
- # == Examples
77
- # find_objects("label=Image1"
78
- # find_objects("pid~demo:*", "label=test")
79
- # find_objects("label=Image1", :include => :all)
80
- # find_objects("label=Image1", :include => [:label])
81
- #-
82
- def find_objects(*args)
83
- raise ArgumentError, "Missing query string" unless args.length >= 1
84
- options = args.last.is_a?(Hash) ? args.pop : {}
85
-
86
- fields = options[:select]
87
- fields = (fields.nil? || (fields == :all)) ? ALL_FIELDS : ([:pid] + ([fields].flatten! - [:pid]))
88
-
89
- query = args.join(' ')
90
- params = { :format => 'xml', :query => query }
91
- params[:maxResults] = options[:limit] if options[:limit]
92
- params[:sessionToken] = options[:sessionToken] if options[:sessionToken]
93
- includes = fields.inject("") { |s, f| s += "&#{f}=true"; s }
94
-
95
- convert_xml(connection.get("#{fedora_url.path}/objects?#{params.to_fedora_query}#{includes}"))
96
- end
97
- def find_model(pid, klazz)
98
- obj = self.find_objects("pid=#{pid}").first
99
- doc = REXML::Document.new(obj.object_xml, :ignore_whitespace_nodes=>:all)
100
- klazz.deserialize(doc)
101
- end
102
-
103
- # Create the given object if it's new (not obtained from a find method). Otherwise update the object.
104
- #
105
- # == Return
106
- # boolean:: whether the operation is successful
107
- #-
108
- def save(object)
109
- object.new_object? ? create(object) : update(object)
110
- end
111
-
112
- def nextid
113
- d = REXML::Document.new(connection.post(fedora_url.path+"/management/getNextPID?xml=true").body)
114
- d.elements['//pid'].text
115
- end
116
-
117
-
118
- def create(object)
119
- case object
120
- when Fedora::FedoraObject
121
- pid = (object.pid ? object : 'new')
122
- response = connection.post("#{url_for(pid)}?" + object.attributes.to_fedora_query, object.blob)
123
- if response.code == '201'
124
- object.pid = extract_pid(response)
125
- object.new_object = false
126
- true
127
- else
128
- false
129
- end
130
- when Fedora::Datastream
131
- raise ArgumentError, "Missing dsID attribute" if object.dsid.nil?
132
- extra_headers = {}
133
- extra_headers['Content-Type'] = object.attributes[:mimeType] if object.attributes[:mimeType]
134
- response = connection.post("#{url_for(object)}?" + object.attributes.to_fedora_query,
135
- object.blob, extra_headers)
136
- if response.code == '201'
137
- object.new_object = false
138
- true
139
- else
140
- false
141
- end
142
- else
143
- raise ArgumentError, "Unknown object type"
144
- end
145
-
146
- end
147
-
148
- # Update the given object
149
- # == Return
150
- # boolean:: whether the operation is successful
151
- #-
152
- def update(object)
153
- raise ArgumentError, "Missing pid attribute" if object.nil? || object.pid.nil?
154
- case object
155
- when Fedora::FedoraObject
156
- response = connection.put("#{url_for(object)}?" + object.attributes.to_fedora_query)
157
- response.code == '307'
158
- when Fedora::Datastream
159
- raise ArgumentError, "Missing dsID attribute" if object.dsid.nil?
160
- response = connection.put("#{url_for(object)}?" + object.attributes.to_fedora_query, object.blob)
161
- response.code == '201'
162
- else
163
- raise ArgumentError, "Unknown object type"
164
- end
165
- end
166
-
167
- # Delete the given pid
168
- # == Parameters
169
- # object<Object|String>:: The object to delete.
170
- # This can be a uri String ("demo:1", "fedora:info/demo:1") or any object that responds uri method.
171
- #
172
- # == Return
173
- # boolean:: whether the operation is successful
174
- #-
175
- def delete(object)
176
- raise ArgumentError, "Object must not be nil" if object.nil?
177
- response = connection.delete("#{url_for(object)}")
178
- response.code == '200' or response.code == '204' # Temporary hack around error in Fedora 3.0 Final's REST API
179
- end
180
-
181
- # Fetch the given object using custom method. This is used to fetch other aspects of a fedora object,
182
- # such as profile, versions, etc...
183
- # == Parameters
184
- # object<String|Object>:: a fedora uri, pid, FedoraObject instance
185
- # method<Symbol>:: the method to fetch such as :export, :history, :versions, etc
186
- # extra_params<Hash>:: any other extra parameters to pass to fedora
187
- #
188
- # == Returns
189
- # This method returns raw xml response from the server
190
- #-
191
- def fetch_custom(object, method, extra_params = { :format => 'xml' })
192
- path = case method
193
- when :profile then ""
194
- else "/#{method}"
195
- end
196
-
197
- extra_params.delete(:format) if method == :export
198
- connection.raw_get("#{url_for(object)}#{path}?#{extra_params.to_fedora_query}").body
199
- end
200
-
201
- def describe_repository
202
- result_body = connection.raw_get("#{fedora_url.path}/describe?xml=true").body
203
- XmlSimple.xml_in(result_body)
204
- end
205
-
206
- private
207
- def convert_xml(response)
208
- results = FedoraObjects.new
209
- return results unless response && response['resultList']
210
-
211
- results.session_token = response['listSession']['token'] if response['listSession']
212
- objectFields = response['resultList']['objectFields']
213
- case objectFields
214
- when Array
215
- objectFields.each { |attrs| results << FedoraObject.new(attrs.symbolize_keys!) }
216
- when Hash
217
- results << FedoraObject.new(objectFields.symbolize_keys!)
218
- end
219
- results.each {|result| result.new_object = false}
220
- results
221
- end
222
-
223
- def url_for(object)
224
- uri = object.respond_to?(:uri) ? object.uri : object.to_s
225
- uri = (uri[0..NAMESPACE.length-1] == NAMESPACE ? uri[NAMESPACE.length..-1] : uri) # strip of fedora:info namespace
226
- "#{fedora_url.path}/objects/#{uri}"
227
- end
228
-
229
- # Low level access to the remote fedora server
230
- # The +refresh+ parameter toggles whether or not the connection is refreshed at every request
231
- # or not (defaults to +false+).
232
- def connection(refresh = false)
233
- if refresh || @connection.nil?
234
- @connection = Fedora::Connection.new(@fedora_url, Fedora::XmlFormat, @surrogate)
235
- end
236
- @connection
237
- end
238
-
239
- def extract_pid(response)
240
- CGI.unescape(response['Location'].split('/').last)
241
- end
242
- end
243
- end
244
-
245
- class FedoraObjects < Array
246
- attr_accessor :session_token
247
- end
248
-
@@ -1,21 +0,0 @@
1
- begin
2
- require 'spec'
3
- rescue LoadError
4
- require 'rubygems'
5
- require 'spec'
6
- end
7
- begin
8
- require 'spec/rake/spectask'
9
- rescue LoadError
10
- puts <<-EOS
11
- To use rspec for testing you must install rspec gem:
12
- gem install rspec
13
- EOS
14
- exit(0)
15
- end
16
-
17
- desc "Run the specs under spec/models"
18
- Spec::Rake::SpecTask.new do |t|
19
- t.spec_opts = ['--options', "spec/spec.opts"]
20
- t.spec_files = FileList['spec/**/*_spec.rb']
21
- end