Skip to content

Commit 0f59730

Browse files
committed
When testing, use local path for RDF file loads if it exists.
1 parent 4f0b002 commit 0f59730

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

spec/suite_helper.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,70 @@
11
require 'json/ld'
22
require 'support/extensions'
33

4+
5+
# For now, override RDF::Utils::File.open_file to look for the file locally before attempting to retrieve it
6+
module RDF::Util
7+
module File
8+
REMOTE_PATH = "http://json-ld.org/test-suite/"
9+
LOCAL_PATH = ::File.expand_path("../json-ld.org/test-suite", __FILE__) + '/'
10+
11+
class << self
12+
alias_method :original_open_file, :open_file
13+
end
14+
15+
##
16+
# Override to use Patron for http and https, Kernel.open otherwise.
17+
#
18+
# @param [String] filename_or_url to open
19+
# @param [Hash{Symbol => Object}] options
20+
# @option options [Array, String] :headers
21+
# HTTP Request headers.
22+
# @return [IO] File stream
23+
# @yield [IO] File stream
24+
def self.open_file(filename_or_url, options = {}, &block)
25+
case
26+
when filename_or_url.to_s =~ /^file:/
27+
path = filename_or_url[5..-1]
28+
Kernel.open(path.to_s, options, &block)
29+
when (filename_or_url.to_s =~ %r{^#{REMOTE_PATH}} && Dir.exist?(LOCAL_PATH))
30+
#puts "attempt to open #{filename_or_url} locally"
31+
localpath = filename_or_url.to_s.sub(REMOTE_PATH, LOCAL_PATH)
32+
response = begin
33+
::File.open(localpath)
34+
rescue Errno::ENOENT => e
35+
raise IOError, e.message
36+
end
37+
document_options = {
38+
base_uri: RDF::URI(filename_or_url),
39+
charset: Encoding::UTF_8,
40+
code: 200,
41+
headers: {}
42+
}
43+
#puts "use #{filename_or_url} locally"
44+
document_options[:headers][:content_type] = case filename_or_url.to_s
45+
when /\.ttl$/ then 'text/turtle'
46+
when /\.nt$/ then 'application/n-triples'
47+
when /\.jsonld$/ then 'application/ld+json'
48+
else 'unknown'
49+
end
50+
51+
document_options[:headers][:content_type] = response.content_type if response.respond_to?(:content_type)
52+
# For overriding content type from test data
53+
document_options[:headers][:content_type] = options[:contentType] if options[:contentType]
54+
55+
remote_document = RDF::Util::File::RemoteDocument.new(response.read, document_options)
56+
if block_given?
57+
yield remote_document
58+
else
59+
remote_document
60+
end
61+
else
62+
original_open_file(filename_or_url, options, &block)
63+
end
64+
end
65+
end
66+
end
67+
468
module Fixtures
569
module SuiteTest
670
SUITE = RDF::URI("http://json-ld.org/test-suite/")

0 commit comments

Comments
 (0)