ldp 0.1.0 → 0.2.0
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 +9 -0
- data/lib/ldp/client/methods.rb +8 -0
- data/lib/ldp/resource.rb +6 -4
- data/lib/ldp/version.rb +1 -1
- data/spec/lib/ldp/resource_spec.rb +31 -5
- 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: 5891836d3638c426292b938c2e815bc78963a486
|
4
|
+
data.tar.gz: e846d7a47515ee66db76b539979b01024b39f56d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f82a7c37efd44e165cfba252c64073255d805074ba78e92a3a5ea2d988407b13987b9947614102b120b006578a15ddb43f68e30904cdcc006765ab79c48dec36
|
7
|
+
data.tar.gz: 44771b0779cbd68c5c0f8b2917176ca9de4a36290b3d433318bf859b77688e1e6164e28150af0ee3a66612ea304944ce235c886ffd5f93f8ac7cb626645e1bb7
|
data/lib/ldp.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'ldp/version'
|
2
2
|
require 'linkeddata'
|
3
3
|
require 'logger'
|
4
|
+
require 'singleton'
|
4
5
|
|
5
6
|
module Ldp
|
6
7
|
RDF::Graph.send(:include, RDF::Isomorphic)
|
@@ -17,12 +18,20 @@ module Ldp
|
|
17
18
|
autoload :Orm, 'ldp/orm'
|
18
19
|
|
19
20
|
class HttpError < RuntimeError; end
|
21
|
+
class BadRequest < HttpError; end # 400
|
20
22
|
class NotFound < HttpError; end # 404
|
21
23
|
class Gone < HttpError; end # 410
|
22
24
|
class EtagMismatch < HttpError; end # 412
|
23
25
|
|
24
26
|
class UnexpectedContentType < RuntimeError; end
|
25
27
|
|
28
|
+
# Returned when there is no result (e.g. 404)
|
29
|
+
class NoneClass
|
30
|
+
include Singleton
|
31
|
+
end
|
32
|
+
# The single global instance of NoneClass, representing the empty Option
|
33
|
+
None = NoneClass.instance # :doc:
|
34
|
+
|
26
35
|
class << self
|
27
36
|
def logger
|
28
37
|
@logger ||= Logger.new(STDOUT).tap do |log|
|
data/lib/ldp/client/methods.rb
CHANGED
@@ -105,6 +105,14 @@ module Ldp::Client::Methods
|
|
105
105
|
resp.tap do |resp|
|
106
106
|
unless resp.success?
|
107
107
|
raise case resp.status
|
108
|
+
when 400
|
109
|
+
if resp.env.method == :head
|
110
|
+
# If the request was a HEAD request (which only retrieves HTTP headers),
|
111
|
+
# re-run it as a GET in order to retrieve a message body (which is passed on as the error message)
|
112
|
+
get(resp.env.url.path)
|
113
|
+
else
|
114
|
+
Ldp::BadRequest.new(resp.body)
|
115
|
+
end
|
108
116
|
when 404
|
109
117
|
Ldp::NotFound.new(resp.body)
|
110
118
|
when 410
|
data/lib/ldp/resource.rb
CHANGED
@@ -28,9 +28,7 @@ module Ldp
|
|
28
28
|
##
|
29
29
|
# Is the resource new, or does it exist in the LDP server?
|
30
30
|
def new?
|
31
|
-
subject.nil? ||
|
32
|
-
rescue Ldp::NotFound
|
33
|
-
true
|
31
|
+
subject.nil? || head == None
|
34
32
|
end
|
35
33
|
|
36
34
|
##
|
@@ -46,7 +44,11 @@ module Ldp
|
|
46
44
|
end
|
47
45
|
|
48
46
|
def head
|
49
|
-
@head ||=
|
47
|
+
@head ||= begin
|
48
|
+
@get || client.head(subject)
|
49
|
+
rescue Ldp::NotFound
|
50
|
+
None
|
51
|
+
end
|
50
52
|
end
|
51
53
|
|
52
54
|
##
|
data/lib/ldp/version.rb
CHANGED
@@ -5,6 +5,8 @@ describe Ldp::Resource do
|
|
5
5
|
|
6
6
|
let(:conn_stubs) do
|
7
7
|
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
8
|
+
stub.head('/bad_request_resource') { [400] } # HEAD requests do not have message bodies.
|
9
|
+
stub.get('/bad_request_resource') { [400, {}, "The namespace prefix (fooooooo) has not been registered"] }
|
8
10
|
stub.head('/not_found_resource') { [404] }
|
9
11
|
stub.get('/not_found_resource') { [404] }
|
10
12
|
stub.head('/a_new_resource') { [404] }
|
@@ -31,6 +33,16 @@ describe Ldp::Resource do
|
|
31
33
|
expect{ subject.get }.to raise_error Ldp::NotFound
|
32
34
|
end
|
33
35
|
end
|
36
|
+
context "when the request is bad" do
|
37
|
+
let(:path) { '/bad_request_resource' }
|
38
|
+
it "should return a meaningful error message" do
|
39
|
+
# Ensures that failed head requests rerun as a GET request in order to get a meaningful error message
|
40
|
+
expect{ subject.head }.to raise_error Ldp::BadRequest, "The namespace prefix (fooooooo) has not been registered"
|
41
|
+
end
|
42
|
+
it "should raise an error with error message" do
|
43
|
+
expect{ subject.get }.to raise_error Ldp::BadRequest, "The namespace prefix (fooooooo) has not been registered"
|
44
|
+
end
|
45
|
+
end
|
34
46
|
|
35
47
|
context "when the resource is in the repository" do
|
36
48
|
let(:path) { '/a_resource' }
|
@@ -40,23 +52,37 @@ describe Ldp::Resource do
|
|
40
52
|
end
|
41
53
|
end
|
42
54
|
end
|
43
|
-
|
55
|
+
|
44
56
|
describe "#new?" do
|
45
57
|
context "with an object not in the repository" do
|
46
58
|
let(:path) { '/not_found_resource' }
|
47
|
-
it "should be true" do
|
59
|
+
it "should be true" do
|
48
60
|
expect(subject).to be_new
|
49
61
|
end
|
50
62
|
end
|
51
|
-
|
63
|
+
|
52
64
|
context "with an object in the repository" do
|
53
65
|
let(:path) { '/a_resource' }
|
54
|
-
it "should be false" do
|
66
|
+
it "should be false" do
|
55
67
|
expect(subject).to_not be_new
|
56
68
|
end
|
57
69
|
end
|
58
70
|
end
|
59
|
-
|
71
|
+
|
72
|
+
describe "#head" do
|
73
|
+
context "with an object not in the repository" do
|
74
|
+
let(:path) { '/not_found_resource' }
|
75
|
+
it "should be true" do
|
76
|
+
expect(subject.head).to eq Ldp::None
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should cache requests" do
|
80
|
+
expect(subject.client).to receive(:head).and_raise(Ldp::NotFound).once
|
81
|
+
2.times { subject.head }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
60
86
|
describe "#create" do
|
61
87
|
let(:path) { '/a_new_resource' }
|
62
88
|
context "with a subject uri" do
|
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.
|
4
|
+
version: 0.2.0
|
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-12-
|
11
|
+
date: 2014-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|