Skip to content

Commit

Permalink
Finish 2.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Mar 15, 2017
2 parents 7c61176 + bf4ecae commit b9df327
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 34 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -84,7 +84,10 @@ operations on RDF files using available readers and writers.
* `serialize`: Parse an RDF input and re-serializing to [N-Triples][] or another available format using `--output-format` option.
* `subjects`: Returns unique subjects from parsed input.

The `serialize` command can also be used to serialize as a vocabulary
The `serialize` command can also be used to serialize as a vocabulary.

Different RDF gems will augment the `rdf` script with more capabilities, which may require specifying the appropriate `--input-format` option to revel.

## Examples

require 'rdf'
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
2.2.3
2.2.4
14 changes: 7 additions & 7 deletions lib/rdf/mixin/enumerable.rb
Expand Up @@ -122,7 +122,7 @@ def validate!
# @see #each_statement
# @see #enum_statement
def statements(**options)
Array(enum_statement)
enum_statement.to_a
end

##
Expand Down Expand Up @@ -311,7 +311,7 @@ def subjects(unique: true)
unless unique
enum_statement.map(&:subject) # TODO: optimize
else
Array(enum_subject)
enum_subject.to_a
end
end

Expand Down Expand Up @@ -376,7 +376,7 @@ def predicates(unique: true)
unless unique
enum_statement.map(&:predicate) # TODO: optimize
else
Array(enum_predicate)
enum_predicate.to_a
end
end

Expand Down Expand Up @@ -441,7 +441,7 @@ def objects(unique: true)
unless unique
enum_statement.map(&:object) # TODO: optimize
else
Array(enum_object)
enum_object.to_a
end
end

Expand Down Expand Up @@ -514,7 +514,7 @@ def terms(unique: true)
flatten.
compact
else
Array(enum_term)
enum_term.to_a
end
end

Expand Down Expand Up @@ -759,7 +759,7 @@ def dump(*args, **options)
# @overload #to_writer
# Implements #to_writer for each available instance of {RDF::Writer},
# based on the writer symbol.
#
#
# @return [String]
# @see {RDF::Writer.sym}
def method_missing(meth, *args)
Expand All @@ -780,7 +780,7 @@ def method_missing(meth, *args)
end

##
# @note this instantiates an writer; it could probably be done more
# @note this instantiates an writer; it could probably be done more
# efficiently by refactoring `RDF::Reader` and/or `RDF::Format` to expose
# a list of valid format symbols.
def respond_to_missing?(name, include_private = false)
Expand Down
2 changes: 1 addition & 1 deletion lib/rdf/model/literal/boolean.rb
Expand Up @@ -6,7 +6,7 @@ module RDF; class Literal
# @since 0.2.1
class Boolean < Literal
DATATYPE = RDF::XSD.boolean
GRAMMAR = /^(true|false|1|0)$/i.freeze
GRAMMAR = /^(true|false|1|0)$/.freeze
TRUES = %w(true 1).freeze
FALSES = %w(false 0).freeze

Expand Down
6 changes: 3 additions & 3 deletions lib/rdf/model/literal/double.rb
Expand Up @@ -12,7 +12,7 @@ module RDF; class Literal
# @since 0.2.1
class Double < Numeric
DATATYPE = RDF::XSD.double
GRAMMAR = /^(?:NaN|(?:[\+\-]?(?:INF|(?:\d+(\.\d*)?(e[\+\-]?\d+)?))))$/i.freeze
GRAMMAR = /^(?:NaN|\-?INF|[+\-]?(?:\d+(:?\.\d*)?|\.\d+)(?:[eE][\+\-]?\d+)?)$/.freeze

##
# @param [String, Float, #to_f] value
Expand All @@ -21,11 +21,11 @@ def initialize(value, datatype: nil, lexical: nil, **options)
@datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
@string = lexical || (value if value.is_a?(String))
@object = case
when value.is_a?(::String) then case value
when value.is_a?(::String) then case value.upcase
when '+INF' then 1/0.0
when 'INF' then 1/0.0
when '-INF' then -1/0.0
when 'NaN' then 0/0.0
when 'NAN' then 0/0.0
else Float(value.sub(/\.[eE]/, '.0E')) rescue nil
end
when value.is_a?(::Float) then value
Expand Down
31 changes: 25 additions & 6 deletions lib/rdf/reader.rb
Expand Up @@ -180,18 +180,37 @@ class << self
# @yieldreturn [void] ignored
# @raise [RDF::FormatError] if no reader found for the specified format
def self.open(filename, format: nil, **options, &block)
# If we're the abstract reader, and we can figure out a concrete reader from format and options, use that.
if self == RDF::Reader && reader = self.for(format || {file_name: filename}.merge(options))
return reader.open(filename, format: format, **options, &block)
end

# If we are a concrete reader class or format is not nil, set accept header from our content_types.
unless self == RDF::Reader
headers = (options[:headers] ||= {})
headers['Accept'] ||= (self.format.accept_type + %w(*/*;q=0.1)).join(", ")
end

Util::File.open_file(filename, options) do |file|
format_options = options.dup
format_options[:content_type] ||= file.content_type if file.respond_to?(:content_type)
format_options[:file_name] ||= filename
reader = if self == RDF::Reader
# We are the abstract reader class, find an appropriate reader
self.for(format || format_options) do
# Return a sample from the input file
sample = file.read(1000)
file.rewind
sample
end
else
# We are a concrete reader class
self
end

options[:encoding] ||= file.encoding if file.respond_to?(:encoding)
options[:filename] ||= filename
reader = self.for(format || format_options) do
# Return a sample from the input file
sample = file.read(1000)
file.rewind
sample
end

if reader
reader.new(file, options, &block)
else
Expand Down
2 changes: 1 addition & 1 deletion lib/rdf/util/file.rb
Expand Up @@ -305,7 +305,7 @@ def self.open_file(filename_or_url, proxy: nil, headers: {}, verify_none: false,
content_type = format ? format.content_type.first : 'text/plain'
# Open as a file, passing any options
begin
url_no_frag_or_query = RDF::URI(filename_or_url)
url_no_frag_or_query = RDF::URI(filename_or_url).dup
url_no_frag_or_query.query = nil
url_no_frag_or_query.fragment = nil
options[:encoding] ||= Encoding::UTF_8
Expand Down
8 changes: 8 additions & 0 deletions spec/mixin_enumerable_spec.rb
Expand Up @@ -79,6 +79,14 @@
end
end

context "Not using any deprecated method" do
%w(subjects predicates objects statements triples quads).each do |method|
it "##{method}" do
expect { subject.send(method.to_sym).to_a }.not_to output.to_stderr
end
end
end

context "Obtaining all unique values" do
%w(subjects predicates objects).each do |method|
it "##{method}" do
Expand Down
10 changes: 5 additions & 5 deletions spec/model_literal_spec.rb
Expand Up @@ -353,8 +353,8 @@ def self.literals(*selector)
%w(0 false)
]
it_behaves_like 'RDF::Literal validation', RDF::XSD.boolean,
%w(true false tRuE FaLsE 1 0),
%w(foo 10) + ['true false', 'true foo']
%w(true false 1 0),
%w(foo 10) + ['true false', 'true foo', 'tRuE' 'FaLsE']

context "object values" do
{
Expand Down Expand Up @@ -487,7 +487,10 @@ def self.literals(*selector)
%w(+INF INF),
%w(INF INF),
%w(-INF -INF),
%w(+INF INF),
#%w(NaN NaN),
#%w(NAN NaN),
%w(InF INF),
%w(3E1 3.0E1)
]
it_behaves_like 'RDF::Literal validation', RDF::XSD.double,
Expand All @@ -500,12 +503,9 @@ def self.literals(*selector)
1.0e+1
1.0e-10
123.456e4
+INF
INF
-INF
Inf
NaN
NAN
3E1
),
%w(foo 12.xyz 1.0ez) + ['1.1e1 foo', 'foo 1.1e1']
Expand Down
7 changes: 0 additions & 7 deletions spec/ntriples_spec.rb
Expand Up @@ -8,13 +8,6 @@

describe RDF::NTriples::Format do

# Restrict detected formats
before(:all) do
@formats = RDF::Format.class_variable_get(:@@subclasses)
RDF::Format.class_variable_set(:@@subclasses, [])
end
before(:all) {RDF::Format.class_variable_set(:@@subclasses, @formats)}

# @see lib/rdf/spec/format.rb in rdf-spec
it_behaves_like 'an RDF::Format' do
let(:format_class) { described_class }
Expand Down
60 changes: 60 additions & 0 deletions spec/reader_spec.rb
@@ -0,0 +1,60 @@
# -*- encoding: utf-8 -*-
require File.join(File.dirname(__FILE__), 'spec_helper')
require 'webmock/rspec'
require 'rdf/ntriples'
require 'rdf/nquads'
require 'rdf/turtle'

describe RDF::Reader do
describe ".each" do
it "enumerates formats having a reader" do
expect(described_class.each.to_a).to include(RDF::NTriples::Reader, RDF::NQuads::Reader, RDF::Turtle::Reader)
end

it "does not include RDF::Reader" do
expect(described_class.each.to_a & [RDF::Reader]).to be_empty
end
end

describe ".for" do
[
:ntriples,
'etc/doap.nt',
{file_name: 'etc/doap.nt'},
{file_extension: 'nt'},
{content_type: 'application/n-triples'},
{content_type: 'text/plain'},
].each do |arg|
it "discovers with #{arg.inspect}" do
expect(RDF::Reader.for(arg)).to eq RDF::NTriples::Reader
end
end
end

describe ".format" do
it "returns nil if given no format" do
expect(described_class.format).to be_nil
end

it "returns nil if given a format" do
expect(described_class.format(:ntriples)).to be_nil
end
end

describe ".open" do
it "sets Accept header for all readers" do
uri = "http://example/foo"
accept = (RDF::Format.accept_types + %w(*/*;q=0.1)).join(", ")
reader_mock = double("reader")
expect(reader_mock).to receive(:got_here)
WebMock.stub_request(:get, uri).with do |request|
expect(request.headers['Accept']).to eql accept
end.to_return(body: "foo")

described_class.open(uri) do |r|
expect(r).to be_a(described_class)
reader_mock.got_here
end
end
end
end
4 changes: 2 additions & 2 deletions spec/readme_spec.rb
Expand Up @@ -72,7 +72,7 @@
subject {
if example == :example0
expect(RDF::Util::File).to receive(:open_file).
with("http://ruby-rdf.github.com/rdf/etc/doap.nt", {}).
with("http://ruby-rdf.github.com/rdf/etc/doap.nt", hash_including(:headers)).
at_least(1).
and_yield(Kernel.open(File.expand_path("../../etc/doap.nt", __FILE__)))
end
Expand Down Expand Up @@ -109,7 +109,7 @@
subject {
if example == :example0
expect(RDF::Util::File).to receive(:open_file).
with("http://ruby-rdf.github.com/rdf/etc/doap.nq", {:base_uri=>"http://ruby-rdf.github.com/rdf/etc/doap.nq"}).
with("http://ruby-rdf.github.com/rdf/etc/doap.nq", hash_including(:headers, base_uri: "http://ruby-rdf.github.com/rdf/etc/doap.nq")).
at_least(1).
and_yield(Kernel.open(File.expand_path("../../etc/doap.nq", __FILE__)))
end
Expand Down
2 changes: 2 additions & 0 deletions spec/util_file_spec.rb
Expand Up @@ -13,6 +13,8 @@

it "returns Net::HTTP if rest-client is not available" do
hide_const("RestClient")
RDF::Util::File.remove_instance_variable(:@http_adapter)
RDF::Util::File.http_adapter
expect(RDF::Util::File.http_adapter).to eq RDF::Util::File::NetHttpAdapter
end

Expand Down
20 changes: 20 additions & 0 deletions spec/writer_spec.rb
@@ -0,0 +1,20 @@
# -*- encoding: utf-8 -*-
require File.join(File.dirname(__FILE__), 'spec_helper')
require 'rdf/ntriples'

describe RDF::Writer do
describe ".for" do
[
:ntriples,
'etc/doap.nt',
{file_name: 'etc/doap.nt'},
{file_extension: 'nt'},
{content_type: 'application/n-triples'},
{content_type: 'text/plain'},
].each do |arg|
it "discovers with #{arg.inspect}" do
expect(RDF::Writer.for(arg)).to eq RDF::NTriples::Writer
end
end
end
end

0 comments on commit b9df327

Please sign in to comment.