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](rubygems.org/gems/rest-client) gem is included, it will be used for retrieving resources, allowing for sophisticated HTTP caching using [REST Client Components](rubygems.org/gems/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.http_adapter.
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, 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
257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/rdf/util/file.rb', line 257 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
245 246 247 |
# File 'lib/rdf/util/file.rb', line 245 def http_adapter= http_adapter @http_adapter = http_adapter end |
.open_file(filename_or_url, options = {}) {|RemoteDocument| ... } ⇒ RemoteDocument, Object
Open the file, returning or yielding RemoteDocument.
Adds Accept header based on available reader content types to allow for content negotiation based on available readers.
Input received as non-unicode, is transformed to UTF-8. With Ruby >= 2.2, all UTF is normalized to [Unicode Normalization Form C (NFC)](unicode.org/reports/tr15/#Norm_Forms).
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](www.w3.org/2001/tag/awwsw/issue57/latest/) to pass the appropriate ‘base_uri` to the block or as the return.
Applications needing HTTP caching may consider [Rest Client](rubygems.org/gems/rest-client) and [REST Client Components](rubygems.org/gems/rest-client-components) allowing the use of ‘Rack::Cache` as a local file cache.
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 345 346 347 348 349 350 351 352 353 |
# File 'lib/rdf/util/file.rb', line 313 def self.open_file(filename_or_url, = {}, &block) filename_or_url = $1 if filename_or_url.to_s.match(/^file:(.*)$/) remote_document = nil if filename_or_url.to_s =~ /^https?/ base_uri = filename_or_url.to_s remote_document = self.http_adapter(!![:use_net_http]).open_url(base_uri, ) 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) 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 |