class RDF::Sesame::Server

A server endpoint compatible with the Sesame 2.0 HTTP protocol.

Instances of this class represent Sesame-compatible servers that contain one or more readable and/or writable RDF {Repository repositories}.

@example Connecting to a Sesame server

url    = URI.parse("http://localhost:8080/openrdf-sesame")
server = RDF::Sesame::Server.new(url)

@example Connecting to a Sesame server using Basic Auth & local proxy

server = RDF::Sesame::Server.new("http://localhost:8080/openrdf-sesame", {:user=> 'username', :pass => 'password',
    :proxy_host => 'localhost', :proxy_port => 8888})
repo = server.repositories['repositoryname']

@example Retrieving the server's protocol version

server.protocol                 #=> 4

@example Iterating over available RDF repositories

server.each_repository do |repository|
  puts repository.inspect
end

@example Finding all readable, non-empty RDF repositories

server.find_all do |repository|
  repository.readable? && !repository.empty?
end

@example Checking if any RDF repositories are writable

server.any? { |repository| repository.writable? }

@example Checking if a specific RDF repository exists on the server

server.has_repository?(:SYSTEM) #=> true
server.has_repository?(:foobar) #=> false

@example Obtaining a specific RDF repository

server.repository(:SYSTEM)      #=> RDF::Sesame::Repository(SYSTEM)
server[:SYSTEM]                 #=> RDF::Sesame::Repository(SYSTEM)

@see www.openrdf.org/doc/sesame2/system/ch08.html

Constants

ACCEPT_BINARY
ACCEPT_BINARY_TABLE
ACCEPT_BOOL
ACCEPT_JSON
ACCEPT_N3
ACCEPT_NTRIPLES
ACCEPT_RDF_JSON
ACCEPT_TRIG
ACCEPT_TRIX
ACCEPT_TURTLE
ACCEPT_XML
ACCEPT_XML_PURE
CONTENT_TYPE_SPARQL_QUERY
CONTENT_TYPE_SPARQL_UPDATE
CONTENT_TYPE_TEXT
CONTENT_TYPE_X_FORM
RESULT_BOOL
RESULT_JSON
RESULT_RDF_JSON
RESULT_XML

Attributes

connection[R]

@return [Connection]

Public Class Methods

new(url, options = {}, &block) click to toggle source

Initializes this `Server` instance.

@param [URI, to_s] url @param [Hash{Symbol => Object}] options @option options [Connection] :connection (nil) @yield [connection] @yieldparam [Server]

# File lib/rdf/sesame/server.rb, line 79
def initialize(url, options = {}, &block)
  @connection = options.delete(:connection) || Connection.new(url, options)

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end

Public Instance Methods

[](id)
Alias for: repository
delete(path, headers = {}) click to toggle source
# File lib/rdf/sesame/server.rb, line 219
def delete(path, headers = {})
  self.connection.open do
    process_response self.connection.delete(path, headers)
  end
end
each(&block)
Alias for: each_repository
each_repository(&block) click to toggle source

Enumerates over each repository on this Sesame server.

@yield [repository] @yieldparam [Repository] repository @return [Enumerator] @see repository @see repositories

# File lib/rdf/sesame/server.rb, line 155
def each_repository(&block)
  repositories.values.each(&block)
end
Also aliased as: each
get(path, headers = {}) click to toggle source
# File lib/rdf/sesame/server.rb, line 201
def get(path, headers = {})
  self.connection.open do
    process_response self.connection.get(path, headers)
  end
end
has_repository?(id) click to toggle source

Returns `true` if this server has a repository identified by `id`.

@param [String] id @return [Boolean]

# File lib/rdf/sesame/server.rb, line 166
def has_repository?(id)
  repositories.has_key?(id.to_s)
end
inspect() click to toggle source

Returns a developer-friendly representation of this instance.

@return [String]

# File lib/rdf/sesame/server.rb, line 127
def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, object_id, to_s)
end
post(path, data, headers = {}) click to toggle source
# File lib/rdf/sesame/server.rb, line 207
def post(path, data, headers = {})
  self.connection.open do
    process_response self.connection.post(path, data, headers)
  end
end
process_response(response, options = {}) click to toggle source

Executes a SPARQL query and returns the Net::HTTP::Response of the result.

@param [String, to_s] url @param [Hash{Symbol => Object}] options @option options [String] :content_type @return [String]

# File lib/rdf/sesame/server.rb, line 232
def process_response(response, options = {})
  @headers['Accept'] = options[:content_type] if options[:content_type]
  if response.is_a?(Net::HTTPSuccess)
    response
  else
    case response
    when Net::HTTPBadRequest # 400 Bad Request
      raise MalformedQuery.new(response.body)
    when Net::HTTPClientError # 4xx
      raise ClientError.new(response.body)
    when Net::HTTPServerError # 5xx
      raise ServerError.new(response.body)
    else
      raise ServerError.new(response.body)
    end
  end
end
protocol() click to toggle source

Returns the Sesame server's protocol version.

@example Retrieving the protocol version

server.protocol #=> 4

@return [Integer] @see www.openrdf.org/doc/sesame2/system/ch08.html#d0e180

# File lib/rdf/sesame/server.rb, line 139
def protocol
  response = get(:protocol)
  version = response.body
  version.to_i rescue 0
end
Also aliased as: protocol_version
protocol_version()
Alias for: protocol
put(path, data, headers = {}) click to toggle source
# File lib/rdf/sesame/server.rb, line 213
def put(path, data, headers = {})
  self.connection.open do
    process_response self.connection.put(path, data, headers)
  end
end
repositories() click to toggle source

Returns all repositories on this Sesame server.

@return [Hash{String => Repository}] @see repository @see each_repository @see www.openrdf.org/doc/sesame2/system/ch08.html#d0e204

# File lib/rdf/sesame/server.rb, line 190
def repositories
  require 'json' unless defined?(::JSON)
  response = get(:repositories, ACCEPT_JSON)

  json = ::JSON.parse(response.body)
  json['results']['bindings'].inject({}) do |repositories, binding|
    repository = parse_repository(binding)
    repositories.merge!(repository.id => repository)
  end
end
repository(id) click to toggle source

Returns a repository on this Sesame server.

@param [String] id @return [Repository] @see repositories @see each_repository

# File lib/rdf/sesame/server.rb, line 177
def repository(id)
  repositories[id.to_s]
end
Also aliased as: []
to_s() click to toggle source

Returns the URL of this server as a string.

@return [String]

# File lib/rdf/sesame/server.rb, line 119
def to_s
  url
end
to_uri() click to toggle source

Returns the URL of this server.

@return [URI]

# File lib/rdf/sesame/server.rb, line 111
def to_uri
  URI.parse(url)
end
uri(path = nil)
Alias for: url
url(path = nil) click to toggle source

Returns the URL for the given server-relative `path`.

@example Getting a Sesame server's URL

server.url            #=> "http://localhost:8080/openrdf-sesame"

@example Getting a Sesame server's protocol URL

server.url(:protocol) #=> "http://localhost:8080/openrdf-sesame/protocol"

@param [String, to_s] path @return [String]

# File lib/rdf/sesame/server.rb, line 101
def url(path = nil)
  self.connection.url(path)
end
Also aliased as: uri

Private Instance Methods

parse_repository(json) click to toggle source
# File lib/rdf/sesame/server.rb, line 252
def parse_repository(json)
  Repository.new(
    server:   self,
    uri:      RDF::URI.new(json['uri']['value']),
    id:       json['id']['value'],
    title:    json.has_key?('title') ? json['title']['value'] : nil,
    readable: json['readable']['value'].to_s == 'true',
    writable: json['writable']['value'].to_s == 'true'
  )
end