Class: Rubydora::Repository

Inherits:
Object
  • Object
show all
Includes:
ResourceIndex, RestApiClient
Defined in:
lib/rubydora/repository.rb

Overview

Fedora Repository object that provides API access

Constant Summary

Constants included from RestApiClient

Rubydora::RestApiClient::VALID_CLIENT_OPTIONS

Constants included from FedoraUrlHelpers

FedoraUrlHelpers::API_DOCUMENTATION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RestApiClient

#add_datastream, #add_relationship, #client, #datastream, #datastream_dissemination, #datastream_versions, #dissemination, #export, #find_objects, #ingest, #modify_datastream, #modify_object, #next_pid, #object, #object_versions, #object_xml, #purge_datastream, #purge_object, #purge_relationship, #relationships, #safe_subresource, #set_datastream_options

Methods included from FedoraUrlHelpers

#datastream_content_url, #datastream_history_url, #datastream_url, #dissemination_url, #export_object_url, #find_objects_url, #new_object_relationship_url, #next_pid_url, #object_relationship_url, #object_url, #object_versions_url, #object_xml_url, #url_for, #validate_object_url

Methods included from ResourceIndex

#find_by_sparql, #find_by_sparql_relationship, #risearch, #sparql

Constructor Details

#initialize(options = {}) ⇒ Repository

Returns a new instance of Repository.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String)
  • :user (String)
  • :password (String)
  • :validateChecksum (Boolean)


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

def initialize options = {}
  @config = options.symbolize_keys
  check_repository_version!
end

Instance Attribute Details

#configObject (readonly)

repository configuration (see #initialize)



10
11
12
# File 'lib/rubydora/repository.rb', line 10

def config
  @config
end

Instance Method Details

#create(pid, options = {}) ⇒ Object

create a new fedora object (see also DigitalObject#save)



53
54
55
# File 'lib/rubydora/repository.rb', line 53

def create pid, options = {}
  DigitalObject.create(pid, options = {}, self)
end

#find(pid) ⇒ Object

find an existing fedora object TODO: raise an error if the object does not yet exist



23
24
25
# File 'lib/rubydora/repository.rb', line 23

def find pid
  DigitalObject.find(pid, self)
end

#pingObject

Raise an error if unable to connect to the API endpoint



92
93
94
95
# File 'lib/rubydora/repository.rb', line 92

def ping
  raise "Unable to establish connection to Fedora Repository" unless profile
  true
end

#profileHash

repository profile (from API-A-LITE data)

Returns:

  • (Hash)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubydora/repository.rb', line 59

def profile
  @profile ||= begin
    profile_xml = client['describe?xml=true'].get
    profile_xml.gsub! '<fedoraRepository', '<fedoraRepository xmlns="http://www.fedora.info/definitions/1/0/access/"' unless profile_xml =~ /xmlns=/
    doc = Nokogiri::XML(profile_xml)
    xmlns = { 'access' => "http://www.fedora.info/definitions/1/0/access/"  }
    h = doc.xpath('/access:fedoraRepository/*', xmlns).inject({}) do |sum, node|
                 sum[node.name] ||= []
                 case node.name
                   when "repositoryPID"
                     sum[node.name] << Hash[*node.xpath('access:*', xmlns).map { |x| [node.name, node.text]}.flatten]
                   else
                     sum[node.name] << node.text
                 end
                 sum
               end
    h.select { |key, value| value.length == 1 }.each do |key, value|
      next if key == "objModels"
      h[key] = value.first
    end

    h
  rescue
    nil
  end
end

#search(query, options = {}) {|DigitalObject| ... } ⇒ Object

High-level access to the Fedora find_objects API

Yields:

  • (DigitalObject)

    Yield a DigitalObject for each search result



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubydora/repository.rb', line 32

def search query, options = {}, &block
  return to_enum(:search, query, options).to_a unless block_given?
  
  sessionToken = nil 
  doc = nil

  begin 
    sessionOptions = {}
    sessionOptions[:sessionToken] = sessionToken unless sessionToken.nil? or sessionToken.blank?

    response = self.find_objects(options.merge(:query => query, :resultFormat => 'xml', :pid => true).merge(sessionOptions))

    doc = Nokogiri::XML(response)
    doc.xpath('//xmlns:objectFields/xmlns:pid', doc.namespaces).each { |pid| obj = self.find(pid.text); block.call(obj) }

    sessionToken = doc.xpath('//xmlns:listSession/xmlns:token', doc.namespaces).text
  end until sessionToken.nil? or sessionToken.empty? or doc.xpath('//xmlns:resultList/xmlns:objectFields', doc.namespaces).empty?

end

#versionFloat

Returns repository version.

Returns:

  • (Float)

    repository version



87
88
89
# File 'lib/rubydora/repository.rb', line 87

def version
  @version ||= profile['repositoryVersion'].to_f rescue nil
end