Class: Fedora::Repository

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fedora_url = "http://localhost:8080/fedora") ⇒ Repository

Returns a new instance of Repository.



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

def initialize(fedora_url = "http://localhost:8080/fedora") 
  @fedora_url = fedora_url.is_a?(URI) ? fedora_url : URI.parse(fedora_url)
  @connection = nil
end

Instance Attribute Details

#fedora_urlObject

Returns the value of attribute fedora_url.



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

def fedora_url
  @fedora_url
end

Instance Method Details

#create(object) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fedora/repository.rb', line 72

def create(object)
  case object
    when Fedora::FedoraObject
      pid = (object.pid ? object : 'new')
      response = connection.post("#{url_for(pid)}?" + object.attributes.to_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?
      response = connection.post("#{url_for(object)}?" + object.attributes.to_query, 
        object.blob)
      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)


127
128
129
130
131
# File 'lib/fedora/repository.rb', line 127

def delete(object)
  raise ArgumentError, "Object must not be nil" if object.nil?
  response = connection.delete("#{url_for(object)}")
  response.code == '200'
end

#fetch_conent(object_uri) ⇒ Object

Fetch the raw content of either a fedora object or datastream



26
27
28
# File 'lib/fedora/repository.rb', line 26

def fetch_conent(object_uri)
  connection.raw_get("#{url_for(object_uri)}?format=xml").body
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 -



143
144
145
146
147
148
149
150
151
# File 'lib/fedora/repository.rb', line 143

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}?#{to_query(extra_params)}").body
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)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fedora/repository.rb', line 47

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 = { :format => '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?#{to_query(params)}#{includes}"))
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

-



68
69
70
# File 'lib/fedora/repository.rb', line 68

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)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fedora/repository.rb', line 104

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_query)
    response.code == '307'
  when Fedora::Datastream
    raise ArgumentError, "Missing dsID attribute" if object.dsid.nil?
    response = connection.put("#{url_for(object)}?" + object.attributes.to_query, object.blob)
    response.code == '201'
  else
    raise ArgumentError, "Unknown object type"
  end
end