Module: Rubydora::ResourceIndex

Included in:
Repository
Defined in:
lib/rubydora/resource_index.rb

Overview

Fedora resource index helpers

Instance Method Summary collapse

Instance Method Details

#find_by_sparql(query, options = { :binding => 'pid' }) ⇒ Array<Rubydora::DigitalObject>

Find new objects using a sparql query

Parameters:

  • query (String)

    SPARQL query

  • options (Hash) (defaults to: { :binding => 'pid' })

Options Hash (options):

  • :binding (String)

    the SPARQL binding name to create new objects from

Returns:



11
12
13
14
15
16
17
18
# File 'lib/rubydora/resource_index.rb', line 11

def find_by_sparql query, options = { :binding => 'pid' }
  return to_enum(:find_by_sparql, query, options).to_a unless block_given?

  self.sparql(query).each do |x| 
    obj = self.find(x[options[:binding]]) rescue nil
    yield obj if obj
  end
end

#find_by_sparql_relationship(subject, predicate) ⇒ Array<Rubydora::DigitalObject>

Find new objects by their relationship to a subject

Parameters:

  • subject (String)

    Subject URI

  • predicate (String)

    Predicate URI

Returns:



24
25
26
27
28
29
30
# File 'lib/rubydora/resource_index.rb', line 24

def find_by_sparql_relationship subject, predicate
  find_by_sparql <<-RELSEXT
    SELECT ?pid FROM <#ri> WHERE {
      <#{subject}> <#{predicate}> ?pid 
    }
  RELSEXT
end

#risearch(query, options = {}) ⇒ Object

Run a raw query against the Fedora risearch resource index

Parameters:

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


46
47
48
49
50
# File 'lib/rubydora/resource_index.rb', line 46

def risearch query, options = {}
  request_params = { :dt => 'on', :format => 'CSV', :lang => 'sparql', :limit => nil, :query => query, :type => 'tuples' }.merge(options)

  self.client['risearch'].post request_params
end

#sparql(query) ⇒ FasterCSV::Table

Run a raw SPARQL query and return a FasterCSV object

Parameters:

  • query (String)

    SPARQL query

Returns:

  • (FasterCSV::Table)


35
36
37
38
39
40
41
# File 'lib/rubydora/resource_index.rb', line 35

def sparql query
  if CSV.const_defined? :Reader
    FasterCSV.parse(self.risearch(query), :headers => true)
  else
    CSV.parse(self.risearch(query), :headers => true)
  end
end