kasabi 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ module Kasabi
2
+
3
+ class Status < BaseClient
4
+ #Initialize the store client to work with a specific endpoint
5
+ #
6
+ # The _options_ hash can contain the following values:
7
+ # * *:apikey*: required. apikey authorized to use the API
8
+ # * *:client*: HTTPClient object instance
9
+ def initialize(endpoint, options={})
10
+ super(endpoint, options)
11
+ end
12
+
13
+ def get_status()
14
+ response = get(@endpoint)
15
+ validate_response(response)
16
+ return JSON.parse( response.content )
17
+ end
18
+
19
+ def status
20
+ return get_status()["status"]
21
+ end
22
+
23
+ def storage_mode
24
+ return get_status()["storageMode"]
25
+ end
26
+
27
+ def published?
28
+ return status() == "published"
29
+ end
30
+
31
+ def writeable?
32
+ return storage_mode() == "read-write"
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,63 @@
1
+ module Kasabi
2
+
3
+ module Storage
4
+
5
+ class Client < Kasabi::BaseClient
6
+
7
+ #Initialize the store client to work with a specific endpoint
8
+ #
9
+ # The _options_ hash can contain the following values:
10
+ # * *:apikey*: required. apikey authorized to use the API
11
+ # * *:client*: HTTPClient object instance
12
+ def initialize(endpoint, options={})
13
+ super(endpoint, options)
14
+ end
15
+
16
+ # Store the contents of a File (or any IO stream) in the store associated with this dataset
17
+ # The client does not support streaming submissions of data, so the stream will be fully read before data is submitted to the platform
18
+ # file:: an IO object
19
+ # content_type:: mimetype of RDF serialization
20
+ def store_file(file, content_type="application/rdf+xml")
21
+ data = file.read()
22
+ file.close()
23
+ return store_data(data, content_type)
24
+ end
25
+
26
+ #Store triples contained in the provided string
27
+ def store_data(data, content_type="application/rdf+xml")
28
+ response = post(endpoint, data, {"Content-Type" => content_type } )
29
+ if response.status != 202
30
+ raise "Unable to perform request. Status: #{response.status}. Message: #{response.content}"
31
+ end
32
+ return response.content
33
+ end
34
+
35
+ def store_uri(uri, content_type="application/rdf+xml")
36
+ response = post(endpoint, {"data_uri" => uri }, {"Content-Type" => content_type } )
37
+ if response.status != 202
38
+ raise "Unable to perform request. Status: #{response.status}. Message: #{response.content}"
39
+ end
40
+ return response.content
41
+ end
42
+
43
+ def apply_changeset(cs)
44
+ response = post(endpoint, cs, {"Content-Type" => "application/vnd.talis.changeset+xml"} )
45
+ if response.status != 202
46
+ raise "Unable to apply changeset. Status: #{response.status}. Message: #{response.content}"
47
+ end
48
+ return response.content
49
+ end
50
+
51
+ def applied?(update_uri)
52
+ response = get( update_uri, nil, {"Content-Type" => "application/json"} )
53
+ if response.status != 200
54
+ raise "Unable to determine update status. Status: #{response.status}. Message: #{response.content}"
55
+ end
56
+ json = JSON.parse(response.content)
57
+ return json["status"] && json["status"] == "applied"
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,90 @@
1
+ module Kasabi
2
+
3
+ class Dataset < Kasabi::BaseClient
4
+
5
+ attr_reader :short_code
6
+ attr_reader :uri
7
+
8
+ #Initialize the client to work with a specific dataset endpoint
9
+ #Dataset endpoints are available from api.kasabi.com/data/...
10
+ #
11
+ # The _options_ hash can contain the following values:
12
+ # * *:apikey*: required. apikey authorized to use the API
13
+ # * *:client*: HTTPClient object instance
14
+ def initialize(endpoint, options={})
15
+ super(endpoint, options)
16
+ uri = URI.parse(endpoint)
17
+ domain = uri.host.split(".")[0]
18
+ case domain
19
+ when "data"
20
+ @uri = endpoint
21
+ @endpoint = endpoint.gsub("http://data", "http://api")
22
+ when "api"
23
+ @endpoint = endpoint
24
+ @uri = endpoint.gsub("http://api", "http://data")
25
+ else
26
+ #probably website, e.g. beta.kasabi or www.kasabi
27
+ @endpoint = "http://api.kasabi.com" + uri.path
28
+ @uri = "http://data.kasabi.com" + uri.path
29
+ end
30
+ end
31
+
32
+ #Read the metadata about this dataset from the live service
33
+ def metadata(allow_cached=true)
34
+ if @metadata
35
+ return @metadata
36
+ end
37
+ response = get(@uri, nil,
38
+ {"Accept" => "application/json"})
39
+ validate_response(response)
40
+ @metadata = JSON.parse( response.content )
41
+ return @metadata
42
+ end
43
+
44
+ PROPERTIES = {
45
+ :title => "http://purl.org/dc/terms/title",
46
+ :description => "http://purl.org/dc/terms/description",
47
+ :homepage => "http://xmlns.com/foaf/0.1/homepage",
48
+ :sparql_endpoint => "http://rdfs.org/ns/void#sparqlEndpoint",
49
+ :lookup_api => "http://rdfs.org/ns/void#uriLookupEndpoint",
50
+ :search_api => "http://labs.kasabi.com/ns/services#searchEndpoint",
51
+ :augmentation_api => "http://labs.kasabi.com/ns/services#augmentationEndpoint",
52
+ :reconciliation_api => "http://labs.kasabi.com/ns/services#reconciliationEndpoint",
53
+ :store_api => "http://labs.kasabi.com/ns/services#storeEndpoint",
54
+ :status_api => "http://labs.kasabi.com/ns/services#statusEndpoint",
55
+ :jobs_api => "http://labs.kasabi.com/ns/services#jobsEndpoint",
56
+ :attribution_api => "http://labs.kasabi.com/ns/services#attributionEndpoint"
57
+ }
58
+
59
+ PROPERTIES.keys.each do |arg|
60
+ send :define_method, arg do
61
+ return property(PROPERTIES[arg])
62
+ end
63
+ end
64
+
65
+ STANDARD_API_CLIENTS = {
66
+ :sparql_endpoint => Kasabi::Sparql::Client,
67
+ :lookup_api => Kasabi::Lookup::Client,
68
+ :search_api => Kasabi::Search::Client,
69
+ :augmentation_api => Kasabi::Augment::Client,
70
+ :reconciliation_api => Kasabi::Reconcile::Client,
71
+ :store_api => Kasabi::Storage::Client,
72
+ :status_api => Kasabi::Status,
73
+ :jobs_api => Kasabi::Jobs::Client,
74
+ :attribution_api => Kasabi::Attribution
75
+ }
76
+
77
+ STANDARD_API_CLIENTS.keys.each do |arg|
78
+ send :define_method, "#{arg}_client" do
79
+ return STANDARD_API_CLIENTS[arg].method("new").call( self.method(arg).call(), self.client_options )
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def property(predicate)
86
+ metadata()
87
+ return @metadata[ @uri ][predicate][0]["value"]
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,74 @@
1
+ {
2
+ "http://data.kasabi.com/dataset/nasa/spacecraft/1968-025A": {
3
+ "http://purl.org/net/schemas/space/discipline": [
4
+ {
5
+ "type": "uri",
6
+ "value": "http://data.kasabi.com/dataset/nasa/discipline/engineering"
7
+ }
8
+ ],
9
+ "http://xmlns.com/foaf/0.1/name": [
10
+ {
11
+ "type": "literal",
12
+ "value": "Apollo 6"
13
+ }
14
+ ],
15
+ "http://purl.org/net/schemas/space/internationalDesignator": [
16
+ {
17
+ "type": "literal",
18
+ "value": "1968-025A"
19
+ }
20
+ ],
21
+ "http://purl.org/net/schemas/space/alternateName": [
22
+ {
23
+ "type": "literal",
24
+ "value": "03170"
25
+ },
26
+ {
27
+ "type": "literal",
28
+ "value": "AS-502"
29
+ }
30
+ ],
31
+ "http://purl.org/dc/elements/1.1/description": [
32
+ {
33
+ "type": "literal",
34
+ "value": "The unmanned Saturn/Apollo 6 mission was designed as the final qualification of the Saturn V launch vehicle and Apollo spacecraft for manned Apollo missions. The spacecraft consisted of the three stage Saturn V, the Apollo Command and Service Module (CSM) and a boilerplate Lunar Module (LM). The primary objectives of the mission were to demonstrate structural and thermal integrity and compatibility of the launch vehicle and spacecraft, confirm launch loads and dynamic characteristics, and verify stage separations, propulsion, guidance and control, electrical systems, emergency detection system, and mission support facilities and operations, including Command Module recovery."
35
+ }
36
+ ],
37
+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": [
38
+ {
39
+ "type": "uri",
40
+ "value": "http://purl.org/net/schemas/space/Spacecraft"
41
+ }
42
+ ],
43
+ "http://xmlns.com/foaf/0.1/homepage": [
44
+ {
45
+ "type": "uri",
46
+ "value": "http://nssdc.gsfc.nasa.gov/database/MasterCatalog?sc=1968-025A"
47
+ }
48
+ ],
49
+ "http://purl.org/net/schemas/space/agency": [
50
+ {
51
+ "type": "literal",
52
+ "value": "United States"
53
+ }
54
+ ],
55
+ "http://xmlns.com/foaf/0.1/depiction": [
56
+ {
57
+ "type": "uri",
58
+ "value": "http://nssdc.gsfc.nasa.gov/image/spacecraft/apollo6_launch.jpg"
59
+ }
60
+ ],
61
+ "http://purl.org/net/schemas/space/mass": [
62
+ {
63
+ "type": "literal",
64
+ "value": "36806.0"
65
+ }
66
+ ],
67
+ "http://purl.org/net/schemas/space/launch": [
68
+ {
69
+ "type": "uri",
70
+ "value": "http://nasa.dataincubator.org/launch/1968-025"
71
+ }
72
+ ]
73
+ }
74
+ }
@@ -0,0 +1,31 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'kasabi'
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class AttributionTest < Test::Unit::TestCase
7
+
8
+ ATTRIBUTION = <<-EOL
9
+ {
10
+ "name": "name",
11
+ "homepage": "homepage",
12
+ "source": "source",
13
+ "logo": "logo"
14
+ }
15
+ EOL
16
+
17
+ def test_get
18
+ mc = mock()
19
+ mc.expects(:get).with("http://api.kasabi.com/api/test-data/attribution",
20
+ nil, {"X_KASABI_APIKEY" => "test-key"} ).returns(
21
+ HTTP::Message.new_response( ATTRIBUTION ))
22
+
23
+ client = Kasabi::Attribution.new("http://api.kasabi.com/api/test-data/attribution",
24
+ :apikey=>"test-key", :client=>mc)
25
+
26
+ attribution = client.get_attribution
27
+ assert_not_nil(attribution)
28
+
29
+ end
30
+
31
+ end
@@ -7,7 +7,8 @@ class AugmentTest < Test::Unit::TestCase
7
7
 
8
8
  def test_augment_uri
9
9
  mc = mock()
10
- mc.expects(:get).with("http://api.kasabi.com/api/test-augment-api", {:apikey=>"test-key", "data-uri" => "http://www.example.org/index.rss"}).returns(
10
+ mc.expects(:get).with("http://api.kasabi.com/api/test-augment-api", {"data-uri" => "http://www.example.org/index.rss"},
11
+ {"X_KASABI_APIKEY" => "test-key"}).returns(
11
12
  HTTP::Message.new_response("OK") )
12
13
 
13
14
  client = Kasabi::Augment::Client.new( "http://api.kasabi.com/api/test-augment-api", :apikey=>"test-key", :client=>mc)
@@ -17,11 +18,12 @@ class AugmentTest < Test::Unit::TestCase
17
18
 
18
19
  def test_augment
19
20
  mc = mock()
20
- mc.expects(:post).with("http://api.kasabi.com/api/test-augment-api?apikey=test-key", "data", anything).returns(
21
+ mc.expects(:post).with("http://api.kasabi.com/api/test-augment-api", "data",
22
+ {"Content-Type" => "application/rss+xml", "X_KASABI_APIKEY" => "test-key"}).returns(
21
23
  HTTP::Message.new_response("OK") )
22
24
 
23
- client = Kasabi::Augment::Client.new( "http://api.kasabi.com/api/test-augment-api", :apikey=>"test-key", :client=>mc)
24
- response = client.augment("data")
25
+ client = Kasabi::Augment::Client.new( "http://api.kasabi.com/api/test-augment-api", {:apikey=>"test-key", :client=>mc})
26
+ response = client.augment("data")
25
27
  assert_equal("OK", response)
26
28
  end
27
29
 
@@ -67,7 +67,7 @@ EOL
67
67
  def test_query
68
68
  mc = mock()
69
69
  mc.expects(:get).with("http://api.kasabi.com/api/test-facet-api/facet",
70
- {:query => "austen", :fields => "title,author,publisher", :apikey => "test-key"}, nil).returns(
70
+ {:query => "austen", :fields => "title,author,publisher"}, {"X_KASABI_APIKEY" => "test-key"}).returns(
71
71
  HTTP::Message.new_response(RESULTS) )
72
72
 
73
73
  client = Kasabi::Search::Client.new("http://api.kasabi.com/api/test-facet-api", :apikey => "test-key", :client=>mc)
@@ -0,0 +1,23 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'kasabi'
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class JobsTest < Test::Unit::TestCase
7
+
8
+ def test_reset
9
+ resp = HTTP::Message.new_response("http://api.kasabi.com/dataset/test-data/jobs/abc")
10
+ resp.status = 202
11
+ mc = mock()
12
+ mc.expects(:post).with("http://api.kasabi.com/dataset/test-data/jobs",
13
+ {:jobType=>"reset", :startTime=>"2011-06-05-15:35:30Z"},
14
+ {"Content-Type" => "application/x-www-form-urlencoded", "X_KASABI_APIKEY" => "test"} ).returns(
15
+ resp )
16
+
17
+ dataset = Kasabi::Jobs::Client.new("http://api.kasabi.com/dataset/test-data/jobs",
18
+ { :apikey => "test", :client => mc })
19
+ id = dataset.submit_job("reset", "2011-06-05-15:35:30Z")
20
+ assert_equal("http://api.kasabi.com/dataset/test-data/jobs/abc", id)
21
+ end
22
+
23
+ end
@@ -5,17 +5,22 @@ require 'mocha'
5
5
 
6
6
  class LookupTest < Test::Unit::TestCase
7
7
 
8
+ def setup
9
+ @json = File.read(File.join(File.dirname(__FILE__), "apollo-6.json"))
10
+ end
11
+
8
12
  def test_describe_with_json
9
13
  mc = mock()
10
14
  mc.expects(:get).with("http://api.kasabi.com/api/test-lookup",
11
- {:apikey=>"test-key", :about => "http://www.example.org", :output=>"json"} ).returns(
12
- HTTP::Message.new_response("{}"))
15
+ {:about => "http://www.example.org", :output=>"json"}, {"X_KASABI_APIKEY" => "test-key"} ).returns(
16
+ HTTP::Message.new_response( @json ))
13
17
 
14
18
  client = Kasabi::Lookup::Client.new("http://api.kasabi.com/api/test-lookup",
15
19
  :apikey=>"test-key", :client=>mc)
16
20
 
17
- response = client.lookup("http://www.example.org")
18
- assert_not_nil( response )
21
+ graph = client.lookup("http://www.example.org")
22
+ assert_not_nil( graph )
23
+ assert_equal(12, graph.count )
19
24
  end
20
25
 
21
26
  end
@@ -87,7 +87,7 @@ class ClientTest < Test::Unit::TestCase
87
87
 
88
88
  mc = mock();
89
89
  mc.expects(:get).with("http://api.kasabi.com/api/test-reconcile",
90
- {:apikey=>"test-key", "query" => {:query => "test", :limit => 3, :type_strict => :any}.to_json }
90
+ {"query" => {:query => "test", :limit => 3, :type_strict => :any}.to_json }, {"X_KASABI_APIKEY" => "test-key"}
91
91
  ).returns( HTTP::Message.new_response("{}") )
92
92
 
93
93
  reconciler = Kasabi::Reconcile::Client.new("http://api.kasabi.com/api/test-reconcile", :apikey=>"test-key", :client=>mc)
@@ -108,7 +108,7 @@ class ClientTest < Test::Unit::TestCase
108
108
 
109
109
  mc = mock();
110
110
  mc.expects(:get).with("http://api.kasabi.com/api/test-reconcile",
111
- {:apikey=>"test-key", "query" => {:query => "test", :limit => 3, :type_strict => :any}.to_json }
111
+ {"query" => {:query => "test", :limit => 3, :type_strict => :any}.to_json }, {"X_KASABI_APIKEY" => "test-key"}
112
112
  ).returns( HTTP::Message.new_response( resp.to_json ) )
113
113
 
114
114
  reconciler = Kasabi::Reconcile::Client.new("http://api.kasabi.com/api/test-reconcile", :apikey=>"test-key", :client=>mc)
@@ -131,7 +131,8 @@ class ClientTest < Test::Unit::TestCase
131
131
 
132
132
  mc = mock();
133
133
  mc.expects(:get).with("http://api.kasabi.com/api/test-reconcile",
134
- {:apikey=>"test-key", "query" => {:query => "test", :limit => 3, :type_strict => :any}.to_json }
134
+ {"query" => {:query => "test", :limit => 3, :type_strict => :any}.to_json },
135
+ {"X_KASABI_APIKEY" => "test-key"}
135
136
  ).returns( HTTP::Message.new_response( resp.to_json ) )
136
137
 
137
138
  reconciler = Kasabi::Reconcile::Client.new("http://api.kasabi.com/api/test-reconcile", :apikey=>"test-key", :client=>mc)
@@ -161,7 +162,7 @@ class ClientTest < Test::Unit::TestCase
161
162
  mc = mock();
162
163
  expected_query = { "q0" => { :query => "test", :limit => 3, :type_strict => :any} }
163
164
  mc.expects(:get).with("http://api.kasabi.com/api/test-reconcile",
164
- {:apikey=>"test-key", "queries" => expected_query.to_json }
165
+ {"queries" => expected_query.to_json }, {"X_KASABI_APIKEY" => "test-key"}
165
166
  ).returns( HTTP::Message.new_response( resp.to_json ) )
166
167
 
167
168
  reconciler = Kasabi::Reconcile::Client.new("http://api.kasabi.com/api/test-reconcile", :apikey=>"test-key", :client=>mc)
@@ -7,7 +7,7 @@ class SearchTest < Test::Unit::TestCase
7
7
 
8
8
  def test_simple_search
9
9
  mc = mock()
10
- mc.expects(:get).with("http://api.kasabi.com/api/test-search/search", {:apikey=>"test-key", :query => "lunar", :output=>"json"}).returns(
10
+ mc.expects(:get).with("http://api.kasabi.com/api/test-search/search", {:query => "lunar", :output=>"json"}, {"X_KASABI_APIKEY" => "test-key"}).returns(
11
11
  HTTP::Message.new_response("{}") )
12
12
 
13
13
  client = Kasabi::Search::Client.new("http://api.kasabi.com/api/test-search", :apikey=>"test-key", :client=>mc)
@@ -18,7 +18,7 @@ class SearchTest < Test::Unit::TestCase
18
18
  def test_parameter_search
19
19
  mc = mock()
20
20
  mc.expects(:get).with("http://api.kasabi.com/api/test-search/search",
21
- {:apikey=>"test-key", :query => "lunar", :max => "50", :offset => "10", :output=>"json"}).returns(
21
+ {:query => "lunar", :max => "50", :offset => "10", :output=>"json"}, {"X_KASABI_APIKEY" => "test-key"}).returns(
22
22
  HTTP::Message.new_response("{}") )
23
23
 
24
24
 
@@ -4,174 +4,373 @@ require 'test/unit'
4
4
  require 'mocha'
5
5
 
6
6
  class SparqlTest < Test::Unit::TestCase
7
+
8
+ SELECT_QUERY = <<-EOL
9
+ SELECT ?name WHERE { ?s rdfs:label ?name. }
10
+ EOL
11
+
12
+ ASK_QUERY = <<-EOL
13
+ ASK WHERE { ?s rdfs:label "Something". }
14
+ EOL
15
+
16
+ DESCRIBE_QUERY = <<-EOL
17
+ DESCRIBE <http://www.example.org>
18
+ EOL
7
19
 
8
- def test_simple_query
20
+ CONSTRUCT_QUERY = <<-EOL
21
+ CONSTRUCT { ?s ?p ?o. } WHERE { ?s ?p ?o. }
22
+ EOL
9
23
 
10
- mc = mock()
11
- mc.expects(:get).with("http://www.example.org/sparql", {"query" => "SPARQL"}, {})
24
+ RESULTS = <<-EOL
25
+ {
26
+ "head": { "vars": [ "name" ] } ,
12
27
 
13
- sparql_client = Kasabi::Sparql::Client.new("http://www.example.org/sparql",
14
- {:client => mc})
15
- sparql_client.query("SPARQL")
16
-
17
- end
18
-
19
- def test_query_with_default_graph
28
+ "results": {
29
+ "bindings": [
30
+ { "name": { "type": "literal" , "value": "Apollo 11 Command and Service Module (CSM)" }
31
+ } ,
32
+ { "name": { "type": "literal" , "value": "Apollo 11 SIVB" }
33
+ } ,
34
+ { "name": { "type": "literal" , "value": "Apollo 11 Lunar Module / EASEP" }
35
+ }
36
+ ]
37
+ }
38
+ }
39
+ EOL
40
+
41
+ RESULTS_NO_BINDING = <<-EOL
42
+ {
43
+ "head": { "vars": [ "name" ] } ,
44
+
45
+ "results": {
46
+ "bindings": [
47
+ { } ,
48
+ { "name": { "type": "literal" , "value": "Apollo 11 SIVB" }
49
+ }
50
+ ]
51
+ }
52
+ }
53
+ EOL
54
+
55
+ ASK_RESULTS = <<-EOL
56
+ {
57
+ "head": {},
58
+ "boolean": "true"
59
+ }
60
+ EOL
20
61
 
21
- mc = mock()
22
- mc.expects(:get).with("http://www.example.org/sparql", {"query" => "SPARQL", "default-graph-uri" => ["http://www.example.com"]}, {})
62
+ RDF_JSON_RESULTS = <<-EOL
63
+ {
64
+ "http://www.example.org" : {
65
+ "http://www.example.org/ns/resource" : [ { "value" : "http://www.example.org/page", "type" : "uri" } ]
66
+ }
67
+ }
68
+ EOL
69
+
70
+ RDF_JSON = <<-EOL
71
+ {
72
+ "head": { "vars": [ "name" ] } ,
23
73
 
24
- sparql_client = Kasabi::Sparql::Client.new("http://www.example.org/sparql", {:client => mc})
25
- sparql_client.add_default_graph("http://www.example.com")
26
- sparql_client.query("SPARQL")
27
-
74
+ "results": {
75
+ "bindings": [
76
+ { "name": { "type": "literal" , "value": "Apollo 11 Command and Service Module (CSM)" },
77
+ "uri": { "type": "uri" , "value": "http://nasa.dataincubator.org/spacecraft/12345" },
78
+ "mass": { "type": "literal" , "value": "5000.5", "datatype" : "http://www.w3.org/2001/XMLSchema#float" }
79
+ } ,
80
+ { "name": { "type": "literal" , "value": "Apollo 11 SIVB" },
81
+ "uri": { "type": "uri" , "value": "http://nasa.dataincubator.org/spacecraft/12345" }
82
+ }
83
+ ]
84
+ }
85
+ }
86
+ EOL
87
+
88
+
89
+ def setup
90
+ @json = File.read(File.join(File.dirname(__FILE__), "apollo-6.json"))
28
91
  end
29
-
30
- def test_query_with_named_graph
31
-
92
+
93
+ def test_simple_query
32
94
  mc = mock()
33
- mc.expects(:get).with("http://www.example.org/sparql", {"query" => "SPARQL", "named-graph-uri" => ["http://www.example.com"]}, {})
95
+ mc.expects(:get).with("http://www.example.org/sparql",
96
+ {"query" => "SPARQL"},
97
+ {"X_KASABI_APIKEY" => "test-key"}).returns(
98
+ HTTP::Message.new_response("RESULTS") )
34
99
 
35
- sparql_client = Kasabi::Sparql::Client.new("http://www.example.org/sparql", {:client => mc})
36
- sparql_client.add_named_graph("http://www.example.com")
37
- sparql_client.query("SPARQL")
38
-
100
+ sparql_client = Kasabi::Sparql::Client.new("http://www.example.org/sparql",
101
+ {:apikey => "test-key", :client => mc})
102
+ response = sparql_client.query("SPARQL")
103
+ assert_equal("RESULTS", response)
39
104
  end
40
-
41
- def test_query_with_both_graphs
42
105
 
106
+ def test_query_with_error
43
107
  mc = mock()
44
- mc.expects(:get).with("http://www.example.org/sparql", {"query" => "SPARQL", "named-graph-uri" => ["http://www.example.com"], "default-graph-uri" => ["http://www.example.org"]}, {})
108
+ resp = HTTP::Message.new_response("Error")
109
+ resp.status = 500
110
+ mc.expects(:get).with("http://www.example.org/sparql",
111
+ {"query" => SELECT_QUERY},
112
+ {"Accept" => "application/sparql-results+json", "X_KASABI_APIKEY" => "test-key"}).returns( resp )
45
113
 
46
- sparql_client = Kasabi::Sparql::Client.new("http://www.example.org/sparql", {:client => mc})
47
- sparql_client.add_named_graph("http://www.example.com")
48
- sparql_client.add_default_graph("http://www.example.org")
49
- sparql_client.query("SPARQL")
50
-
51
- end
52
-
53
- def test_sparql_with_mimetype
114
+ client = Kasabi::Sparql::Client.new("http://www.example.org/sparql",
115
+ {:apikey => "test-key", :client => mc})
116
+ assert_raises RuntimeError do
117
+ client.select(SELECT_QUERY)
118
+ end
119
+
120
+ end
121
+ def test_query_with_mimetype
54
122
  mc = mock()
55
- mc.expects(:get).with("http://www.example.org/sparql", {"query" => "SPARQL"}, {"Accept" => "application/sparql-results+xml"})
123
+ mc.expects(:get).with("http://www.example.org/sparql",
124
+ {"query" => "SPARQL"},
125
+ {"X_KASABI_APIKEY" => "test-key", "Accept" => "application/sparql-results+xml"}).returns(
126
+ HTTP::Message.new_response("RESULTS"))
56
127
 
57
- sparql_client = Kasabi::Sparql::Client.new("http://www.example.org/sparql", {:client => mc})
58
- sparql_client.query("SPARQL", "application/sparql-results+xml")
59
-
128
+ sparql_client = Kasabi::Sparql::Client.new("http://www.example.org/sparql", {:client => mc, :apikey=>"test-key"})
129
+ response = sparql_client.query("SPARQL", "application/sparql-results+xml")
130
+ assert_equal("RESULTS", response)
60
131
  end
61
132
 
62
133
  def test_ask
63
134
  mc = mock()
64
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", {"query" => "SPARQL"}, {"Accept" => "application/sparql-results+json"} ).returns(
65
- HTTP::Message.new_response("RESULTS"))
135
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
136
+ {"query" => "ASK"},
137
+ {"Accept" => "application/sparql-results+json", "X_KASABI_APIKEY" => "test-key"} ).returns(
138
+ HTTP::Message.new_response("{ \"boolean\": \"true\" }"))
66
139
 
67
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
68
- response = client.ask("SPARQL")
69
- assert_equal("RESULTS", response.content)
140
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
141
+ response = client.ask("ASK")
142
+ assert_equal(true, response)
70
143
  end
71
-
72
- def test_store_sparql_select
144
+
145
+ def test_exists
73
146
  mc = mock()
74
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", {"query" => "SPARQL"}, {"Accept" => "application/sparql-results+json"} ).returns(
75
- HTTP::Message.new_response("RESULTS"))
147
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
148
+ {"query" => "ASK { <http://www.example.org> ?p ?o }"},
149
+ {"Accept" => "application/sparql-results+json", "X_KASABI_APIKEY" => "test-key"} ).returns(
150
+ HTTP::Message.new_response("{ \"boolean\": \"true\" }"))
151
+
152
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
153
+ response = client.exists?("http://www.example.org")
154
+ assert_equal(true, response)
155
+ end
76
156
 
77
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
157
+ def test_select
158
+ mc = mock()
159
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
160
+ {"query" => "SPARQL"},
161
+ {"Accept" => "application/sparql-results+json", "X_KASABI_APIKEY" => "test-key"} ).returns(
162
+ HTTP::Message.new_response("{}"))
163
+
164
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
78
165
  response = client.select("SPARQL")
79
- assert_equal("RESULTS", response.content)
166
+ assert_not_nil(response)
80
167
  end
81
168
 
82
169
  def test_construct
83
170
  mc = mock()
84
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", {"query" => "SPARQL"}, {"Accept" => "application/rdf+xml"} ).returns(
85
- HTTP::Message.new_response("RESULTS"))
171
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
172
+ {"query" => "SPARQL"},
173
+ {"Accept" => "application/json", "X_KASABI_APIKEY" => "test-key"} ).returns(
174
+ HTTP::Message.new_response( @json ))
86
175
 
87
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
88
- response = client.construct("SPARQL")
89
- assert_equal("RESULTS", response.content)
176
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
177
+ graph = client.construct("SPARQL")
178
+ assert_not_nil( graph )
179
+ assert_equal(12, graph.count )
90
180
  end
91
181
 
92
182
  def test_describe
93
183
  mc = mock()
94
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", {"query" => "SPARQL"}, {"Accept" => "application/rdf+xml"} ).returns(
95
- HTTP::Message.new_response("RESULTS"))
184
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
185
+ {"query" => "SPARQL"},
186
+ {"Accept" => "application/json", "X_KASABI_APIKEY" => "test-key"} ).returns(
187
+ HTTP::Message.new_response(@json))
96
188
 
97
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
98
- response = client.describe("SPARQL")
99
- assert_equal("RESULTS", response.content)
189
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
190
+ graph = client.describe("SPARQL")
191
+ assert_not_nil( graph )
192
+ assert_equal(12, graph.count )
100
193
  end
101
194
 
102
195
  def test_multi_describe
103
196
  mc = mock()
104
197
  mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
105
198
  {"query" => "DESCRIBE <http://www.example.org> <http://www.example.com>"},
106
- {"Accept" => "application/rdf+xml"} ).returns( HTTP::Message.new_response("RESULTS"))
199
+ {"Accept" => "application/json", "X_KASABI_APIKEY" => "test-key"} ).returns(
200
+ HTTP::Message.new_response(@json))
107
201
 
108
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
202
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
109
203
  uris = []
110
204
  uris << "http://www.example.org"
111
205
  uris << "http://www.example.com"
112
- response = client.multi_describe(uris)
113
- assert_equal("RESULTS", response.content)
114
-
206
+ graph = client.multi_describe(uris)
207
+ assert_not_nil( graph )
208
+ assert_equal(12, graph.count )
115
209
  end
116
210
 
117
211
  def test_describe_uri
118
212
  mc = mock()
119
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", {"query" => "DESCRIBE <http://www.example.org>"}, {"Accept" => "application/rdf+xml"} ).returns(
120
- HTTP::Message.new_response("RESULTS"))
213
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
214
+ {"query" => "DESCRIBE <http://www.example.org>"},
215
+ {"Accept" => "application/json", "X_KASABI_APIKEY" => "test-key"} ).returns(
216
+ HTTP::Message.new_response(@json))
121
217
 
122
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
123
- response = client.describe_uri("http://www.example.org")
124
- assert_equal("RESULTS", response.content)
218
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
219
+ graph = client.describe_uri("http://www.example.org")
220
+ assert_not_nil( graph )
221
+ assert_equal(12, graph.count )
125
222
  end
126
223
 
127
224
  def test_describe_uri_using_cbd
128
225
  mc = mock()
129
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", {"query" => "DESCRIBE <http://www.example.org>"}, {"Accept" => "application/rdf+xml"} ).returns(
130
- HTTP::Message.new_response("RESULTS"))
226
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
227
+ {"query" => "DESCRIBE <http://www.example.org>"},
228
+ {"Accept" => "application/json", "X_KASABI_APIKEY" => "test-key"} ).returns(
229
+ HTTP::Message.new_response(@json))
131
230
 
132
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
133
- response = client.describe_uri("http://www.example.org", "application/rdf+xml", :cbd)
134
- assert_equal("RESULTS", response.content)
231
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
232
+ graph = client.describe_uri("http://www.example.org", :cbd)
233
+ assert_not_nil( graph )
234
+ assert_equal(12, graph.count )
135
235
  end
136
236
 
137
237
  def test_describe_uri_using_lcbd
138
238
  mc = mock()
139
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", anything, {"Accept" => "application/rdf+xml"} ).returns(
140
- HTTP::Message.new_response("RESULTS"))
239
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
240
+ anything,
241
+ {"Accept" => "application/json", "X_KASABI_APIKEY" => "test-key"} ).returns(
242
+ HTTP::Message.new_response(@json))
141
243
 
142
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
143
- response = client.describe_uri("http://www.example.org", "application/rdf+xml", :lcbd)
144
- assert_equal("RESULTS", response.content)
244
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
245
+ graph = client.describe_uri("http://www.example.org", :lcbd)
246
+ assert_not_nil( graph )
247
+ assert_equal(12, graph.count )
248
+
145
249
  end
146
250
 
147
251
  def test_describe_uri_using_scbd
148
252
  mc = mock()
149
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", anything, {"Accept" => "application/rdf+xml"} ).returns(
150
- HTTP::Message.new_response("RESULTS"))
253
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
254
+ anything,
255
+ {"Accept" => "application/json", "X_KASABI_APIKEY" => "test-key"} ).returns(
256
+ HTTP::Message.new_response(@json))
151
257
 
152
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
153
- response = client.describe_uri("http://www.example.org", "application/rdf+xml", :scbd)
154
- assert_equal("RESULTS", response.content)
258
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
259
+ graph = client.describe_uri("http://www.example.org", :scbd)
260
+ assert_not_nil( graph )
261
+ assert_equal(12, graph.count )
155
262
  end
156
263
 
157
264
  def test_describe_uri_using_slcbd
158
265
  mc = mock()
159
- mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql", anything, {"Accept" => "application/rdf+xml"} ).returns(
160
- HTTP::Message.new_response("RESULTS"))
266
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
267
+ anything,
268
+ {"Accept" => "application/json", "X_KASABI_APIKEY" => "test-key"} ).returns(
269
+ HTTP::Message.new_response(@json))
161
270
 
162
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
163
- response = client.describe_uri("http://www.example.org", "application/rdf+xml", :slcbd)
164
- assert_equal("RESULTS", response.content)
271
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
272
+ graph = client.describe_uri("http://www.example.org", :slcbd)
273
+ assert_not_nil( graph )
274
+ assert_equal(12, graph.count )
275
+
165
276
  end
166
277
 
167
278
  def test_describe_uri_using_unknown
168
279
  mc = mock()
169
280
 
170
- client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc})
281
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql",
282
+ {"X_KASABI_APIKEY" => "test-key", :client => mc})
283
+
171
284
  assert_raises RuntimeError do
172
- response = client.describe_uri("http://www.example.org", "application/rdf+xml", :unknown)
285
+ response = client.describe_uri("http://www.example.org", :unknown)
173
286
  end
174
287
 
175
288
  end
289
+
290
+ def test_apply_initial_bindings
291
+ query = "SELECT ?p ?o WHERE { ?s ?p ?o }"
292
+ values = { "s" => "<http://www.example.org>" }
293
+
294
+ bound = Kasabi::Sparql::Client.apply_initial_bindings(query, values)
295
+ assert_not_nil(bound)
296
+ assert_equal( "SELECT ?p ?o WHERE { <http://www.example.org> ?p ?o }", bound )
297
+ end
298
+
299
+ def test_apply_initial_bindings_with_dollars
300
+ query = "SELECT $p $o WHERE { $s $p $o }"
301
+ values = { "s" => "<http://www.example.org>" }
302
+
303
+ bound = Kasabi::Sparql::Client.apply_initial_bindings(query, values)
304
+ assert_not_nil(bound)
305
+ assert_equal( "SELECT $p $o WHERE { <http://www.example.org> $p $o }", bound )
306
+ end
307
+
308
+ def test_apply_initial_bindings_with_literal
309
+ query = "SELECT ?s WHERE { ?s ?p ?o }"
310
+ values = { "o" => "'some value'" }
311
+
312
+ bound = Kasabi::Sparql::Client.apply_initial_bindings(query, values)
313
+ assert_not_nil(bound)
314
+ assert_equal( "SELECT ?s WHERE { ?s ?p 'some value' }", bound )
315
+ end
316
+
317
+ def test_binding_to_hash
318
+ json = JSON.parse(RDF_JSON)
319
+ binding = json["results"]["bindings"][0]
320
+
321
+ hash = Kasabi::Sparql::Client.result_to_query_binding(binding)
322
+ assert_equal(3, hash.size)
323
+ assert_equal("\"Apollo 11 Command and Service Module (CSM)\"", hash["name"])
324
+ assert_equal("<http://nasa.dataincubator.org/spacecraft/12345>", hash["uri"])
325
+ assert_equal("\"5000.5\"^^http://www.w3.org/2001/XMLSchema#float", hash["mass"])
326
+ end
327
+
328
+ def test_results_to_bindings
329
+ json = JSON.parse(RDF_JSON)
330
+ bindings = Kasabi::Sparql::Client.results_to_query_bindings(json)
331
+ assert_equal(2, bindings.size)
332
+ hash = bindings[0]
333
+ assert_equal("\"Apollo 11 Command and Service Module (CSM)\"", hash["name"])
334
+ assert_equal("<http://nasa.dataincubator.org/spacecraft/12345>", hash["uri"])
335
+ assert_equal("\"5000.5\"^^http://www.w3.org/2001/XMLSchema#float", hash["mass"])
336
+ end
337
+
338
+ def test_select_values
339
+ mc = mock()
340
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
341
+ {"query" => SELECT_QUERY},
342
+ {"Accept" => "application/sparql-results+json", "X_KASABI_APIKEY" => "test-key"} ).returns(
343
+ HTTP::Message.new_response(RESULTS))
344
+
345
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
346
+ results = client.select_values(SELECT_QUERY)
347
+ assert_not_nil( results )
348
+ assert_equal( 3, results.length )
349
+ end
350
+
351
+ def test_select_values_with_empty_binding
352
+ mc = mock()
353
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
354
+ {"query" => SELECT_QUERY},
355
+ {"Accept" => "application/sparql-results+json", "X_KASABI_APIKEY" => "test-key"} ).returns(
356
+ HTTP::Message.new_response(RESULTS_NO_BINDING))
357
+
358
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
359
+ results = client.select_values(SELECT_QUERY)
360
+ assert_not_nil( results )
361
+ assert_equal( 1, results.length )
362
+ end
363
+
364
+ def test_select_single_value
365
+ mc = mock()
366
+ mc.expects(:get).with("http://api.talis.com/stores/testing/services/sparql",
367
+ {"query" => SELECT_QUERY},
368
+ {"Accept" => "application/sparql-results+json", "X_KASABI_APIKEY" => "test-key"} ).returns(
369
+ HTTP::Message.new_response(RESULTS))
176
370
 
371
+ client = Kasabi::Sparql::Client.new("http://api.talis.com/stores/testing/services/sparql", {:client => mc, :apikey=>"test-key"})
372
+ result = client.select_single_value(SELECT_QUERY)
373
+ assert_equal( "Apollo 11 Command and Service Module (CSM)", result )
374
+ end
375
+
177
376
  end