Class: RDF::Util::File::RemoteDocument
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb
Overview
A RemoteDocument contains the body and headers of a remote resource.
Link headers are parsed using the LinkHeader
gem
Instance Attribute Summary collapse
-
#base_uri ⇒ String
readonly
Base URI based on resource location or returned Location header.
-
#charset ⇒ String
readonly
Encoding of resource (from Content-Type), downcased.
-
#code ⇒ Integer
readonly
Response code.
-
#content_type ⇒ String
readonly
Content-Type of the returned resource.
-
#etag ⇒ String
readonly
ETag from headers.
-
#headers ⇒ Hash{Symbol => Object}
readonly
Raw headers from response.
-
#last_modified ⇒ DateTime
readonly
Last-Modified time from headers.
-
#parameters ⇒ Symbol => String
readonly
Parameters from Content-Type.
-
#requested_url ⇒ String
readonly
Originally requested URL.
Instance Method Summary collapse
-
#content_encoding ⇒ Array<String>
Returns a list of encodings in Content-Encoding field as an array of strings.
-
#initialize(body, options = {}) ⇒ RemoteDocument
constructor
Set content.
-
#links ⇒ ::LinkHeader
Return links from the Link header.
Constructor Details
#initialize(body, options = {}) ⇒ RemoteDocument
Set content
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 401 def initialize(body, = {}) .each do |key, value| # de-quote charset matchdata = value.match(/^["'](.*)["']$/.freeze) if key == "charset" value = matchdata[1] if matchdata value = value.downcase if value.is_a?(String) instance_variable_set(:"@#{key}", value) end @headers = .fetch(:headers, {}) @charset = [:charset].to_s.downcase if [:charset] @parameters = {} # Find Content-Type and extract other parameters if headers[:content_type] ct, *params = headers[:content_type].split(';').map(&:strip) @content_type ||= ct # Find charset params.each do |param| p, v = param.split('=') @parameters[p.downcase.to_sym] = v.sub(/^["']?([^"']*)["']?$/, '\1') @charset ||= @parameters[p.downcase.to_sym].downcase if p.downcase == 'charset' end end @etag = headers[:etag] @last_modified = DateTime.parse(headers[:last_modified]) if headers[:last_modified] encoding = @charset ||= "utf-8" unless encoding.start_with?("utf") body.force_encoding(Encoding::UTF_8) encoding = "utf-8" # Make sure Unicode is in NFC begin body.unicode_normalize! unless !body.unicode_normalized? rescue Encoding::CompatibilityError # Oh, well ... end if body.respond_to?(:unicode_normalized?) end super(body).set_encoding encoding end |
Instance Attribute Details
#base_uri ⇒ String (readonly)
Base URI based on resource location or returned Location header.
363 364 365 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 363 def base_uri @base_uri end |
#charset ⇒ String (readonly)
Encoding of resource (from Content-Type), downcased. Also applied to content if it is UTF
371 372 373 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 371 def charset @charset end |
#code ⇒ Integer (readonly)
Response code
379 380 381 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 379 def code @code end |
#content_type ⇒ String (readonly)
Content-Type of the returned resource
367 368 369 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 367 def content_type @content_type end |
#etag ⇒ String (readonly)
ETag from headers
384 385 386 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 384 def etag @etag end |
#headers ⇒ Hash{Symbol => Object} (readonly)
Raw headers from response
392 393 394 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 392 def headers @headers end |
#last_modified ⇒ DateTime (readonly)
Last-Modified time from headers
388 389 390 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 388 def last_modified @last_modified end |
#parameters ⇒ Symbol => String (readonly)
Parameters from Content-Type
375 376 377 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 375 def parameters @parameters end |
#requested_url ⇒ String (readonly)
Originally requested URL
396 397 398 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 396 def requested_url @requested_url end |
Instance Method Details
#content_encoding ⇒ Array<String>
Returns a list of encodings in Content-Encoding field as an array of strings.
The encodings are downcased for canonicalization.
450 451 452 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 450 def content_encoding headers.fetch(:content_encoding, "").split(',').map(&:strip).map(&:downcase) end |
#links ⇒ ::LinkHeader
Return links from the Link header.
Links can be returned in array form, or searched.
465 466 467 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/util/file.rb', line 465 def links @links ||= LinkHeader.parse(@headers[:link]) end |