ldp 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17053f7fbf04e6b64d7ffeed9be3f913e9a26cc9
4
- data.tar.gz: ce8e5c9c59bad81c8fb1fad3216578f8379027d1
3
+ metadata.gz: e8f3d184e1548ea9060af3c059322168f6753e13
4
+ data.tar.gz: 69d0817c390fbcbff78fb98adf180ddd1e0be33b
5
5
  SHA512:
6
- metadata.gz: 5428c3dbfc518bd48967f849d0b4a42aad3d967d13c35f6725449189223bf0d3fd40f7c00ef349dbaa316b2a05c650cd33ba613b5969848c23e2f1bd2527bc91
7
- data.tar.gz: 4bc45e92606ee0940cee48a937eb9ff53a15f2adf7bc533e81af5cc075410c4b8f46318bb8374c61f7969d30a07455655642fbaa95f7110e2ac07e8429ab78f1
6
+ metadata.gz: aab3b961a934cd34e826a6504c079e2531826fbfe0b6b4b13ca63e0cb6f3edefdc6595be6931994f471605c96097e5309dd13116d3dcbe86ec259dc7b9629e3f
7
+ data.tar.gz: 0f8212e7fd4e83f494cda059ad2acb6395129b239a36df47d8abdb7d9d67ad93bb7377d1675f1a0cd2f70a4552cedd65553477ebe2016bf27cffdea53178364e
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["chris@cbeer.info"]
11
11
  spec.description = %q{Linked Data Platform client library}
12
12
  spec.summary = spec.description
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/cbeer/ldp"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -6,10 +6,11 @@ module Ldp
6
6
  attr_reader :client, :subject
7
7
  attr_accessor :content
8
8
 
9
- def initialize client, subject, response = nil
9
+ def initialize client, subject, response = nil, base_path = ''
10
10
  @client = client
11
11
  @subject = subject
12
12
  @get = response if response.is_a? Faraday::Response and current? response
13
+ @base_path = base_path
13
14
  end
14
15
 
15
16
  ##
@@ -65,7 +66,8 @@ module Ldp
65
66
  # @return [RdfSource] the new representation
66
67
  def create &block
67
68
  raise "Can't call create on an existing resource" unless new?
68
- resp = client.post((subject || ""), content) do |req|
69
+ verb = subject.nil? ? :post : :put
70
+ resp = client.send(verb, (subject || @base_path), content) do |req|
69
71
 
70
72
  yield req if block_given?
71
73
  end
@@ -79,9 +81,11 @@ module Ldp
79
81
  # Update the stored graph
80
82
  def update new_content = nil
81
83
  new_content ||= content
82
- client.put subject, new_content do |req|
84
+ resp = client.put subject, new_content do |req|
83
85
  req.headers['If-Match'] = get.etag if retrieved_content?
84
86
  end
87
+ update_cached_get(resp) if retrieved_content?
88
+ resp
85
89
  end
86
90
 
87
91
  def current? response = nil
@@ -95,5 +99,15 @@ module Ldp
95
99
  new_response.headers['ETag'] == response.headers['ETag'] &&
96
100
  new_response.headers['Last-Modified'] == response.headers['Last-Modified']
97
101
  end
102
+
103
+ def update_cached_get response
104
+ Response.wrap(client, response)
105
+ if response.etag.nil? || response.last_modified.nil?
106
+ response = Response.wrap(client, client.head(subject))
107
+ end
108
+ @get.etag = response.etag
109
+ @get.last_modified = response.last_modified
110
+ end
111
+
98
112
  end
99
113
  end
@@ -2,8 +2,8 @@ module Ldp
2
2
  class Resource::BinarySource < Ldp::Resource
3
3
  attr_accessor :content
4
4
 
5
- def initialize client, subject, content_or_response = nil
6
- super client, subject, content_or_response
5
+ def initialize client, subject, content_or_response = nil, base_path = ''
6
+ super
7
7
 
8
8
  case content_or_response
9
9
  when Faraday::Response
@@ -1,8 +1,8 @@
1
1
  module Ldp
2
2
  class Resource::RdfSource < Ldp::Resource
3
3
 
4
- def initialize client, subject, graph_or_response = nil
5
- super client, subject, graph_or_response
4
+ def initialize client, subject, graph_or_response = nil, base_path = ''
5
+ super
6
6
 
7
7
  case graph_or_response
8
8
  when RDF::Graph
@@ -15,7 +15,7 @@ module Ldp
15
15
  def self.links response
16
16
  h = {}
17
17
  Array(response.headers["Link"]).map { |x| x.split(", ") }.flatten.inject(h) do |memo, header|
18
- m = header.match(/(?<link>.*);\s?rel="(?<rel>[^"]+)"/)
18
+ m = header.match(/<(?<link>.*)>;\s?rel="(?<rel>[^"]+)"/)
19
19
  if m
20
20
  memo[m[:rel]] ||= []
21
21
  memo[m[:rel]] << m[:link]
@@ -113,13 +113,21 @@ module Ldp
113
113
  ##
114
114
  # Extract the ETag for the resource
115
115
  def etag
116
- headers['ETag']
116
+ @etag ||= headers['ETag']
117
+ end
118
+
119
+ def etag=(val)
120
+ @etag = val
117
121
  end
118
122
 
119
123
  ##
120
124
  # Extract the last modified header for the resource
121
125
  def last_modified
122
- headers['Last-Modified']
126
+ @last_modified ||= headers['Last-Modified']
127
+ end
128
+
129
+ def last_modified=(val)
130
+ @last_modified = val
123
131
  end
124
132
 
125
133
  ##
@@ -1,3 +1,3 @@
1
1
  module Ldp
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -22,8 +22,8 @@ describe "Ldp::Client" do
22
22
  let(:conn_stubs) do
23
23
  stubs = Faraday::Adapter::Test::Stubs.new do |stub|
24
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 ]}
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
27
  stub.head('/a_binary_resource') { [200]}
28
28
  stub.get('/a_binary_resource') { [200, {}, ""]}
29
29
  stub.put("/a_resource") { [204]}
@@ -66,7 +66,7 @@ describe "Ldp::Client" do
66
66
  resp = subject.get "a_resource"
67
67
  expect(resp).to be_a_kind_of(Ldp::Response)
68
68
  expect(resp.body).to eq(simple_graph)
69
- expect(resp.resource?).to be_true
69
+ expect(resp.resource?).to be true
70
70
  end
71
71
 
72
72
  it "should accept a block to change the HTTP request" do
@@ -9,7 +9,7 @@ describe Ldp::Orm do
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#Resource>;rel=\"type\""}, simple_graph.dump(:ttl) ]}
13
13
  stub.head('/a_resource') { [200] }
14
14
  stub.put("/a_resource") { [204]}
15
15
  end
@@ -32,7 +32,7 @@ describe Ldp::Orm do
32
32
 
33
33
  describe "#delete" do
34
34
  it "should delete the LDP resource" do
35
- test_resource.should_receive(:delete)
35
+ expect(test_resource).to receive(:delete)
36
36
  subject.delete
37
37
  end
38
38
  end
@@ -53,13 +53,13 @@ describe Ldp::Orm do
53
53
 
54
54
  describe "#save" do
55
55
  it "should update the resource from the graph" do
56
- expect(subject.save).to be_true
56
+ expect(subject.save).to be true
57
57
  end
58
58
 
59
59
  it "should return false if the response was not successful" do
60
60
  conn_stubs.instance_variable_get(:@stack)[:put] = [] # erases the stubs for :put
61
61
  conn_stubs.put('/a_resource') {[412, nil, 'There was an error']}
62
- expect(subject.save).to be_false
62
+ expect(subject.save).to be false
63
63
  end
64
64
  end
65
65
 
@@ -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\"",
85
85
  "ETag" => "new-tag"},
86
86
  updated_graph.dump(:ttl)]}
87
87
  end
@@ -7,9 +7,9 @@ describe Ldp::Resource::RdfSource do
7
7
 
8
8
  let(:conn_stubs) do
9
9
  Faraday::Adapter::Test::Stubs.new do |stub|
10
- # stub.get('/a_resource') {[ 200, {"Link" => "http://www.w3.org/ns/ldp#Resource;rel=\"type\""}, simple_graph ]}
10
+ # stub.get('/a_resource') {[ 200, {"Link" => "<http://www.w3.org/ns/ldp#Resource>;rel=\"type\""}, simple_graph ]}
11
11
  stub.post("/") { [201]}
12
- stub.post("/abs_url_object") { [201]}
12
+ stub.put("/abs_url_object") { [201]}
13
13
  end
14
14
  end
15
15
 
@@ -35,7 +35,7 @@ describe Ldp::Resource::RdfSource do
35
35
 
36
36
  it "should allow absolute URLs to the LDP server" do
37
37
  obj = Ldp::Resource::RdfSource.new mock_client, "http://my.ldp.server/abs_url_object"
38
- obj.stub(new?: true)
38
+ allow(obj).to receive(:new?).and_return(true)
39
39
  created_resource = obj.create
40
40
  expect(created_resource).to be_kind_of Ldp::Resource::RdfSource
41
41
  end
@@ -58,13 +58,52 @@ describe Ldp::Resource do
58
58
  end
59
59
 
60
60
  describe "#create" do
61
- context "with initial content" do
62
- let(:path) { '/a_new_resource' }
63
- it "should post an RDF graph" do
64
- mock_client.should_receive(:post).with(path, "xyz").and_return(double(headers: {}))
65
- subject.content = "xyz"
66
- subject.save
61
+ let(:path) { '/a_new_resource' }
62
+ context "with a subject uri" do
63
+ let(:conn_stubs) do
64
+ Faraday::Adapter::Test::Stubs.new do |stub|
65
+ stub.head(path) { [404] }
66
+ stub.put(path) { [200, {'Last-Modified' => 'Tue, 22 Jul 2014 02:23:32 GMT' }] }
67
+ end
67
68
  end
69
+
70
+ context "and without a base path" do
71
+ it "should post an RDF graph" do
72
+ subject.content = "xyz"
73
+ subject.save
74
+ end
75
+ end
76
+
77
+ context "and with a base path" do
78
+ let(:base_path) { '/foo' }
79
+
80
+ subject { Ldp::Resource.new(mock_client, path, nil, base_path) }
81
+
82
+ it "should ignore the base path" do
83
+ subject.content = "xyz"
84
+ subject.save
85
+ end
86
+ end
87
+ end
88
+
89
+ context "without a subject" do
90
+ context "and with a base path" do
91
+ let(:base_path) { '/foo' }
92
+
93
+ let(:conn_stubs) do
94
+ Faraday::Adapter::Test::Stubs.new do |stub|
95
+ stub.post(base_path) { [200, {'Last-Modified' => 'Tue, 22 Jul 2014 02:23:32 GMT' }] }
96
+ end
97
+ end
98
+
99
+ subject { Ldp::Resource.new(mock_client, nil, nil, base_path) }
100
+
101
+ it "should post an RDF graph" do
102
+ subject.content = "xyz"
103
+ subject.save
104
+ end
105
+ end
106
+
68
107
  end
69
108
  end
70
109
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Ldp::Response do
4
- LDP_RESOURCE_HEADERS = { "Link" => Ldp.resource.to_s + ";rel=\"type\""}
4
+ LDP_RESOURCE_HEADERS = { "Link" => "<#{Ldp.resource.to_s}>;rel=\"type\""}
5
5
 
6
6
  let(:mock_response) { double(headers: {}, env: { url: "info:a" }) }
7
7
  let(:mock_client) { double(Ldp::Client) }
@@ -19,14 +19,14 @@ describe Ldp::Response do
19
19
 
20
20
  describe ".links" do
21
21
  it "should extract link headers with relations as a hash" do
22
- mock_response.stub(:headers => {
22
+ allow(mock_response).to receive(:headers).and_return(
23
23
  "Link" => [
24
- "xyz;rel=\"some-rel\"",
25
- "abc;rel=\"some-multi-rel\"",
26
- "123;rel=\"some-multi-rel\"",
27
- "vanilla-link"
24
+ "<xyz>;rel=\"some-rel\"",
25
+ "<abc>;rel=\"some-multi-rel\"",
26
+ "<123>;rel=\"some-multi-rel\"",
27
+ "<vanilla-link>"
28
28
  ]
29
- })
29
+ )
30
30
  h = Ldp::Response.links mock_response
31
31
 
32
32
  expect(h['some-rel']).to include("xyz")
@@ -35,7 +35,7 @@ describe Ldp::Response do
35
35
  end
36
36
 
37
37
  it "should return an empty hash if no link headers are availabe" do
38
- mock_response.stub(:headers => {})
38
+ allow(mock_response).to receive(:headers).and_return({})
39
39
  h = Ldp::Response.links mock_response
40
40
 
41
41
  expect(h).to be_empty
@@ -45,18 +45,19 @@ describe Ldp::Response do
45
45
 
46
46
  describe ".resource?" do
47
47
  it "should be a resource if a Link[rel=type] header asserts it is an ldp:resource" do
48
- mock_response.stub(:headers => {
48
+ allow(mock_response).to receive(:headers).and_return(
49
49
  "Link" => [
50
- "#{Ldp.resource};rel=\"type\""
50
+ "<#{Ldp.resource}>;rel=\"type\""
51
51
  ]
52
- })
53
- expect(Ldp::Response.resource? mock_response).to be_true
52
+ )
53
+ expect(Ldp::Response.resource? mock_response).to be_truthy
54
54
  end
55
55
  end
56
56
 
57
57
  describe "#graph" do
58
58
  it "should parse the response body for an RDF graph" do
59
- mock_response.stub :body => "<> <info:b> <info:c> .", :headers => LDP_RESOURCE_HEADERS
59
+ allow(mock_response).to receive(:body).and_return("<> <info:b> <info:c> .")
60
+ allow(mock_response).to receive(:headers).and_return(LDP_RESOURCE_HEADERS)
60
61
  graph = subject.graph
61
62
 
62
63
  expect(graph).to have_subject(RDF::URI.new("info:a"))
@@ -67,7 +68,7 @@ describe Ldp::Response do
67
68
 
68
69
  describe "#etag" do
69
70
  it "should pass through the response's ETag" do
70
- mock_response.stub :headers => { 'ETag' => 'xyz'}
71
+ allow(mock_response).to receive(:headers).and_return('ETag' => 'xyz')
71
72
 
72
73
  expect(subject.etag).to eq('xyz')
73
74
  end
@@ -75,7 +76,7 @@ describe Ldp::Response do
75
76
 
76
77
  describe "#last_modified" do
77
78
  it "should pass through the response's Last-Modified" do
78
- mock_response.stub :headers => { 'Last-Modified' => 'some-date'}
79
+ allow(mock_response).to receive(:headers).and_return('Last-Modified' => 'some-date')
79
80
  expect(subject.last_modified).to eq('some-date')
80
81
  end
81
82
  end
@@ -86,14 +87,16 @@ describe Ldp::Response do
86
87
 
87
88
  graph << [RDF::URI.new('info:a'), RDF.type, Ldp.page]
88
89
 
89
- mock_response.stub :body => graph.dump(:ttl), :headers => LDP_RESOURCE_HEADERS
90
+ allow(mock_response).to receive(:body).and_return(graph.dump(:ttl))
91
+ allow(mock_response).to receive(:headers).and_return(LDP_RESOURCE_HEADERS)
90
92
 
91
93
  expect(subject).to have_page
92
94
  end
93
95
 
94
96
  it "should be false otherwise" do
95
- subject.stub :page_subject => RDF::URI.new('info:a')
96
- mock_response.stub :body => '', :headers => LDP_RESOURCE_HEADERS
97
+ allow(subject).to receive(:page_subject).and_return RDF::URI.new('info:a')
98
+ allow(mock_response).to receive(:body).and_return('')
99
+ allow(mock_response).to receive(:headers).and_return(LDP_RESOURCE_HEADERS)
97
100
  expect(subject).not_to have_page
98
101
  end
99
102
  end
@@ -105,7 +108,8 @@ describe Ldp::Response do
105
108
  graph << [RDF::URI.new('info:a'), RDF.type, Ldp.page]
106
109
  graph << [RDF::URI.new('info:b'), RDF.type, Ldp.page]
107
110
 
108
- mock_response.stub :body => graph.dump(:ttl), :headers => LDP_RESOURCE_HEADERS
111
+ allow(mock_response).to receive(:body).and_return(graph.dump(:ttl))
112
+ allow(mock_response).to receive(:headers).and_return(LDP_RESOURCE_HEADERS)
109
113
 
110
114
  expect(subject.page.count).to eq(1)
111
115
 
@@ -114,8 +118,9 @@ describe Ldp::Response do
114
118
 
115
119
  describe "#subject" do
116
120
  it "should extract the HTTP request URI as an RDF URI" do
117
- mock_response.stub :body => '', :headers => LDP_RESOURCE_HEADERS
118
- mock_response.stub :env => { :url => 'http://xyz/a'}
121
+ allow(mock_response).to receive(:body).and_return('')
122
+ allow(mock_response).to receive(:headers).and_return(LDP_RESOURCE_HEADERS)
123
+ allow(mock_response).to receive(:env).and_return(:url => 'http://xyz/a')
119
124
  expect(subject.subject).to eq(RDF::URI.new("http://xyz/a"))
120
125
  end
121
126
  end
@@ -2,7 +2,6 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
 
4
4
 
5
- require 'rspec/autorun'
6
5
  require 'ldp'
7
6
  require 'faraday'
8
7
  require 'active_support/notifications'
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.5
4
+ version: 0.0.6
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-04-24 00:00:00.000000000 Z
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -146,7 +146,7 @@ files:
146
146
  - spec/lib/ldp/resource_spec.rb
147
147
  - spec/lib/ldp/response_spec.rb
148
148
  - spec/spec_helper.rb
149
- homepage: ''
149
+ homepage: https://github.com/cbeer/ldp
150
150
  licenses:
151
151
  - MIT
152
152
  metadata: {}