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: {})


42
43
44
45
46
# File 'lib/rubydora/resource_index.rb', line 42

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) ⇒ CSV::Table

Run a raw SPARQL query and return a FasterCSV object

Parameters:

  • query (String)

    SPARQL query

Returns:

  • (CSV::Table)


35
36
37
# File 'lib/rubydora/resource_index.rb', line 35

def sparql(query)
  CSV.parse(self.risearch(query), :headers => true)
end