Sha256: 3a83961c8d62e453948f6a917cb9c4cc52c757cb297557c0c43e35158cb7f568

Contents?: true

Size: 1.7 KB

Versions: 49

Compression:

Stored size: 1.7 KB

Contents

require 'enumerator'

module Rubydora
  # Fedora resource index helpers
  module ResourceIndex
    # Find new objects using a sparql query
    # @param [String] query SPARQL query
    # @param [Hash] options 
    # @option options [String] :binding the SPARQL binding name to create new objects from
    # @return [Array<Rubydora::DigitalObject>]
    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 new objects by their relationship to a subject
    # @param [String] subject Subject URI 
    # @param [String] predicate Predicate URI
    # @return [Array<Rubydora::DigitalObject>]
    def find_by_sparql_relationship subject, predicate
      find_by_sparql <<-RELSEXT
        SELECT ?pid FROM <#ri> WHERE {
          <#{subject}> <#{predicate}> ?pid 
        }
      RELSEXT
    end

    # Run a raw SPARQL query and return a FasterCSV object
    # @param [String] query SPARQL query
    # @return [FasterCSV::Table]
    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

    # Run a raw query against the Fedora risearch resource index
    # @param [String] query
    # @param [Hash] options
    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

  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
rubydora-1.8.1 lib/rubydora/resource_index.rb
rubydora-1.8.0 lib/rubydora/resource_index.rb
rubydora-1.7.5 lib/rubydora/resource_index.rb
rubydora-1.7.4 lib/rubydora/resource_index.rb
rubydora-1.7.3 lib/rubydora/resource_index.rb
rubydora-1.7.1 lib/rubydora/resource_index.rb
rubydora-1.7.0 lib/rubydora/resource_index.rb
rubydora-1.7.0.pre1 lib/rubydora/resource_index.rb
rubydora-1.6.5 lib/rubydora/resource_index.rb
rubydora-1.6.4 lib/rubydora/resource_index.rb
rubydora-1.6.3 lib/rubydora/resource_index.rb
rubydora-1.6.2 lib/rubydora/resource_index.rb
rubydora-1.6.1 lib/rubydora/resource_index.rb
rubydora-1.6.0 lib/rubydora/resource_index.rb
rubydora-1.5.1 lib/rubydora/resource_index.rb
rubydora-1.5.0 lib/rubydora/resource_index.rb
rubydora-1.4.2 lib/rubydora/resource_index.rb
rubydora-1.4.1 lib/rubydora/resource_index.rb
rubydora-1.4.0 lib/rubydora/resource_index.rb
rubydora-1.3.0 lib/rubydora/resource_index.rb