Skip to content
Browse files

Initial release.

  • Loading branch information...
0 parents commit e47c763f430c548cce6ca72b22a7ea8c56beb5d2 @gkellogg committed
Showing with 498 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +12 −0 .yardopts
  3. +1 −0 AUTHORS
  4. +2 −0 History.md
  5. +1 −0 README
  6. +87 −0 README.md
  7. +25 −0 Rakefile
  8. +24 −0 UNLICENSE
  9. +1 −0 VERSION
  10. +37 −0 json-ld.gemspec
  11. +31 −0 lib/json/ld.rb
  12. +26 −0 lib/json/ld/format.rb
  13. +11 −0 lib/json/ld/reader.rb
  14. +18 −0 lib/json/ld/version.rb
  15. +11 −0 lib/json/ld/writer.rb
  16. +6 −0 spec/format_spec.rb
  17. +130 −0 spec/matchers.rb
  18. +6 −0 spec/reader_spec.rb
  19. +1 −0 spec/spec.opts
  20. +60 −0 spec/spec_helper.rb
  21. +6 −0 spec/writer_spec.rb
2 .gitignore
@@ -0,0 +1,2 @@
+/.yardoc/
+/doc/
12 .yardopts
@@ -0,0 +1,12 @@
+--title "JSON-LD reader/writer for RDF.rb."
+--output-dir doc/yard
+--protected
+--no-private
+--hide-void-return
+--markup markdown
+--readme README.md
+-
+History.md
+AUTHORS
+VERSION
+UNLICENSE
1 AUTHORS
@@ -0,0 +1 @@
+* Gregg Kellogg <gregg@kellogg-assoc.com>
2 History.md
@@ -0,0 +1,2 @@
+=== 0.0.1
+* First release of project scaffold.
1 README
87 README.md
@@ -0,0 +1,87 @@
+JSON-LD reader/writer
+==================================
+
+[JSON-LD][] reader/writer for [RDF.rb][RDF.rb] .
+
+Features
+--------
+
+JSON::LD parses and serializes [JSON-LD][] into statements or triples.
+
+Install with `gem install json-ld`
+
+Examples
+--------
+
+ require 'rubygems'
+ require 'json/ld'
+
+Documentation
+-------------
+
+<http://rdf.rubyforge.org/json-ld>
+
+* {JSON::LD}
+
+Dependencies
+------------
+
+* [Ruby](http://ruby-lang.org/) (>= 1.8.7) or (>= 1.8.1 with [Backports][])
+* [RDF.rb](http://rubygems.org/gems/rdf) (>= 0.3.3)
+* [JSON](https://rubygems.org/gems/json) (>= 1.5.1)
+
+Installation
+------------
+
+The recommended installation method is via [RubyGems](http://rubygems.org/).
+To install the latest official release of the `JSON-LD` gem, do:
+
+ % [sudo] gem install json-ld
+
+Download
+--------
+
+To get a local working copy of the development repository, do:
+
+ % git clone git://github.com/gkellogg/json-ld.git
+
+Mailing List
+------------
+
+* <http://lists.w3.org/Archives/Public/public-rdf-ruby/>
+
+Author
+------
+
+* [Gregg Kellogg](http://github.com/gkellogg) - <http://kellogg-assoc.com/>
+
+
+Contributing
+------------
+
+* Do your best to adhere to the existing coding conventions and idioms.
+* Don't use hard tabs, and don't leave trailing whitespace on any line.
+* Do document every method you add using [YARD][] annotations. Read the
+ [tutorial][YARD-GS] or just look at the existing code for examples.
+* Don't touch the `.gemspec`, `VERSION` or `AUTHORS` files. If you need to
+ change them, do so on your private branch only.
+* Do feel free to add yourself to the `CREDITS` file and the corresponding
+ list in the the `README`. Alphabetical order applies.
+* Do note that in order for us to merge any non-trivial changes (as a rule
+ of thumb, additions larger than about 15 lines of code), we need an
+ explicit [public domain dedication][PDD] on record from you.
+
+License
+-------
+
+This is free and unencumbered public domain software. For more information,
+see <http://unlicense.org/> or the accompanying {file:UNLICENSE} file.
+
+[Ruby]: http://ruby-lang.org/
+[RDF]: http://www.w3.org/RDF/
+[YARD]: http://yardoc.org/
+[YARD-GS]: http://rubydoc.info/docs/yard/file/docs/GettingStarted.md
+[PDD]: http://lists.w3.org/Archives/Public/public-rdf-ruby/2010May/0013.html
+[RDF.rb]: http://rdf.rubyforge.org/
+[Backports]: http://rubygems.org/gems/backports
+[JSON-LD]: http://json-ld.org/spec/ED/20110507/
25 Rakefile
@@ -0,0 +1,25 @@
+require 'rubygems'
+
+task :default => [ :spec ]
+
+require 'rspec/core/rake_task'
+desc 'Run specifications'
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.rspec_opts = %w(--options spec/spec.opts) if File.exists?('spec/spec.opts')
+end
+
+desc "Run specs through RCov"
+RSpec::Core::RakeTask.new("spec:rcov") do |spec|
+ spec.rcov = true
+ spec.rcov_opts = %q[--exclude "spec"]
+end
+
+desc "Generate HTML report specs"
+RSpec::Core::RakeTask.new("doc:spec") do |spec|
+ spec.rspec_opts = ["--format", "html", "-o", "doc/spec.html"]
+end
+
+require 'yard'
+YARD::Rake::YardocTask.new
+
+task :default => :spec
24 UNLICENSE
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org/>
1 VERSION
@@ -0,0 +1 @@
+0.0.1
37 json-ld.gemspec
@@ -0,0 +1,37 @@
+#!/usr/bin/env ruby -rubygems
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ gem.version = File.read('VERSION').chomp
+ gem.date = File.mtime('VERSION').strftime('%Y-%m-%d')
+
+ gem.name = "json-ld"
+ gem.homepage = "http://rdf.rubyforge.org/json-ld"
+ gem.license = 'Public Domain' if gem.respond_to?(:license=)
+ gem.summary = "JSON-LD reader/writer for Ruby."
+ gem.description = gem.summary
+ gem.rubyforge_project = 'json-ld'
+
+ gem.authors = ['Gregg Kellogg']
+ gem.email = 'public-rdf-ruby@w3.org'
+
+ gem.platform = Gem::Platform::RUBY
+ gem.files = %w(AUTHORS CREDITS README UNLICENSE VERSION) + Dir.glob('lib/**/*.rb')
+ gem.bindir = %q(bin)
+ gem.executables = %w(json_ld)
+ gem.default_executable = gem.executables.first
+ gem.require_paths = %w(lib)
+ gem.extensions = %w()
+ gem.test_files = %w()
+ gem.has_rdoc = false
+
+ gem.required_ruby_version = '>= 1.8.1'
+ gem.requirements = []
+ gem.add_runtime_dependency 'rdf', '~> 0.4.0'
+ gem.add_runtime_dependency 'json', '>= 1.5.1'
+ gem.add_development_dependency 'nokogiri' , '>= 1.4.4'
+ gem.add_development_dependency 'yard' , '>= 0.6.0'
+ gem.add_development_dependency 'rspec', '>= 2.5.0'
+ gem.add_development_dependency 'rdf-spec', '~> 0.4.0'
+ gem.post_install_message = nil
+end
31 lib/json/ld.rb
@@ -0,0 +1,31 @@
+$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..')))
+require 'rdf'
+
+module JSON
+ ##
+ # **`JSON::LD`** is a JSON-LD plugin for RDF.rb.
+ #
+ # @example Requiring the `JSON::LD` module
+ # require 'json/ld'
+ #
+ # @example Parsing RDF statements from a JSON-LD file
+ # JSON::LD::Reader.open("etc/foaf.jld") do |reader|
+ # reader.each_statement do |statement|
+ # puts statement.inspect
+ # end
+ # end
+ #
+ # @see http://rdf.rubyforge.org/
+ # @see http://www.w3.org/TR/REC-rdf-syntax/
+ #
+ # @author [Gregg Kellogg](http://greggkellogg.net/)
+ module LD
+ require 'json/ld/format'
+ autoload :Reader, 'json/ld/reader'
+ autoload :VERSION, 'json/ld/version'
+ autoload :Writer, 'json/ld/writer'
+
+ def self.debug?; @debug; end
+ def self.debug=(value); @debug = value; end
+ end
+end
26 lib/json/ld/format.rb
@@ -0,0 +1,26 @@
+module RDF::N3
+ ##
+ # RDFa format specification.
+ #
+ # @example Obtaining an Notation3 format class
+ # RDF::Format.for(:jld) #=> JSON::LD::Format
+ # RDF::Format.for("etc/foaf.jld")
+ # RDF::Format.for(:file_name => "etc/foaf.jld")
+ # RDF::Format.for(:file_extension => "jld")
+ # RDF::Format.for(:content_type => "application/json")
+ #
+ # @example Obtaining serialization format MIME types
+ # RDF::Format.content_types #=> {"application/json" => [JSON::LD::Format]}
+ #
+ # @example Obtaining serialization format file extension mappings
+ # RDF::Format.file_extensions #=> {:ttl => "application/json"}
+ #
+ # @see http://www.w3.org/TR/rdf-testcases/#ntriples
+ class Format < RDF::Format
+ content_type 'application/json', :extension => :jld
+ content_encoding 'utf-8'
+
+ reader { RDF::N3::Reader }
+ writer { RDF::N3::Writer }
+ end
+end
11 lib/json/ld/reader.rb
@@ -0,0 +1,11 @@
+module JSON::LD
+ ##
+ # A JSON-LD parser in Ruby.
+ #
+ # @see http://json-ld.org/spec/ED/20110507/
+ # @author [Gregg Kellogg](http://greggkellogg.net/)
+ class Reader < RDF::Reader
+ format Format
+ end
+end
+
18 lib/json/ld/version.rb
@@ -0,0 +1,18 @@
+module RDF::N3::VERSION
+ VERSION_FILE = File.join(File.expand_path(File.dirname(__FILE__)), "..", "..", "..", "VERSION")
+ MAJOR, MINOR, TINY, EXTRA = File.read(VERSION_FILE).chop.split(".")
+
+ STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.')
+
+ ##
+ # @return [String]
+ def self.to_s() STRING end
+
+ ##
+ # @return [String]
+ def self.to_str() STRING end
+
+ ##
+ # @return [Array(Integer, Integer, Integer)]
+ def self.to_a() STRING.split(".") end
+end
11 lib/json/ld/writer.rb
@@ -0,0 +1,11 @@
+module JSON::LD
+ ##
+ # A JSON-LD parser in Ruby.
+ #
+ # @see http://json-ld.org/spec/ED/20110507/
+ # @author [Gregg Kellogg](http://greggkellogg.net/)
+ class Writer < RDF::Writer
+ format Format
+ end
+end
+
6 spec/format_spec.rb
@@ -0,0 +1,6 @@
+# coding: utf-8
+$:.unshift "."
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe "JSON::LD::Format" do
+end
130 spec/matchers.rb
@@ -0,0 +1,130 @@
+require 'rdf/isomorphic'
+
+module Matchers
+ class BeEquivalentGraph
+ Info = Struct.new(:about, :information, :trace, :compare, :inputDocument, :outputDocument)
+ def normalize(graph)
+ case @info.compare
+ when :array
+ array = case graph
+ when RDF::Graph
+ raise ":compare => :array used with Graph"
+ when Array
+ graph.sort
+ else
+ graph.to_s.split("\n").
+ map {|t| t.gsub(/^\s*(.*)\s*$/, '\1')}.
+ reject {|t2| t2.match(/^\s*$/)}.
+ compact.
+ sort.
+ uniq
+ end
+
+ # Implement to_ntriples on array, to simplify logic later
+ def array.to_ntriples; self.join("\n") + "\n"; end
+ array
+ else
+ case graph
+ when RDF::Graph then graph
+ when IO, StringIO
+ RDF::Graph.new.load(graph, :base_uri => @info.about)
+ else
+ # Figure out which parser to use
+ g = RDF::Graph.new
+ reader_class = RDF::Reader.for(detect_format(graph))
+ reader_class.new(graph, :base_uri => @info.about).each {|s| g << s}
+ g
+ end
+ end
+ end
+
+ def initialize(expected, info)
+ @info = if info.respond_to?(:about)
+ info
+ elsif info.is_a?(Hash)
+ identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.context : info[:about]
+ trace = info[:trace]
+ trace = trace.join("\n") if trace.is_a?(Array)
+ Info.new(identifier, info[:information] || "", trace, info[:compare])
+ else
+ Info.new(expected.is_a?(RDF::Graph) ? expected.context : info, info.to_s)
+ end
+ @expected = normalize(expected)
+ end
+
+ def matches?(actual)
+ @actual = normalize(actual)
+ if @info.compare == :array
+ @actual == @expected
+ else
+ @actual.isomorphic_with?(@expected)
+ end
+ end
+
+ def failure_message_for_should
+ info = @info.respond_to?(:information) ? @info.information : @info.inspect
+ if @expected.is_a?(RDF::Graph) && @actual.size != @expected.size
+ "Graph entry count differs:\nexpected: #{@expected.size}\nactual: #{@actual.size}"
+ elsif @expected.is_a?(Array) && @actual.size != @expected.length
+ "Graph entry count differs:\nexpected: #{@expected.length}\nactual: #{@actual.size}"
+ else
+ "Graph differs"
+ end +
+ "\n#{info + "\n" unless info.empty?}" +
+ (@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
+ (@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
+ "Unsorted Expected:\n#{@expected.to_ntriples}" +
+ "Unsorted Results:\n#{@actual.to_ntriples}" +
+# "Unsorted Expected Dump:\n#{@expected.dump}\n" +
+# "Unsorted Results Dump:\n#{@actual.dump}" +
+ (@info.trace ? "\nDebug:\n#{@info.trace}" : "")
+ end
+ def negative_failure_message
+ "Graphs do not differ\n"
+ end
+ end
+
+ def be_equivalent_graph(expected, info = nil)
+ BeEquivalentGraph.new(expected, info)
+ end
+
+ class MatchRE
+ Info = Struct.new(:about, :information, :trace, :inputDocument, :outputDocument)
+ def initialize(expected, info)
+ @info = if info.respond_to?(:about)
+ info
+ elsif info.is_a?(Hash)
+ identifier = info[:identifier] || info[:about]
+ trace = info[:trace]
+ trace = trace.join("\n") if trace.is_a?(Array)
+ Info.new(identifier, info[:information] || "", trace, info[:inputDocument], info[:outputDocument])
+ else
+ Info.new(info, info.to_s)
+ end
+ @expected = expected
+ end
+
+ def matches?(actual)
+ @actual = actual
+ @actual.to_s.match(@expected)
+ end
+
+ def failure_message_for_should
+ info = @info.respond_to?(:information) ? @info.information : @info.inspect
+ "Match failed"
+ "\n#{info + "\n" unless info.empty?}" +
+ (@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
+ (@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
+ "Expression: #{@expected}\n" +
+ "Unsorted Results:\n#{@actual}" +
+ (@info.trace ? "\nDebug:\n#{@info.trace}" : "")
+ end
+ def negative_failure_message
+ "Match succeeded\n"
+ end
+ end
+
+ def match_re(expected, info = nil)
+ MatchRE.new(expected, info)
+ end
+end
6 spec/reader_spec.rb
@@ -0,0 +1,6 @@
+# coding: utf-8
+$:.unshift "."
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe "JSON::LD::Reader" do
+end
1 spec/spec.opts
@@ -0,0 +1 @@
+--colour
60 spec/spec_helper.rb
@@ -0,0 +1,60 @@
+$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$:.unshift File.dirname(__FILE__)
+
+require 'rubygems'
+require 'rspec'
+require 'matchers'
+require 'bigdecimal' # XXX Remove Me
+require 'json/ld'
+require 'rdf/ntriples'
+require 'rdf/spec'
+require 'rdf/spec/matchers'
+require 'rdf/isomorphic'
+
+include Matchers
+
+module RDF
+ module Isomorphic
+ alias_method :==, :isomorphic_with?
+ end
+ class Graph
+ def to_ntriples
+ RDF::Writer.for(:ntriples).buffer do |writer|
+ self.each_statement do |statement|
+ writer << statement
+ end
+ end
+ end
+ def dump
+ b = []
+ self.each_statement do |statement|
+ b << statement.to_triple.inspect
+ end
+ b.join("\n")
+ end
+ end
+end
+
+::RSpec.configure do |c|
+ c.filter_run :focus => true
+ c.run_all_when_everything_filtered = true
+ c.exclusion_filter = {
+ :ruby => lambda { |version| !(RUBY_VERSION.to_s =~ /^#{version.to_s}/) },
+ }
+ c.include(Matchers)
+ c.include(RDF::Spec::Matchers)
+end
+
+# Serialize graph and replace bnodes with predictable versions, return as sorted array
+def normalize_bnodes(graph, anon = "a")
+ anon_ctx = {}
+ # Find and replace all BNodes within graph string
+ g_str = graph.to_ntriples
+ anon_entries = g_str.scan(/_:g\d+/).sort.uniq
+ anon_entries.each do |a|
+ anon_ctx[a] = "_:#{anon}"
+ anon = anon.succ
+ end
+
+ g_str.gsub(/_:g\d+/) { |bn| anon_ctx[bn] }.split("\n").sort
+end
6 spec/writer_spec.rb
@@ -0,0 +1,6 @@
+# coding: utf-8
+$:.unshift "."
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe "JSON::LD::Writer" do
+end

0 comments on commit e47c763

Please sign in to comment.
Something went wrong with that request. Please try again.