@@ -9,6 +9,10 @@ module File
99 REMOTE_PATH = "https://raw.githubusercontent.com/pchampin/ld-patch-testsuite/master/"
1010 LOCAL_PATH = ::File . expand_path ( "../testsuite" , __FILE__ ) + '/'
1111
12+ class << self
13+ alias_method :original_open_file , :open_file
14+ end
15+
1216 ##
1317 # Override to use Patron for http and https, Kernel.open otherwise.
1418 #
@@ -19,42 +23,56 @@ module File
1923 # @return [IO] File stream
2024 # @yield [IO] File stream
2125 def self . open_file ( filename_or_url , options = { } , &block )
22- case filename_or_url . to_s
23- when /^file:/
26+ case
27+ when filename_or_url . to_s =~ /^file:/
2428 path = filename_or_url [ 5 ..-1 ]
2529 Kernel . open ( path . to_s , options , &block )
26- when /^#{ REMOTE_PATH } /
27- begin
28- #puts "attempt to open #{filename_or_url} locally"
29- local_filename = filename_or_url . to_s . sub ( REMOTE_PATH , LOCAL_PATH )
30- if ::File . exist? ( local_filename )
31- response = ::File . open ( local_filename )
32- #puts "use #{filename_or_url} locally"
33- case filename_or_url . to_s
34- when /\. ttl$/
35- def response . content_type ; 'text/turtle' ; end
36- when /\. ldpatch$/
37- def response . content_type ; 'text/ldpatch' ; end
38- end
39-
40- if block_given?
41- begin
42- yield response
43- ensure
44- response . close
45- end
46- else
47- response
48- end
30+ when ( filename_or_url . to_s =~ %r{^#{ REMOTE_PATH } } && Dir . exist? ( LOCAL_PATH ) )
31+ localpath = RDF ::URI ( filename_or_url ) . dup
32+ localpath . query = nil
33+ localpath = localpath . to_s . sub ( REMOTE_PATH , LOCAL_PATH )
34+ response = begin
35+ ::File . open ( localpath )
36+ rescue Errno ::ENOENT => e
37+ raise IOError , e . message
38+ end
39+ document_options = {
40+ base_uri : RDF ::URI ( filename_or_url ) ,
41+ charset : Encoding ::UTF_8 ,
42+ code : 200 ,
43+ headers : { }
44+ }
45+ #puts "use #{filename_or_url} locally"
46+ document_options [ :headers ] [ :content_type ] = case filename_or_url . to_s
47+ when /\. ttl$/ then 'text/turtle'
48+ when /\. ldpatch$/ then 'text/ldpatch'
49+ else 'unknown'
50+ end
51+
52+ document_options [ :headers ] [ :content_type ] = response . content_type if response . respond_to? ( :content_type )
53+ # For overriding content type from test data
54+ document_options [ :headers ] [ :content_type ] = options [ :contentType ] if options [ :contentType ]
55+
56+ remote_document = RDF ::Util ::File ::RemoteDocument . new ( response . read , document_options )
57+ if block_given?
58+ yield remote_document
59+ else
60+ remote_document
61+ end
62+ else
63+ original_open_file ( filename_or_url , options ) do |rd |
64+ # Override content_type
65+ if options [ :contentType ]
66+ rd . headers [ :content_type ] = options [ :contentType ]
67+ rd . instance_variable_set ( :@content_type , options [ :contentType ] . split ( ';' ) . first )
68+ end
69+
70+ if block_given?
71+ yield rd
4972 else
50- Kernel . open ( filename_or_url . to_s , options . fetch ( :headers , { } ) , & block )
73+ rd
5174 end
52- rescue Errno ::ENOENT #, OpenURI::HTTPError
53- # Not there, don't run tests
54- StringIO . new ( "" )
5575 end
56- else
57- Kernel . open ( filename_or_url . to_s , options . fetch ( :headers , { } ) , &block )
5876 end
5977 end
6078 end
0 commit comments