ldp 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe92f6866205a3b27b56215e6f55184f38dd9dfa
4
- data.tar.gz: 5b92b3da6682fa7d5fc329a79c42acf198294d57
3
+ metadata.gz: 5891836d3638c426292b938c2e815bc78963a486
4
+ data.tar.gz: e846d7a47515ee66db76b539979b01024b39f56d
5
5
  SHA512:
6
- metadata.gz: 746f3dac610884ad80e1db2cb0b6924c63891e4731a2efd1f5c77285eb3cf7aee3c3e153216570ef8f0cfa756e021b87dadca32d30a9f3c51b9e13e4ec2e8bdc
7
- data.tar.gz: 1fe3736a07b3bac70583327dbc3a1ceb1a60c5438ce52c21e0c815f9b8f21118c8487e7a1ffc22d957132be343a5ba0bf22f3d3c46bd868f2beab0bca2358610
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|
@@ -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
@@ -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? || !head
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 ||= @get || client.head(subject)
47
+ @head ||= begin
48
+ @get || client.head(subject)
49
+ rescue Ldp::NotFound
50
+ None
51
+ end
50
52
  end
51
53
 
52
54
  ##
@@ -1,3 +1,3 @@
1
1
  module Ldp
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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.1.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-04 00:00:00.000000000 Z
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday