Class: RDF::RedStore::Repository

Inherits:
SPARQL::Client::Repository
  • Object
show all
Defined in:
lib/rdf/redstore/repository.rb

Overview

An RDF::Repository backend for RedStore.

Instance Method Summary collapse

Constructor Details

#initialize(options_or_uri = {}) ⇒ RDF::RedStore::Repository

Create a new RDF::RedStore::Repository.

Examples:

RDF::RedStore::Repository.new('http://localhost:8080/')

Parameters:

  • uri (String)
  • opts (Hash)

25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rdf/redstore/repository.rb', line 25

def initialize(options_or_uri = {})
  case options_or_uri
    when Hash
      @settings = options_or_uri.dup
      @uri = @settings[:uri].to_s
    else
      @settings = {}
      @uri = options_or_uri.to_s
  end
  super(uri_for(:query), @settings)
end

Instance Method Details

#clear_statementsObject


76
77
78
79
80
81
# File 'lib/rdf/redstore/repository.rb', line 76

def clear_statements
  uri = uri_for(:data)
  Net::HTTP.start(uri.host, uri.port) do |http|
    http.delete(uri.path).code == '200'
  end
end

#countObject Also known as: size, length


38
39
40
41
42
43
44
45
46
47
# File 'lib/rdf/redstore/repository.rb', line 38

def count
  uri = uri_for('description?format=ntriples')
  description = RDF::Graph.load(uri, :format => :ntriples)
  total = description.first_value(:predicate => SD.totalTriples)
  if total.nil? or total.to_i < 0
    nil
  else
    total.to_i
  end
end

#delete_statement(statement, opts = {}) ⇒ Object


62
63
64
65
66
67
68
# File 'lib/rdf/redstore/repository.rb', line 62

def delete_statement(statement, opts = {})
  if statement.invalid?
    delete_statements(query(statement), opts)
  else
    delete_statements([statement], opts)
  end
end

#delete_statements(statements, opts = {}) ⇒ Object


71
72
73
# File 'lib/rdf/redstore/repository.rb', line 71

def delete_statements(statements, opts = {})
  post_statements(:delete, statements, opts)
end

#insert_statement(statement, opts = {}) ⇒ Object


52
53
54
# File 'lib/rdf/redstore/repository.rb', line 52

def insert_statement(statement, opts = {})
  insert_statements([statement], opts)
end

#insert_statements(statements, opts = {}) ⇒ Object


57
58
59
# File 'lib/rdf/redstore/repository.rb', line 57

def insert_statements(statements, opts = {})
  post_statements(:insert, statements, opts)
end

#post_statements(action, statements, opts = {}) ⇒ Object


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rdf/redstore/repository.rb', line 93

def post_statements(action, statements, opts = {})
  graph = RDF::Graph.new
  graph.insert_statements(statements)
  content = RDF::Writer.for(:ntriples).dump(graph)

  uri = uri_for(action)
  req = Net::HTTP::Post.new(uri.path)
  req.form_data = {
    'content' => content,
    'content-type' => 'ntriples',
    'graph' => opts[:context].to_s
  }

  Net::HTTP.start(uri.host, uri.port) do |http|
    response = http.request(req)
    response.code == '200'
  end
end

#uri_for(action) ⇒ Object


88
89
90
# File 'lib/rdf/redstore/repository.rb', line 88

def uri_for(action)
  RDF::URI.parse(@uri+action.to_s)
end

#writable?Boolean

Returns:

  • (Boolean)

83
84
85
# File 'lib/rdf/redstore/repository.rb', line 83

def writable?
  true
end