Sha256: 69c7a2d75faf434c430230418ed456108c6c84781d44d2fbcd64b6dbb9a44339

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'rdf'
require 'rdf/rdfxml'
require 'sparql/client'
require 'enumerator'

module RDF
  class Marmotta < ::RDF::Repository

    attr_accessor :endpoints

    DEFAULT_OPTIONS = {
      :sparql => 'sparql/select',
      :sparql_update => 'sparql/update',
      :ldpath => 'ldpath'
    }

    def initialize(base_url, options = {})
      @endpoints = DEFAULT_OPTIONS
      @endpoints.merge!(options)
      @endpoints.each do |k, v|
        @endpoints[k] = URI(base_url.to_s + v.to_s) unless v.is_a? ::URI
      end
    end

    def query_client
      @query_client ||= Client.new(endpoints[:sparql])
    end

    def update_client
      @update_client ||= Client.new(endpoints[:sparql_update])
    end

    # @see RDF::Enumerable#each.
    def each(&block)
      query_client.construct([:s, :p, :o]).where([:s, :p, :o]).each_statement(&block)
    end

    # @see RDF::Mutable#insert_statement
    def insert_statement(statement)
      insert_statements([statement])
    end

    def insert_statements(statements)
      update_client.insert_data(statements)
    end

    # @see RDF::Mutable#delete_statement
    def delete_statement(statement)
      delete_statements([statement])
    end

    def delete_statements(statements)
      constant = statements.all? do |value|
        !value.respond_to?(:each_statement) && begin
          statement = RDF::Statement.from(value)
          statement.constant? && !statement.has_blank_nodes?
        end
      end

      if constant
        update_client.delete_data(statements)
      else
        update_client.delete_insert(statements)
      end
    end

    def clear
      update_client.query("DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }")
    end

    class Client < SPARQL::Client
      def initialize(url, options = {}, &block)
        options[:method] ||= :get
        options[:protocol] ||= '1.1'
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rdf-marmotta-0.0.4 lib/rdf/marmotta.rb