ldp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63183c29706de20d216c7ac0701f1c487ac198a1
4
+ data.tar.gz: e08f80b008698907385b8060d4d2dcaed42ede20
5
+ SHA512:
6
+ metadata.gz: c7ff75b126b3fcf65c11c3a2a5265551f6bb9ea579c9775038a6b5a2decf64799ff8a898473c96be70232671526ffcabf278538dca1b49a98b5246c31ac4c44e
7
+ data.tar.gz: ecd3068a5a86397768f428e142be9832b587f5474629fb2c0c68a3364128ea1a9b67fb15471323f0ddaba2e0c09ada6f00cc898670481f8ab6a393d914902a41
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ldp-client.gemspec
4
+ gemspec
5
+
6
+ gem "byebug"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris Beer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Ldp::Client
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ldp-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ldp-client
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/ldp.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ldp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ldp"
8
+ spec.version = Ldp::VERSION
9
+ spec.authors = ["Chris Beer"]
10
+ spec.email = ["chris@cbeer.info"]
11
+ spec.description = %q{Linked Data Platform client library}
12
+ spec.summary = spec.description
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^spec/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "faraday"
22
+ spec.add_dependency "linkeddata"
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
data/lib/ldp.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'ldp/version'
2
+
3
+ module Ldp
4
+ require 'ldp/client'
5
+
6
+ def self.resource
7
+ RDF::URI.new("http://www.w3.org/ns/ldp/Resource")
8
+ end
9
+
10
+ def self.container
11
+ RDF::URI.new("http://www.w3.org/ns/ldp/Container")
12
+ end
13
+
14
+ def self.inlinedResource
15
+ RDF::URI.new("http://www.w3.org/ns/ldp/inlinedResource")
16
+ end
17
+
18
+ autoload :Response, 'ldp/response'
19
+ autoload :Resource, 'ldp/resource'
20
+ autoload :Container, 'ldp/container'
21
+ end
data/lib/ldp/client.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'faraday'
2
+ require 'linkeddata'
3
+
4
+ module Ldp
5
+ class Client
6
+
7
+ attr_reader :http
8
+
9
+ def initialize *http_client
10
+ if http_client.length == 1 and http_client.first.is_a? Faraday::Connection
11
+ @http = http_client.first
12
+ else
13
+ @http = Faraday.new *http_client
14
+ end
15
+ end
16
+
17
+ def find_or_initialize subject
18
+ data = get(subject)
19
+
20
+ if !data.is_a? Response
21
+ raise "#{subject} is not an LDP Resource"
22
+ end
23
+
24
+ if data.container?
25
+ Container.new self, subject, data
26
+ else
27
+ Resource.new self, subject, data
28
+ end
29
+ end
30
+
31
+ def get url
32
+ resp = http.get do |req|
33
+ req.url url
34
+ yield req if block_given?
35
+ end
36
+
37
+ if Response.resource? resp
38
+ Response.wrap self, resp
39
+ else
40
+ resp
41
+ end
42
+ end
43
+
44
+ def delete url
45
+ http.delete do |req|
46
+ req.url url
47
+ yield req if block_given?
48
+ end
49
+ end
50
+
51
+ def post url, body
52
+ http.post do |req|
53
+ req.url url
54
+ req.headers['Content-Type'] = 'text/turtle'
55
+ req.body = body
56
+ yield req if block_given?
57
+ end
58
+ end
59
+
60
+ def put url, body
61
+ http.put do |req|
62
+ req.url url
63
+ req.headers['Content-Type'] = 'text/turtle'
64
+ req.body = body
65
+ yield req if block_given?
66
+ end
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,7 @@
1
+ module Ldp
2
+ class Container < Resource
3
+ def add graph
4
+ client.post subject, graph.dump(:ttl)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,58 @@
1
+ module Ldp
2
+ class Resource
3
+ attr_reader :client, :subject
4
+
5
+ def initialize client, subject, data = nil
6
+ @client = client
7
+ @subject = subject
8
+ @get = data
9
+ end
10
+
11
+ def graph
12
+ @graph ||= begin
13
+ original_graph = get.graph
14
+
15
+ inlinedResources = get.graph.query(:predicate => Ldp.inlinedResource).map { |x| x.object }
16
+
17
+ unless inlinedResource.empty?
18
+ new_graph = RDF::Graph.new
19
+
20
+ original_graph.each_statement do |s|
21
+ unless inlinedResource.include? s.subject
22
+ new_graph << s
23
+ end
24
+ end
25
+
26
+ new_graph
27
+ else
28
+ original_graph
29
+ end
30
+ end
31
+ end
32
+
33
+ def get
34
+ @get ||= client.get subject
35
+ end
36
+
37
+ def delete
38
+ client.delete subject do |req|
39
+ if @get
40
+ req.headers['If-Match'] << get.headers['ETag']
41
+ end
42
+ end
43
+
44
+ @get = nil
45
+ @graph = nil
46
+ end
47
+
48
+ def update
49
+ client.put subject, graph.dump(:ttl) do |req|
50
+ if @get
51
+ req.headers['If-Match'] << get.headers['ETag']
52
+ end
53
+ end
54
+ @get = nil
55
+ @graph = nil
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,96 @@
1
+ module Ldp
2
+ module Response
3
+ def self.wrap client, raw_resp
4
+ raw_resp.send(:extend, Ldp::Response)
5
+ raw_resp.ldp_client = client
6
+ raw_resp
7
+ end
8
+
9
+ def self.links raw_resp
10
+ Array(raw_resp.headers["Link"]).inject({}) do |memo, header|
11
+ v = header.scan(/(.*);\s?rel="([^"]+)"/)
12
+
13
+ if v.length == 1
14
+ memo[v.first.last] ||= []
15
+ memo[v.first.last] << v.first.first
16
+ end
17
+
18
+ memo
19
+ end
20
+ end
21
+
22
+ def self.resource? raw_resp
23
+ links(raw_resp).fetch("type", []).include? Ldp.resource
24
+ end
25
+
26
+ def ldp_client= client
27
+ @ldp_client = client
28
+ end
29
+
30
+ def ldp_client
31
+ @ldp_client
32
+ end
33
+
34
+ def graph
35
+ @graph ||= begin
36
+ graph = RDF::Graph.new
37
+
38
+ RDF::Reader.for(:ttl).new(StringIO.new(body), :base_uri => subject) do |reader|
39
+ reader.each_statement do |s|
40
+ graph << s
41
+ end
42
+ end
43
+
44
+ graph
45
+ end
46
+ end
47
+
48
+ def etag
49
+
50
+ end
51
+
52
+ def last_modified
53
+
54
+ end
55
+
56
+ def page
57
+
58
+ end
59
+
60
+ def paginated?
61
+
62
+ end
63
+
64
+ def next_page
65
+
66
+ end
67
+
68
+ def first_page
69
+
70
+ end
71
+
72
+ def resources
73
+
74
+ end
75
+
76
+ def members
77
+
78
+ end
79
+
80
+ def resource?
81
+ Ldp::Response.resource?(self)
82
+ end
83
+
84
+ def container?
85
+ graph.has_statement? RDF::Statement.new(subject, RDF.type, Ldp.container)
86
+ end
87
+
88
+ def sort
89
+
90
+ end
91
+
92
+ def subject
93
+ RDF::URI.new env[:url]
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,3 @@
1
+ module Ldp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ describe "Ldp::Client" do
3
+
4
+ let(:simple_graph) do
5
+ graph = RDF::Graph.new << [RDF::URI.new(""), RDF::DC.title, "Hello, world!"]
6
+ graph.dump(:ttl)
7
+ end
8
+
9
+ let(:simple_container_graph) do
10
+ graph = RDF::Graph.new << [RDF::URI.new(""), RDF.type, Ldp.container]
11
+ graph.dump(:ttl)
12
+ end
13
+
14
+ let(:conn_stubs) do
15
+ stubs = Faraday::Adapter::Test::Stubs.new do |stub|
16
+ stub.get('/a_resource') {[ 200, {"Link" => "http://www.w3.org/ns/ldp/Resource;rel=\"type\""}, simple_graph ]}
17
+ stub.get('/a_container') {[ 200, {"Link" => "http://www.w3.org/ns/ldp/Resource;rel=\"type\""}, simple_container_graph ]}
18
+
19
+ end
20
+ end
21
+
22
+ let(:mock_conn) do
23
+ test = Faraday.new do |builder|
24
+ builder.adapter :test, conn_stubs do |stub|
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ subject do
31
+ Ldp::Client.new mock_conn
32
+ end
33
+
34
+ describe "initialize" do
35
+ it "should accept an existing Faraday connection" do
36
+ conn = Faraday.new "http://example.com"
37
+ client = Ldp::Client.new conn
38
+ expect(client.http).to eq(conn)
39
+ end
40
+
41
+ it "should create a connection from Faraday constructor params" do
42
+ client = Ldp::Client.new "http://example.com"
43
+ expect(client.http.host).to eq("example.com")
44
+ end
45
+ end
46
+
47
+ describe "get" do
48
+
49
+ it "should GET content from the HTTP endpoint" do
50
+ resp = subject.get "a_resource"
51
+ expect(resp).to be_a_kind_of(Ldp::Response)
52
+ expect(resp.body).to eq(simple_graph)
53
+ expect(resp.resource?).to be_true
54
+ end
55
+
56
+ describe "response" do
57
+
58
+ end
59
+ end
60
+
61
+ describe "find_or_initialize" do
62
+ it "should be a resource" do
63
+ resource = subject.find_or_initialize "a_resource"
64
+ expect(resource).to be_a_kind_of(Ldp::Resource)
65
+ end
66
+
67
+ it "should be a container" do
68
+ resource = subject.find_or_initialize "a_container"
69
+ expect(resource).to be_a_kind_of(Ldp::Resource)
70
+ expect(resource).to be_a_kind_of(Ldp::Container)
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+
5
+ require 'rspec/autorun'
6
+ require 'ldp'
7
+ require 'faraday'
8
+ require "byebug"
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ldp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Beer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: linkeddata
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Linked Data Platform client library
84
+ email:
85
+ - chris@cbeer.info
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - ldp.gemspec
96
+ - lib/ldp.rb
97
+ - lib/ldp/client.rb
98
+ - lib/ldp/container.rb
99
+ - lib/ldp/resource.rb
100
+ - lib/ldp/response.rb
101
+ - lib/ldp/version.rb
102
+ - spec/lib/ldp/client_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.0.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Linked Data Platform client library
128
+ test_files:
129
+ - spec/lib/ldp/client_spec.rb
130
+ - spec/spec_helper.rb