Class: Fedora::Repository
- Inherits:
-
Object
- Object
- Fedora::Repository
- Defined in:
- lib/fedora/repository.rb
Defined Under Namespace
Classes: StringResponse
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#fedora_url ⇒ Object
Returns the value of attribute fedora_url.
-
#fedora_version ⇒ Object
Returns the value of attribute fedora_version.
-
#pid_delimiter ⇒ Object
Returns the value of attribute pid_delimiter.
-
#pid_namespace ⇒ Object
Returns the value of attribute pid_namespace.
-
#repository_name ⇒ Object
Returns the value of attribute repository_name.
Class Method Summary collapse
Instance Method Summary collapse
- #create(object) ⇒ Object
-
#delete(object) ⇒ Object
- Delete the given pid == Parameters object<Object|String>
-
The object to delete.
- #describe_repository ⇒ Object
-
#export(object, extra_params = {}) ⇒ Object
- Export the given object == Parameters object<String|Object>
- a fedora uri, pid, FedoraObject instance method<Symbol>
- the method to fetch such as :export, :history, :versions, etc extra_params<Hash>
-
any other extra parameters to pass to fedora.
-
#fetch_content(object_uri) ⇒ Object
Fetch the raw content of either a fedora object or datastream.
-
#fetch_custom(object, method, extra_params = { :format => 'xml' }) ⇒ Object
Fetch the given object using custom method.
-
#find_model(pid, klazz) ⇒ Object
Retrieve an object from fedora and load it as an instance of the given model/class.
-
#find_objects(*args) ⇒ Object
Find fedora objects with www.fedora.info/wiki/index.php/API-A-Lite_findObjects.
- #ingest(content_to_ingest, extra_params = {}) ⇒ Object
-
#initialize(fedora_url, surrogate = nil) ⇒ Repository
constructor
A new instance of Repository.
- #nextid(attrs = {}) ⇒ Object
-
#save(object) ⇒ Object
Create the given object if it’s new (not obtained from a find method).
-
#update(object) ⇒ Object
- Update the given object == Return boolean
-
whether the operation is successful -.
Constructor Details
#initialize(fedora_url, surrogate = nil) ⇒ Repository
Returns a new instance of Repository.
54 55 56 57 58 |
# File 'lib/fedora/repository.rb', line 54 def initialize(fedora_url, surrogate=nil) @fedora_url = fedora_url.is_a?(URI) ? fedora_url : URI.parse(fedora_url) @surrogate = surrogate @connection = nil end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
19 20 21 |
# File 'lib/fedora/repository.rb', line 19 def base_url @base_url end |
#fedora_url ⇒ Object
Returns the value of attribute fedora_url.
52 53 54 |
# File 'lib/fedora/repository.rb', line 52 def fedora_url @fedora_url end |
#fedora_version ⇒ Object
Returns the value of attribute fedora_version.
19 20 21 |
# File 'lib/fedora/repository.rb', line 19 def fedora_version @fedora_version end |
#pid_delimiter ⇒ Object
Returns the value of attribute pid_delimiter.
19 20 21 |
# File 'lib/fedora/repository.rb', line 19 def pid_delimiter @pid_delimiter end |
#pid_namespace ⇒ Object
Returns the value of attribute pid_namespace.
19 20 21 |
# File 'lib/fedora/repository.rb', line 19 def pid_namespace @pid_namespace end |
#repository_name ⇒ Object
Returns the value of attribute repository_name.
19 20 21 |
# File 'lib/fedora/repository.rb', line 19 def repository_name @repository_name end |
Class Method Details
.flush ⇒ Object
21 22 23 |
# File 'lib/fedora/repository.rb', line 21 def self.flush Thread.current[:repo]=nil end |
.instance ⇒ Object
39 40 41 42 |
# File 'lib/fedora/repository.rb', line 39 def self.instance raise "did you register a repo?" unless Thread.current[:repo] Thread.current[:repo] end |
.register(url, surrogate = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fedora/repository.rb', line 24 def self.register(url, surrogate=nil) url = url.to_s.chop if url.to_s =~ /\/\Z/ Thread.current[:repo]= Fedora::Repository.new(url, surrogate) begin repo = Thread.current[:repo] attributes = repo.describe_repository repo.repository_name = attributes["repositoryName"].first repo.base_url = attributes["repositoryBaseURL"].first repo.fedora_version = attributes["repositoryVersion"].first repo.pid_namespace = attributes["repositoryPID"].first["PID-namespaceIdentifier"].first repo.pid_delimiter = attributes["repositoryPID"].first["PID-delimiter"].first rescue end Thread.current[:repo] end |
Instance Method Details
#create(object) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/fedora/repository.rb', line 131 def create(object) case object when Fedora::FedoraObject pid = (object.pid ? object : 'new') response = connection.post("#{url_for(pid)}?" + object.attributes.to_fedora_query, object.blob) if response.code == '201' object.pid = extract_pid(response) object.new_object = false true else false end when Fedora::Datastream raise ArgumentError, "Missing dsID attribute" if object.dsid.nil? extra_headers = {} extra_headers['Content-Type'] = object.attributes[:mimeType] if object.attributes[:mimeType] response = connection.post("#{url_for(object)}?" + object.attributes.to_fedora_query, object.blob, extra_headers) if response.code == '201' object.new_object = false true else false end else raise ArgumentError, "Unknown object type" end end |
#delete(object) ⇒ Object
Delete the given pid
Parameters
- object<Object|String>
-
The object to delete.
This can be a uri String ("demo:1", "fedora:info/demo:1") or any object that responds uri method.
Return
- boolean
-
whether the operation is successful
-
189 190 191 192 193 |
# File 'lib/fedora/repository.rb', line 189 def delete(object) raise ArgumentError, "Object must not be nil" if object.nil? response = connection.delete("#{url_for(object)}") response.code == '200' or response.code == '204' # Temporary hack around error in Fedora 3.0 Final's REST API end |
#describe_repository ⇒ Object
251 252 253 254 |
# File 'lib/fedora/repository.rb', line 251 def describe_repository result_body = connection.raw_get("#{fedora_url.path}/describe?xml=true").body XmlSimple.xml_in(result_body) end |
#export(object, extra_params = {}) ⇒ Object
Export the given object
Parameters
- object<String|Object>
-
a fedora uri, pid, FedoraObject instance
- method<Symbol>
-
the method to fetch such as :export, :history, :versions, etc
- extra_params<Hash>
-
any other extra parameters to pass to fedora
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/fedora/repository.rb', line 201 def export(object, extra_params={}) extra_params = {:format=>:foxml, :context=>:archive}.merge!(extra_params) if extra_params[:format].kind_of?(String) format = extra_params[:format] else format = case extra_params[:format] when :atom then "info:fedora/fedora-system:ATOM-1.1" when :atom_zip then "info:fedora/fedora-system:ATOMZip-1.1" when :mets then "info:fedora/fedora-system:METSFedoraExt-1.1" when :foxml then "info:fedora/fedora-system:FOXML-1.1" else "info:fedora/fedora-system:FOXML-1.1" end end fetch_custom(object, "export", :format=>format, :context=>extra_params[:context].to_s) end |
#fetch_content(object_uri) ⇒ Object
Fetch the raw content of either a fedora object or datastream
61 62 63 64 |
# File 'lib/fedora/repository.rb', line 61 def fetch_content(object_uri) response = connection.raw_get("#{url_for(object_uri)}?format=xml") StringResponse.new(response.body, response.content_type) end |
#fetch_custom(object, method, extra_params = { :format => 'xml' }) ⇒ Object
Fetch the given object using custom method. This is used to fetch other aspects of a fedora object, such as profile, versions, etc…
Parameters
- object<String|Object>
-
a fedora uri, pid, FedoraObject instance
- method<Symbol>
-
the method to fetch such as :export, :history, :versions, etc
- extra_params<Hash>
-
any other extra parameters to pass to fedora
Returns
This method returns raw xml response from the server -
241 242 243 244 245 246 247 248 249 |
# File 'lib/fedora/repository.rb', line 241 def fetch_custom(object, method, extra_params = { :format => 'xml' }) path = case method when :profile then "" else "/#{method}" end extra_params.delete(:format) if method == :export connection.raw_get("#{url_for(object)}#{path}?#{extra_params.to_fedora_query}").body end |
#find_model(pid, klazz) ⇒ Object
Retrieve an object from fedora and load it as an instance of the given model/class
105 106 107 108 109 110 111 112 |
# File 'lib/fedora/repository.rb', line 105 def find_model(pid, klazz) obj = self.find_objects("pid=#{pid}").first if obj.nil? raise ActiveFedora::ObjectNotFoundError, "The repository does not have an object with pid #{pid}. The repository URL is #{self.base_url}" end doc = Nokogiri::XML::Document.parse(obj.object_xml) klazz.deserialize(doc) end |
#find_objects(*args) ⇒ Object
Find fedora objects with www.fedora.info/wiki/index.php/API-A-Lite_findObjects
Parameters
- query<String>
-
the query string to be sent to Fedora.
- options<Hash>
-
see below
Options<Hash> keys
- limit<String|Number>
-
set the maxResults parameter in fedora
- select<Symbol|Array>
-
the fields to returned. To include all fields, pass :all as the value.
The field "pid" is always included.
Examples
find_objects("label=Image1")
find_objects("pid~demo:*", "label=test")
find_objects("label=Image1", :include => :all)
find_objects("label=Image1", :include => [:label])
-
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/fedora/repository.rb', line 85 def find_objects(*args) raise ArgumentError, "Missing query string" unless args.length >= 1 = args.last.is_a?(Hash) ? args.pop : {} fields = [:select] fields = (fields.nil? || (fields == :all)) ? ALL_FIELDS : ([:pid] + ([fields].flatten! - [:pid])) query = args.join(' ') params = { :resultFormat => 'xml', :query => query } params[:maxResults] = [:limit] if [:limit] params[:sessionToken] = [:sessionToken] if [:sessionToken] includes = fields.inject("") { |s, f| s += "&#{f}=true"; s } convert_xml(connection.get("#{fedora_url.path}/objects?#{params.to_fedora_query}#{includes}")) end |
#ingest(content_to_ingest, extra_params = {}) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/fedora/repository.rb', line 217 def ingest(content_to_ingest, extra_params={}) if extra_params[:pid] url = url_for(extra_params[:pid]) else url = url_for("new") end if content_to_ingest.kind_of?(File) content_to_ingest = content_to_ingest.read end connection.post(url,content_to_ingest) end |
#nextid(attrs = {}) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/fedora/repository.rb', line 123 def nextid(attrs={}) request_url = fedora_url.path+"/management/getNextPID?xml=true" request_url += "&namespace=#{attrs[:namespace]}" if attrs[:namespace] d = REXML::Document.new(connection.post(request_url).body) d.elements['//pid'].text end |
#save(object) ⇒ Object
Create the given object if it’s new (not obtained from a find method). Otherwise update the object.
Return
- boolean
-
whether the operation is successful
-
119 120 121 |
# File 'lib/fedora/repository.rb', line 119 def save(object) object.new_object? ? create(object) : update(object) end |
#update(object) ⇒ Object
Update the given object
Return
- boolean
-
whether the operation is successful
-
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/fedora/repository.rb', line 165 def update(object) raise ArgumentError, "Missing pid attribute" if object.nil? || object.pid.nil? case object when Fedora::FedoraObject response = connection.put("#{url_for(object)}?" + object.attributes.to_fedora_query) response.code == '200' || '307' when Fedora::Datastream raise ArgumentError, "Missing dsID attribute" if object.dsid.nil? response = connection.put("#{url_for(object)}?" + object.attributes.to_fedora_query, object.blob) response.code == '200' || '201' return response.code else raise ArgumentError, "Unknown object type" end end |