Class: Fedora::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/fedora/repository.rb

Defined Under Namespace

Classes: StringResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fedora_url, surrogate = nil) ⇒ Repository

Returns a new instance of Repository.



53
54
55
56
57
# File 'lib/fedora/repository.rb', line 53

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_urlObject

Returns the value of attribute base_url.



18
19
20
# File 'lib/fedora/repository.rb', line 18

def base_url
  @base_url
end

#fedora_urlObject

Returns the value of attribute fedora_url.



51
52
53
# File 'lib/fedora/repository.rb', line 51

def fedora_url
  @fedora_url
end

#fedora_versionObject

Returns the value of attribute fedora_version.



18
19
20
# File 'lib/fedora/repository.rb', line 18

def fedora_version
  @fedora_version
end

#pid_delimiterObject

Returns the value of attribute pid_delimiter.



18
19
20
# File 'lib/fedora/repository.rb', line 18

def pid_delimiter
  @pid_delimiter
end

#pid_namespaceObject

Returns the value of attribute pid_namespace.



18
19
20
# File 'lib/fedora/repository.rb', line 18

def pid_namespace
  @pid_namespace
end

#repository_nameObject

Returns the value of attribute repository_name.



18
19
20
# File 'lib/fedora/repository.rb', line 18

def repository_name
  @repository_name
end

Class Method Details

.flushObject



20
21
22
# File 'lib/fedora/repository.rb', line 20

def self.flush
  Thread.current[:repo]=nil
end

.instanceObject



38
39
40
41
# File 'lib/fedora/repository.rb', line 38

def self.instance
  raise "did you register a repo?" unless Thread.current[:repo]
  Thread.current[:repo]
end

.register(url, surrogate = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fedora/repository.rb', line 23

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fedora/repository.rb', line 120

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

-

Raises:

  • (ArgumentError)


178
179
180
181
182
# File 'lib/fedora/repository.rb', line 178

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_repositoryObject



204
205
206
207
# File 'lib/fedora/repository.rb', line 204

def describe_repository
  result_body = connection.raw_get("#{fedora_url.path}/describe?xml=true").body
  XmlSimple.xml_in(result_body)
end

#fetch_content(object_uri) ⇒ Object

Fetch the raw content of either a fedora object or datastream



60
61
62
63
# File 'lib/fedora/repository.rb', line 60

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 -



194
195
196
197
198
199
200
201
202
# File 'lib/fedora/repository.rb', line 194

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



99
100
101
102
103
# File 'lib/fedora/repository.rb', line 99

def find_model(pid, klazz)
  obj = self.find_objects("pid=#{pid}").first
  doc = REXML::Document.new(obj.object_xml, :ignore_whitespace_nodes=>:all)
  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])

-

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fedora/repository.rb', line 84

def find_objects(*args)
  raise ArgumentError, "Missing query string" unless args.length >= 1
  options = args.last.is_a?(Hash) ? args.pop : {}
  
  fields = options[:select]
  fields = (fields.nil? || (fields == :all)) ? ALL_FIELDS : ([:pid] + ([fields].flatten! - [:pid]))
  
  query = args.join(' ')
  params = { :resultFormat => 'xml', :query => query }
  params[:maxResults] = options[:limit] if options[:limit]
  params[:sessionToken] = options[:sessionToken] if options[:sessionToken]
  includes = fields.inject("") { |s, f| s += "&#{f}=true"; s }
  
  convert_xml(connection.get("#{fedora_url.path}/objects?#{params.to_fedora_query}#{includes}"))
end

#nextidObject



114
115
116
117
# File 'lib/fedora/repository.rb', line 114

def nextid
  d = REXML::Document.new(connection.post(fedora_url.path+"/management/getNextPID?xml=true").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

-



110
111
112
# File 'lib/fedora/repository.rb', line 110

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

-

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fedora/repository.rb', line 154

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