ldp 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ldp.rb +6 -4
- data/lib/ldp/client/methods.rb +4 -2
- data/lib/ldp/response.rb +18 -4
- data/lib/ldp/uri.rb +12 -4
- data/lib/ldp/version.rb +1 -1
- data/spec/lib/ldp/client_spec.rb +32 -16
- data/spec/lib/ldp/orm/orm_spec.rb +3 -3
- data/spec/lib/ldp/response_spec.rb +48 -28
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9fff3eeeb00aa2238f94ece780c316f278afe7e
|
4
|
+
data.tar.gz: 64b98dd94fe94d5543effc81f2a91effcbc0a39c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77c0d192ad0494b4c4c0eeec7d9a453305965d4a241186fcae6157aabd8ec2150ede530cc54528d635d7eecbb0b4062436a64890a42b243688acb8a5545d1863
|
7
|
+
data.tar.gz: da2fee235e20c876ab531e340abd00c9c754f16fe6825b32989f7ecdc4a317d0a15fc3b22363c8f0d37424f823151bb1db91cad7f9128e3e6857d21f68faee01
|
data/lib/ldp.rb
CHANGED
@@ -18,8 +18,11 @@ module Ldp
|
|
18
18
|
autoload :Orm, 'ldp/orm'
|
19
19
|
|
20
20
|
class HttpError < RuntimeError; end
|
21
|
-
class NotFound < HttpError; end
|
22
|
-
class
|
21
|
+
class NotFound < HttpError; end # 404
|
22
|
+
class Gone < HttpError; end # 410
|
23
|
+
class EtagMismatch < HttpError; end # 412
|
24
|
+
|
25
|
+
class UnexpectedContentType < RuntimeError; end
|
23
26
|
|
24
27
|
class << self
|
25
28
|
def logger
|
@@ -27,7 +30,7 @@ module Ldp
|
|
27
30
|
log.level = Logger::WARN
|
28
31
|
end
|
29
32
|
end
|
30
|
-
|
33
|
+
|
31
34
|
def instrument *args, &block
|
32
35
|
if defined?(::ActiveSupport) && defined?(::ActiveSupport::Notifications)
|
33
36
|
ActiveSupport::Notifications.instrument *args, &block
|
@@ -38,5 +41,4 @@ module Ldp
|
|
38
41
|
|
39
42
|
attr_writer :logger
|
40
43
|
end
|
41
|
-
|
42
44
|
end
|
data/lib/ldp/client/methods.rb
CHANGED
@@ -106,9 +106,11 @@ module Ldp::Client::Methods
|
|
106
106
|
unless resp.success?
|
107
107
|
raise case resp.status
|
108
108
|
when 404
|
109
|
-
Ldp::NotFound.new(resp.body)
|
109
|
+
Ldp::NotFound.new(resp.body)
|
110
|
+
when 410
|
111
|
+
Ldp::Gone.new(resp.body)
|
110
112
|
when 412
|
111
|
-
Ldp::EtagMismatch.new(resp.body)
|
113
|
+
Ldp::EtagMismatch.new(resp.body)
|
112
114
|
else
|
113
115
|
Ldp::HttpError.new("STATUS: #{resp.status} #{resp.body[0, 1000]}...")
|
114
116
|
end
|
data/lib/ldp/response.rb
CHANGED
@@ -47,12 +47,19 @@ module Ldp
|
|
47
47
|
# Is the response an LDP container?
|
48
48
|
def self.container? response
|
49
49
|
[
|
50
|
-
Ldp.basic_container,
|
51
|
-
Ldp.direct_container,
|
50
|
+
Ldp.basic_container,
|
51
|
+
Ldp.direct_container,
|
52
52
|
Ldp.indirect_container
|
53
53
|
].any? { |x| Array(links(response)["type"]).include? x.to_s }
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
|
+
##
|
57
|
+
# Is the response an LDP RDFSource?
|
58
|
+
# ldp:Container is a subclass of ldp:RDFSource
|
59
|
+
def self.rdf_source? response
|
60
|
+
container?(response) || Array(links(response)["type"]).include?(Ldp.rdf_source)
|
61
|
+
end
|
62
|
+
|
56
63
|
##
|
57
64
|
# Link: headers from the HTTP response
|
58
65
|
def links
|
@@ -65,6 +72,12 @@ module Ldp
|
|
65
72
|
Ldp::Response.resource?(self)
|
66
73
|
end
|
67
74
|
|
75
|
+
##
|
76
|
+
# Is the response an LDP rdf source?
|
77
|
+
def rdf_source?
|
78
|
+
Ldp::Response.rdf_source?(self)
|
79
|
+
end
|
80
|
+
|
68
81
|
##
|
69
82
|
# Is the response an LDP container
|
70
83
|
def container?
|
@@ -89,13 +102,14 @@ module Ldp
|
|
89
102
|
##
|
90
103
|
# Is the response paginated?
|
91
104
|
def has_page?
|
92
|
-
graph.has_statement?
|
105
|
+
rdf_source? && graph.has_statement?(RDF::Statement.new(page_subject, RDF.type, Ldp.page))
|
93
106
|
end
|
94
107
|
|
95
108
|
##
|
96
109
|
# Get the graph for the resource (or a blank graph if there is no metadata for the resource)
|
97
110
|
def graph
|
98
111
|
@graph ||= begin
|
112
|
+
raise UnexpectedContentType, "The resource #{page_subject} is not an RDFSource" unless rdf_source?
|
99
113
|
graph = RDF::Graph.new
|
100
114
|
|
101
115
|
if resource?
|
data/lib/ldp/uri.rb
CHANGED
@@ -8,6 +8,14 @@ module Ldp::Uri
|
|
8
8
|
uri("Resource")
|
9
9
|
end
|
10
10
|
|
11
|
+
def rdf_source
|
12
|
+
uri("RDFSource")
|
13
|
+
end
|
14
|
+
|
15
|
+
def non_rdf_source
|
16
|
+
uri("NonRDFSource")
|
17
|
+
end
|
18
|
+
|
11
19
|
def container
|
12
20
|
uri("Container")
|
13
21
|
end
|
@@ -15,19 +23,19 @@ module Ldp::Uri
|
|
15
23
|
def basic_container
|
16
24
|
uri("BasicContainer")
|
17
25
|
end
|
18
|
-
|
26
|
+
|
19
27
|
def direct_container
|
20
28
|
uri("DirectContainer")
|
21
29
|
end
|
22
|
-
|
30
|
+
|
23
31
|
def indirect_container
|
24
32
|
uri("IndirectContainer")
|
25
33
|
end
|
26
|
-
|
34
|
+
|
27
35
|
def contains
|
28
36
|
uri("contains")
|
29
37
|
end
|
30
|
-
|
38
|
+
|
31
39
|
def page
|
32
40
|
uri("Page")
|
33
41
|
end
|
data/lib/ldp/version.rb
CHANGED
data/spec/lib/ldp/client_spec.rb
CHANGED
@@ -21,17 +21,20 @@ describe "Ldp::Client" do
|
|
21
21
|
|
22
22
|
let(:conn_stubs) do
|
23
23
|
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
24
|
-
stub.head('/a_resource') {[
|
25
|
-
stub.get('/a_resource') {[
|
26
|
-
stub.get('/a_container') {[
|
27
|
-
stub.head('/a_binary_resource') { [200]}
|
28
|
-
stub.get('/a_binary_resource') { [200, {}, ""]}
|
24
|
+
stub.head('/a_resource') { [200] }
|
25
|
+
stub.get('/a_resource') { [200, {"Link" => "<http://www.w3.org/ns/ldp#Resource>;rel=\"type\""}, simple_graph] }
|
26
|
+
stub.get('/a_container') { [200, {"Link" => ["<http://www.w3.org/ns/ldp#Resource>;rel=\"type\"","<http://www.w3.org/ns/ldp#BasicContainer>;rel=\"type\""]}, simple_container_graph] }
|
27
|
+
stub.head('/a_binary_resource') { [200] }
|
28
|
+
stub.get('/a_binary_resource') { [200, {}, ""] }
|
29
29
|
stub.put("/a_resource") { [204]}
|
30
|
-
stub.delete("/a_resource") { [204]}
|
31
|
-
stub.head('/a_container') {[
|
32
|
-
stub.post("/a_container") { [201, {"Location" => "http://example.com/a_container/subresource"}]}
|
30
|
+
stub.delete("/a_resource") { [204] }
|
31
|
+
stub.head('/a_container') { [200] }
|
32
|
+
stub.post("/a_container") { [201, {"Location" => "http://example.com/a_container/subresource"}] }
|
33
33
|
stub.get("/test:1") { [200] }
|
34
34
|
stub.get("http://test:8080/abc") { [200] }
|
35
|
+
stub.put("/mismatch_resource") { [412] }
|
36
|
+
stub.put("/conflict_resource") { [409, {}, ''] }
|
37
|
+
stub.get("/deleted_resource") { [410, {}, 'Gone'] }
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
@@ -61,7 +64,6 @@ describe "Ldp::Client" do
|
|
61
64
|
end
|
62
65
|
|
63
66
|
describe "get" do
|
64
|
-
|
65
67
|
it "should GET content from the HTTP endpoint" do
|
66
68
|
resp = subject.get "a_resource"
|
67
69
|
expect(resp).to be_a_kind_of(Ldp::Response)
|
@@ -72,7 +74,7 @@ describe "Ldp::Client" do
|
|
72
74
|
it "should accept a block to change the HTTP request" do
|
73
75
|
expect { |b| subject.get "a_resource", &b }.to yield_control
|
74
76
|
end
|
75
|
-
|
77
|
+
|
76
78
|
context "should provide convenient accessors for LDP Prefer headers" do
|
77
79
|
it "should set the minimal header" do
|
78
80
|
subject.get "a_resource", minimal: true do |req|
|
@@ -90,13 +92,13 @@ describe "Ldp::Client" do
|
|
90
92
|
end
|
91
93
|
end
|
92
94
|
end
|
93
|
-
|
95
|
+
|
94
96
|
context "with an invalid relative uri" do
|
95
97
|
it "should work" do
|
96
98
|
subject.get "test:1"
|
97
99
|
end
|
98
100
|
end
|
99
|
-
|
101
|
+
|
100
102
|
context "with an absolute uri" do
|
101
103
|
it "should work" do
|
102
104
|
subject.get "http://test:8080/abc"
|
@@ -130,13 +132,13 @@ describe "Ldp::Client" do
|
|
130
132
|
|
131
133
|
it "should set default Content-type" do
|
132
134
|
subject.post "a_container", 'foo' do |req|
|
133
|
-
expect(req.headers).to eq({ "Content-Type" => "text/turtle" })
|
135
|
+
expect(req.headers).to eq({ "Content-Type" => "text/turtle" })
|
134
136
|
end
|
135
137
|
end
|
136
138
|
|
137
139
|
it "should set headers" do
|
138
140
|
subject.post "a_container", 'foo', {'Content-Type' => 'application/pdf'} do |req|
|
139
|
-
expect(req.headers).to eq({ "Content-Type" => "application/pdf" })
|
141
|
+
expect(req.headers).to eq({ "Content-Type" => "application/pdf" })
|
140
142
|
end
|
141
143
|
end
|
142
144
|
|
@@ -162,7 +164,21 @@ describe "Ldp::Client" do
|
|
162
164
|
|
163
165
|
it "should set headers" do
|
164
166
|
subject.put "a_resource", 'payload', {'Content-Type' => 'application/pdf'} do |req|
|
165
|
-
expect(req.headers).to eq({ "Content-Type" => "application/pdf" })
|
167
|
+
expect(req.headers).to eq({ "Content-Type" => "application/pdf" })
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "error checking" do
|
172
|
+
it "should check for 409 errors" do
|
173
|
+
expect { subject.put "conflict_resource", "some-payload" }.to raise_error Ldp::HttpError
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should check for 410 errors" do
|
177
|
+
expect { subject.get "deleted_resource" }.to raise_error Ldp::Gone
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should check for 412 errors" do
|
181
|
+
expect { subject.put "mismatch_resource", "some-payload" }.to raise_error Ldp::EtagMismatch
|
166
182
|
end
|
167
183
|
end
|
168
184
|
end
|
@@ -179,7 +195,7 @@ describe "Ldp::Client" do
|
|
179
195
|
expect(resource).to be_a_kind_of(Ldp::Resource)
|
180
196
|
expect(resource).to be_a_kind_of(Ldp::Container)
|
181
197
|
end
|
182
|
-
|
198
|
+
|
183
199
|
it "should be a binary resource" do
|
184
200
|
resource = subject.find_or_initialize "a_binary_resource"
|
185
201
|
expect(resource).to be_a_kind_of(Ldp::Resource::BinarySource)
|
@@ -2,14 +2,14 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Ldp::Orm do
|
4
4
|
subject { Ldp::Orm.new test_resource }
|
5
|
-
|
5
|
+
|
6
6
|
let(:simple_graph) do
|
7
7
|
RDF::Graph.new << [RDF::URI.new(""), RDF::DC.title, "Hello, world!"]
|
8
8
|
end
|
9
9
|
|
10
10
|
let(:conn_stubs) do
|
11
11
|
Faraday::Adapter::Test::Stubs.new do |stub|
|
12
|
-
stub.get('/a_resource') {[ 200, {"Link" => "<http://www.w3.org/ns/ldp#Resource>;rel=\"type\""}, simple_graph.dump(:ttl) ]}
|
12
|
+
stub.get('/a_resource') {[ 200, {"Link" => "<http://www.w3.org/ns/ldp#DirectContainer>;rel=\"type\", <http://www.w3.org/ns/ldp#Resource>;rel=\"type\""}, simple_graph.dump(:ttl) ]}
|
13
13
|
stub.head('/a_resource') { [200] }
|
14
14
|
stub.put("/a_resource") { [204]}
|
15
15
|
end
|
@@ -81,7 +81,7 @@ describe Ldp::Orm do
|
|
81
81
|
before do
|
82
82
|
updated_graph = RDF::Graph.new << [RDF::URI.new(""), RDF::DC.title, "Hello again, world!"]
|
83
83
|
conn_stubs.get('/a_resource') {[200,
|
84
|
-
{"Link" => "<http://www.w3.org/ns/ldp#Resource>;rel=\"type\"",
|
84
|
+
{"Link" => "<http://www.w3.org/ns/ldp#Resource>;rel=\"type\", <http://www.w3.org/ns/ldp#DirectContainer>;rel=\"type\"",
|
85
85
|
"ETag" => "new-tag"},
|
86
86
|
updated_graph.dump(:ttl)]}
|
87
87
|
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Ldp::Response do
|
4
|
-
|
4
|
+
LDP_RDF_RESOURCE_HEADERS = { "Link" => "<#{Ldp.resource.to_s}>;rel=\"type\", <#{Ldp.direct_container.to_s}>;rel=\"type\""}
|
5
|
+
LDP_NON_RDF_SOURCE_HEADERS = { "Link" => "<#{Ldp.resource.to_s}>;rel=\"type\", <#{Ldp.non_rdf_source.to_s}>;rel=\"type\""}
|
5
6
|
|
6
7
|
let(:mock_response) { double(headers: {}, env: { url: "info:a" }) }
|
7
8
|
let(:mock_client) { double(Ldp::Client) }
|
@@ -48,21 +49,30 @@ describe Ldp::Response do
|
|
48
49
|
allow(mock_response).to receive(:headers).and_return(
|
49
50
|
"Link" => [
|
50
51
|
"<#{Ldp.resource}>;rel=\"type\""
|
51
|
-
]
|
52
|
+
]
|
52
53
|
)
|
53
|
-
expect(Ldp::Response.resource? mock_response).to
|
54
|
+
expect(Ldp::Response.resource? mock_response).to be true
|
54
55
|
end
|
55
56
|
end
|
56
57
|
|
57
58
|
describe "#graph" do
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
59
|
+
context "for an RDFSource (or Container)" do
|
60
|
+
it "should parse the response body for an RDF graph" do
|
61
|
+
allow(mock_response).to receive(:body).and_return("<> <info:b> <info:c> .")
|
62
|
+
allow(mock_response).to receive(:headers).and_return(LDP_RDF_RESOURCE_HEADERS)
|
63
|
+
graph = subject.graph
|
64
|
+
|
65
|
+
expect(graph).to have_subject(RDF::URI.new("info:a"))
|
66
|
+
expect(graph).to have_statement RDF::Statement.new(RDF::URI.new("info:a"), RDF::URI.new("info:b"), RDF::URI.new("info:c"))
|
67
|
+
end
|
68
|
+
end
|
65
69
|
|
70
|
+
context "for a NonRDFSource" do
|
71
|
+
it "should parse the response body for an RDF graph" do
|
72
|
+
allow(mock_response).to receive(:body).and_return("<> <info:b> <info:c> .")
|
73
|
+
allow(mock_response).to receive(:headers).and_return(LDP_NON_RDF_SOURCE_HEADERS)
|
74
|
+
expect { subject.graph }.to raise_error Ldp::UnexpectedContentType
|
75
|
+
end
|
66
76
|
end
|
67
77
|
end
|
68
78
|
|
@@ -81,23 +91,34 @@ describe Ldp::Response do
|
|
81
91
|
end
|
82
92
|
end
|
83
93
|
|
84
|
-
describe "#has_page" do
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
+
describe "#has_page?" do
|
95
|
+
context "for an RDF Source" do
|
96
|
+
before do
|
97
|
+
allow(mock_response).to receive(:headers).and_return(LDP_RDF_RESOURCE_HEADERS)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should see if the response has an ldp:Page statement" do
|
101
|
+
graph = RDF::Graph.new
|
102
|
+
graph << [RDF::URI.new('info:a'), RDF.type, Ldp.page]
|
103
|
+
allow(mock_response).to receive(:body).and_return(graph.dump(:ttl))
|
104
|
+
expect(subject).to have_page
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should be false otherwise" do
|
108
|
+
# allow(subject).to receive(:page_subject).and_return RDF::URI.new('info:a')
|
109
|
+
graph = RDF::Graph.new
|
110
|
+
allow(mock_response).to receive(:body).and_return(graph.dump(:ttl))
|
111
|
+
expect(subject).not_to have_page
|
112
|
+
end
|
94
113
|
end
|
95
114
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
115
|
+
context "with a non-rdf-source" do
|
116
|
+
it "should be false" do
|
117
|
+
# allow(subject).to receive(:page_subject).and_return RDF::URI.new('info:a')
|
118
|
+
# allow(mock_response).to receive(:body).and_return('')
|
119
|
+
allow(mock_response).to receive(:headers).and_return(LDP_NON_RDF_SOURCE_HEADERS)
|
120
|
+
expect(subject).not_to have_page
|
121
|
+
end
|
101
122
|
end
|
102
123
|
end
|
103
124
|
|
@@ -109,17 +130,16 @@ describe Ldp::Response do
|
|
109
130
|
graph << [RDF::URI.new('info:b'), RDF.type, Ldp.page]
|
110
131
|
|
111
132
|
allow(mock_response).to receive(:body).and_return(graph.dump(:ttl))
|
112
|
-
allow(mock_response).to receive(:headers).and_return(
|
133
|
+
allow(mock_response).to receive(:headers).and_return(LDP_RDF_RESOURCE_HEADERS)
|
113
134
|
|
114
135
|
expect(subject.page.count).to eq(1)
|
115
|
-
|
116
136
|
end
|
117
137
|
end
|
118
138
|
|
119
139
|
describe "#subject" do
|
120
140
|
it "should extract the HTTP request URI as an RDF URI" do
|
121
141
|
allow(mock_response).to receive(:body).and_return('')
|
122
|
-
allow(mock_response).to receive(:headers).and_return(
|
142
|
+
allow(mock_response).to receive(:headers).and_return(LDP_RDF_RESOURCE_HEADERS)
|
123
143
|
allow(mock_response).to receive(:env).and_return(:url => 'http://xyz/a')
|
124
144
|
expect(subject.subject).to eq(RDF::URI.new("http://xyz/a"))
|
125
145
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ldp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Beer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|