Class: Sesame::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sesame.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, sesamedir, repo, user, pass, options = {}) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sesame.rb', line 11

def initialize(host, port, sesamedir, repo, user, pass, options={})
  @host = host
  @port = port
  @sesamedir = sesamedir
  @repo = repo
  @user = user
  @pass = pass
  @opt = Hash.new(false).merge options
  @uri =
    URI.parse("http://#{@host}:#{@port}/#{@sesamedir}/repositories")
end

Instance Attribute Details

#repoObject (readonly)

Returns the value of attribute repo.



9
10
11
# File 'lib/sesame.rb', line 9

def repo
  @repo
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/sesame.rb', line 9

def uri
  @uri
end

Instance Method Details

#list_namespaces(repo) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sesame.rb', line 64

def list_namespaces repo
  process_query_xml "/#{repo}/namespaces" do |xml|
    res = Hash.new
    xml.xpath('//xmlns:result').each do |result|
      prefix = result.xpath('.//xmlns:binding[@name="prefix"]/xmlns:literal')[0].content
      namespc = result.xpath('.//xmlns:binding[@name="namespace"]/xmlns:literal')[0].content
      res[prefix] = namespc
    end
    res # res should be the result of the block
  end
end

#list_reposObject



54
55
56
57
58
59
60
61
62
# File 'lib/sesame.rb', line 54

def list_repos
  process_query_xml "" do |xml|
    res = Array.new
    xml.xpath('//xmlns:binding[@name="id"]/xmlns:literal').each do |repo|
      res << repo.content
    end
    res # res should be the result of the block
  end
end

#process_query_xml(uri, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sesame.rb', line 23

def process_query_xml(uri, &block)
  req = Net::HTTP::Get.new(@uri.path + uri, { 'Accept' => 'application/sparql-results+xml'})
  req.basic_auth @user, @pass
  xml = Nokogiri::XML.parse(request(req).body)
  if !@opt[:return_xml]
    res = yield xml
    return res
  else
    return xml
  end
end

#request(req) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/sesame.rb', line 76

def request(req)
  res = Net::HTTP.start(@host, @port) {|http|
    http.request(req)
  }
  if (not res.kind_of?(Net::HTTPSuccess))
    handle_error(req, res)
  end
  res
end

#select(query_str, query_lang = "sparql") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sesame.rb', line 35

def select(query_str, query_lang="sparql")
  params = { "query" => query_str, "queryLn" => query_lang }
  query_params =
    params.collect { |k,v| "#{k}=#{URI.escape(v.to_s)}" }.reverse.join '&'
  
  process_query_xml "/#{@repo}?#{query_params}" do |xml|
    res = Hash.new
    # Get variables from query
    xml.xpath('//xmlns:head/xmlns:variable').each do |var|
      res[var['name']] = Array.new
    end
    # Populate result hash by variable
    xml.xpath('//xmlns:result/xmlns:binding').each do |binding|
      res[binding['name']] << (binding / 'uri')[0].content
    end
    res # res should be the result of the block
  end
end