Module: RDF::Util::File
- Defined in:
- lib/rdf/util/file.rb
Overview
Wrapper for retrieving RDF resources from HTTP(S) and file: scheme locations.
By default, HTTP(S) resources are retrieved using Net::HTTP. However, If the Rest Client gem is included, it will be used for retrieving resources, allowing for sophisticated HTTP caching using REST Client Components allowing the use of Rack::Cache
to avoid network access.
To use other HTTP clients, consumers can subclass HttpAdapter and set the File.
Also supports the file: scheme for access to local files.
Defined Under Namespace
Classes: FaradayAdapter, HttpAdapter, NetHttpAdapter, RemoteDocument, RestClientAdapter
Class Method Summary collapse
-
.http_adapter(use_net_http = false) ⇒ HttpAdapter
Get current HTTP adapter.
-
.http_adapter=(http_adapter) ⇒ HttpAdapter
Set the HTTP adapter.
-
.open_file(filename_or_url, proxy: nil, headers: {}, verify_none: false, **options) {|RemoteDocument| ... } ⇒ RemoteDocument, Object
Open the file, returning or yielding RemoteDocument.
Class Method Details
.http_adapter(use_net_http = false) ⇒ HttpAdapter
Get current HTTP adapter. If no adapter has been explicitly set, use RestClientAdapter (if RestClient is loaded), or the NetHttpAdapter
244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/rdf/util/file.rb', line 244 def http_adapter(use_net_http = false) if use_net_http NetHttpAdapter else @http_adapter ||= begin # Otherwise, fallback to Net::HTTP if defined?(RestClient) RestClientAdapter else NetHttpAdapter end end end end |
.http_adapter=(http_adapter) ⇒ HttpAdapter
Set the HTTP adapter
232 233 234 |
# File 'lib/rdf/util/file.rb', line 232 def http_adapter= http_adapter @http_adapter = http_adapter end |
.open_file(filename_or_url, proxy: nil, headers: {}, verify_none: false, **options) {|RemoteDocument| ... } ⇒ RemoteDocument, Object
Open the file, returning or yielding RemoteDocument.
Input received as non-unicode, is transformed to UTF-8. With Ruby >= 2.2, all UTF is normalized to Unicode Normalization Form C (NFC).
HTTP resources may be retrieved via proxy using the proxy
option. If RestClient
is loaded, they will use the proxy globally by setting something like the following: RestClient.proxy = "http://proxy.example.com/"
. When retrieving documents over HTTP(S), use the mechanism described in Providing and Discovering URI Documentation to pass the appropriate base_uri
to the block or as the return.
Applications needing HTTP caching may consider Rest Client and REST Client Components allowing the use of Rack::Cache
as a local file cache.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/rdf/util/file.rb', line 299 def self.open_file(filename_or_url, proxy: nil, headers: {}, verify_none: false, **, &block) filename_or_url = $1 if filename_or_url.to_s.match(/^file:(.*)$/) remote_document = nil if filename_or_url.to_s.match?(/^https?/) base_uri = filename_or_url.to_s remote_document = self.http_adapter(!![:use_net_http]). open_url(base_uri, proxy: proxy, headers: headers, verify_none: verify_none, **) else # Fake content type based on found format format = RDF::Format.for(filename_or_url.to_s) 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).dup url_no_frag_or_query.query = nil url_no_frag_or_query.fragment = nil [:encoding] ||= Encoding::UTF_8 Kernel.open(url_no_frag_or_query, "r", **) do |file| = { base_uri: filename_or_url.to_s, charset: file.external_encoding.to_s, code: 200, content_type: content_type, last_modified:file.mtime, headers: {content_type: content_type, last_modified: file.mtime.xmlschema} } remote_document = RemoteDocument.new(file.read, ) end rescue Errno::ENOENT => e raise IOError, e. end end if block_given? yield remote_document else remote_document end end |