pjstadig-rubyrdf 0.0.1 → 0.0.2

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.
data/History.txt CHANGED
@@ -2,3 +2,9 @@
2
2
 
3
3
  * 1 major enhancement:
4
4
  * Initial release
5
+
6
+ == 0.0.2 2008-07-11
7
+
8
+ * 1 major enhancement:
9
+ * Added transaction support for sesame
10
+
data/Manifest.txt CHANGED
@@ -5,21 +5,31 @@ PostInstall.txt
5
5
  README.txt
6
6
  Rakefile
7
7
  TODO.txt
8
+ config/hoe.rb
9
+ config/requirements.rb
8
10
  lib/rdf.rb
9
11
  lib/rdf/blank_node.rb
10
12
  lib/rdf/error.rb
11
13
  lib/rdf/node.rb
12
14
  lib/rdf/plain_literal_node.rb
13
- lib/rdf/sesame/base.rb
15
+ lib/rdf/sesame.rb
14
16
  lib/rdf/sparql_result.rb
15
17
  lib/rdf/statement.rb
16
18
  lib/rdf/typed_literal_node.rb
17
19
  lib/rdf/uri_node.rb
18
20
  lib/rdf/version.rb
19
21
  lib/rubyrdf.rb
22
+ script/console
23
+ script/destroy
24
+ script/generate
25
+ script/txt2html
26
+ setup.rb
27
+ tasks/deployment.rake
28
+ tasks/environment.rake
29
+ tasks/gemspec.rake
20
30
  test/rdf/blank_node_test.rb
21
31
  test/rdf/plain_literal_node_test.rb
22
- test/rdf/sesame/base_test.rb
32
+ test/rdf/sesame_test.rb
23
33
  test/rdf/sparql_result_test.rb
24
34
  test/rdf/statement_test.rb
25
35
  test/rdf/typed_literal_node_test.rb
data/TODO.txt CHANGED
@@ -2,7 +2,6 @@ blank node validation
2
2
  uri node validation
3
3
  plain literal node validation
4
4
  typed literal node validation
5
- transactions
6
5
  to_node
7
6
  to_f
8
7
  to_i
@@ -11,3 +10,4 @@ to_s
11
10
  to_str
12
11
  to_date
13
12
  to_time
13
+ to_ruby
data/config/hoe.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'rdf/version'
2
+
3
+ AUTHOR = 'Paul Stadig' # can also be an array of Authors
4
+ EMAIL = "paul@stadig.name"
5
+ DESCRIPTION = "A Resource Description Framework (RDF) library for Ruby."
6
+ GEM_NAME = 'rubyrdf' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'rubyrdf' # The unix name for your project
8
+ HOMEPATH = "http://github.com/pjstadig/rubyrdf/wikis"
9
+ DOWNLOAD_PATH = "http://github.com/pjstadig/rubyrdf/tarball/master"
10
+ EXTRA_DEPENDENCIES = [
11
+ ['activesupport', '>= 1.3.1']
12
+ ] # An array of rubygem dependencies [name, version]
13
+
14
+ @config_file = "~/.rubyforge/user-config.yml"
15
+ @config = nil
16
+ RUBYFORGE_USERNAME = "unknown"
17
+ def rubyforge_username
18
+ unless @config
19
+ begin
20
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
21
+ rescue
22
+ puts <<-EOS
23
+ ERROR: No rubyforge config file found: #{@config_file}
24
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
25
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
26
+ EOS
27
+ exit
28
+ end
29
+ end
30
+ RUBYFORGE_USERNAME.replace @config["username"]
31
+ end
32
+
33
+
34
+ REV = nil
35
+ # UNCOMMENT IF REQUIRED:
36
+ # REV = YAML.load(`svn info`)['Revision']
37
+ VERS = RDF::VERSION::STRING + (REV ? ".#{REV}" : "")
38
+ RDOC_OPTS = ['--quiet', '--title', 'rubyrdf documentation',
39
+ "--opname", "index.html",
40
+ "--line-numbers",
41
+ "--main", "README",
42
+ "--inline-source"]
43
+
44
+ class Hoe
45
+ def extra_deps
46
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
47
+ @extra_deps
48
+ end
49
+ end
50
+
51
+ # Generate all the Rake tasks
52
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
53
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
54
+ p.developer(AUTHOR, EMAIL)
55
+ p.description = DESCRIPTION
56
+ p.summary = DESCRIPTION
57
+ p.url = HOMEPATH
58
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
59
+ p.test_globs = ["test/**/*_test.rb"]
60
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
61
+
62
+ # == Optional
63
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
64
+ p.extra_deps = EXTRA_DEPENDENCIES
65
+
66
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
67
+ end
68
+
69
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
72
+ $hoe.rsync_args = '-av --delete --ignore-errors'
73
+ $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
data/lib/rdf/sesame.rb ADDED
@@ -0,0 +1,187 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ module RDF
5
+ class Sesame
6
+ attr_reader :address, :port, :path, :repository
7
+
8
+ def initialize(uri, repository)
9
+ uri = URI.parse(uri)
10
+ @address = uri.host
11
+ @port = uri.port
12
+ @path = uri.path
13
+ @repository = repository
14
+ @transactions = []
15
+ end
16
+
17
+ def size
18
+ get_request(repo_path('size')).to_i
19
+ end
20
+
21
+ def empty?
22
+ size == 0
23
+ end
24
+
25
+ def add(*statement)
26
+ if @transactions.any?
27
+ @transactions.last << [:add, statement.to_statement]
28
+ else
29
+ post_request(repo_path('statements'), statement.to_statement.to_ntriples, {}, 'Content-Type' => 'text/plain')
30
+ end
31
+ end
32
+
33
+ def import(data, format = :ntriples)
34
+ headers = case format
35
+ when :ntriples
36
+ {'Content-Type' => 'text/plain; charset=utf-8'}
37
+ when :rdfxml
38
+ {'Content-Type' => 'application/rdf+xml; charset=utf-8'}
39
+ end
40
+
41
+ result = post_request(repo_path("statements"), data, {}, headers)
42
+ result
43
+ end
44
+
45
+ def delete(*statement)
46
+ if @transactions.any?
47
+ @transactions.last << [:delete, statement.to_statement]
48
+ else
49
+ delete_request(repo_path('statements'), to_param_hash(statement.to_statement))
50
+ end
51
+ end
52
+
53
+ def delete_all
54
+ if @transactions.any?
55
+ @transactions.last << [:delete_all, statement.to_statement]
56
+ else
57
+ delete_request(repo_path("statements"))
58
+ end
59
+ end
60
+
61
+ def select(query)
62
+ SparqlResult.new(get_request(repo_path, {"query" => query}, "Accept" => "application/sparql-results+xml"))
63
+ end
64
+
65
+ def ask(query)
66
+ get_request(repo_path, {"query" => query}, "Accept" => "text/boolean") == "true"
67
+ end
68
+
69
+ def include?(*statement)
70
+ ask("ASK { #{statement.to_statement.to_ntriples} }")
71
+ end
72
+
73
+ def transaction
74
+ @transactions << []
75
+ yield self
76
+ _commit(@transactions.pop)
77
+ rescue => e
78
+ @transactions.pop
79
+ throw e
80
+ end
81
+
82
+ def rollback
83
+ @transactions.pop
84
+ @transactions << []
85
+ true
86
+ end
87
+
88
+ def commit
89
+ _commit(@transactions.pop)
90
+ @transactions << []
91
+ true
92
+ end
93
+
94
+ private
95
+ def repo_path(path = nil)
96
+ parts = [@path, "repositories", @repository]
97
+ if path.to_s.strip != ""
98
+ parts << path
99
+ end
100
+
101
+ File.join(*parts)
102
+ end
103
+
104
+ def to_param_hash(statement)
105
+ {:subj => statement.subject.to_ntriples,
106
+ :pred => statement.predicate.to_ntriples,
107
+ :obj => statement.object.to_ntriples}
108
+ end
109
+
110
+ def get_request(path, params = {}, headers = {})
111
+ Net::HTTP.start(@address, @port) do |http|
112
+ http.get(
113
+ format_uri(path, params),
114
+ headers).body
115
+ end
116
+ end
117
+
118
+ def post_request(path, data, params = {}, headers = {})
119
+ Net::HTTP.start(@address, @port) do |http|
120
+ http.open_timeout = 6000
121
+ http.read_timeout = 6000
122
+ http.post(format_uri(path, params), data, headers).body
123
+ end
124
+ end
125
+
126
+ def delete_request(path, params = {}, headers = {})
127
+ Net::HTTP.start(@address, @port) do |http|
128
+ http.delete(format_uri(path, params), headers).body
129
+ end
130
+ end
131
+
132
+ def to_transaction_xml(transaction)
133
+ b = Builder::XmlMarkup.new
134
+ b.transaction do
135
+ transaction.each do |op|
136
+ if op[0] == :add
137
+ b.add do
138
+ transaction_xml_nodes(b, op[1])
139
+ end
140
+ elsif op[0] == :delete
141
+ b.remove do
142
+ transaction_xml_nodes(b, op[1])
143
+ end
144
+ elsif op[0] == :delete_all
145
+ b.clear
146
+ end
147
+ end
148
+ end
149
+ b.target!
150
+ end
151
+
152
+ def transaction_xml_nodes(b, stmt)
153
+ transaction_xml_node(b, stmt.subject)
154
+ transaction_xml_node(b, stmt.predicate)
155
+ transaction_xml_node(b, stmt.object)
156
+ end
157
+
158
+ def transaction_xml_node(b, node)
159
+ if node.is_a?(URINode)
160
+ b.uri(node.uri)
161
+ elsif node.is_a?(BlankNode)
162
+ b.bnode(node.name)
163
+ elsif node.is_a?(TypedLiteralNode)
164
+ b.literal(node.lexical_form, :datatype => node.datatype_uri)
165
+ elsif node.is_a?(PlainLiteralNode)
166
+ attrs = {}
167
+ if node.language_tag
168
+ attrs[:"xml:lang"] = node.language_tag
169
+ end
170
+
171
+ b.literal(node.lexical_form, attrs)
172
+ end
173
+ end
174
+
175
+ def _commit(transaction)
176
+ post_request(repo_path('statements'), to_transaction_xml(transaction), {}, 'Content-Type' => 'application/x-rdftransaction')
177
+ end
178
+
179
+ def format_uri(path, params = {})
180
+ if params.empty?
181
+ path
182
+ else
183
+ path + "?" + params.map{|k, v| "#{k}=#{URI.escape(v)}"}.join("&")
184
+ end
185
+ end
186
+ end
187
+ end
@@ -1,3 +1,5 @@
1
+ require 'rexml/document'
2
+
1
3
  module RDF
2
4
  class SparqlResult < Array
3
5
  class InvalidDocument < Error
data/lib/rdf/version.rb CHANGED
@@ -2,7 +2,7 @@ module RDF
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/lib/rubyrdf.rb CHANGED
@@ -14,4 +14,4 @@ require 'rdf/plain_literal_node'
14
14
  require 'rdf/typed_literal_node'
15
15
  require 'rdf/statement'
16
16
  require 'rdf/sparql_result'
17
- require 'rdf/sesame/base'
17
+ require 'rdf/sesame'
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/rubyrdf.rb'}"
9
+ puts "Loading rubyrdf gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/script/txt2html ADDED
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ GEM_NAME = 'rubyrdf' # what ppl will type to install your gem
4
+ RUBYFORGE_PROJECT = 'rubyrdf'
5
+
6
+ require 'rubygems'
7
+ begin
8
+ require 'newgem'
9
+ require 'rubyforge'
10
+ rescue LoadError
11
+ puts "\n\nGenerating the website requires the newgem RubyGem"
12
+ puts "Install: gem install newgem\n\n"
13
+ exit(1)
14
+ end
15
+ require 'redcloth'
16
+ require 'syntax/convertors/html'
17
+ require 'erb'
18
+ require File.dirname(__FILE__) + "/../lib/#{GEM_NAME}/version.rb"
19
+
20
+ version = Rubyrdf::VERSION::STRING
21
+ download = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
22
+
23
+ def rubyforge_project_id
24
+ RubyForge.new.autoconfig["group_ids"][RUBYFORGE_PROJECT]
25
+ end
26
+
27
+ class Fixnum
28
+ def ordinal
29
+ # teens
30
+ return 'th' if (10..19).include?(self % 100)
31
+ # others
32
+ case self % 10
33
+ when 1: return 'st'
34
+ when 2: return 'nd'
35
+ when 3: return 'rd'
36
+ else return 'th'
37
+ end
38
+ end
39
+ end
40
+
41
+ class Time
42
+ def pretty
43
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
44
+ end
45
+ end
46
+
47
+ def convert_syntax(syntax, source)
48
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
49
+ end
50
+
51
+ if ARGV.length >= 1
52
+ src, template = ARGV
53
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
54
+ else
55
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
56
+ exit!
57
+ end
58
+
59
+ template = ERB.new(File.open(template).read)
60
+
61
+ title = nil
62
+ body = nil
63
+ File.open(src) do |fsrc|
64
+ title_text = fsrc.readline
65
+ body_text_template = fsrc.read
66
+ body_text = ERB.new(body_text_template).result(binding)
67
+ syntax_items = []
68
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
69
+ ident = syntax_items.length
70
+ element, syntax, source = $1, $2, $3
71
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
72
+ "syntax-temp-#{ident}"
73
+ }
74
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
75
+ body = RedCloth.new(body_text).to_html
76
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
77
+ end
78
+ stat = File.stat(src)
79
+ created = stat.ctime
80
+ modified = stat.mtime
81
+
82
+ $stdout << template.result(binding)