Class: RDF::Util::File::RemoteDocument

Inherits:
StringIO
  • Object
show all
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/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

Instance Method Summary collapse

Constructor Details

#initialize(body, options = {}) ⇒ RemoteDocument

Set content

Parameters:

  • body (String)

    entity content of request.

Since:

  • 0.2.4



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 378

def initialize(body, options = {})
  options.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 = options.fetch(:headers, {})
  @charset = options[:charset].to_s.downcase if options[:charset]

  # Find Content-Type
  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('=')
      next unless p.downcase == 'charset'
      @charset ||= v.sub(/^["']?(.*)["']?$/, '\1').downcase
    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, "r:#{encoding}")
end

Instance Attribute Details

#base_uriString (readonly)

Base URI based on resource location or returned Location header.

Returns:

Since:

  • 0.2.4



344
345
346
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 344

def base_uri
  @base_uri
end

#charsetString (readonly)

Encoding of resource (from Content-Type), downcased. Also applied to content if it is UTF

Returns:

Since:

  • 0.2.4



352
353
354
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 352

def charset
  @charset
end

#codeInteger (readonly)

Response code

Returns:

Since:

  • 0.2.4



356
357
358
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 356

def code
  @code
end

#content_typeString (readonly)

Content-Type of the returned resource

Returns:

Since:

  • 0.2.4



348
349
350
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 348

def content_type
  @content_type
end

#etagString (readonly)

ETag from headers

Returns:

Since:

  • 0.2.4



361
362
363
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 361

def etag
  @etag
end

#headersHash{Symbol => Object} (readonly)

Raw headers from response

Returns:

Since:

  • 0.2.4



369
370
371
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 369

def headers
  @headers
end

#last_modifiedDateTime (readonly)

Last-Modified time from headers

Returns:

  • (DateTime)

Since:

  • 0.2.4



365
366
367
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 365

def last_modified
  @last_modified
end

#requested_urlString (readonly)

Originally requested URL

Returns:

Since:

  • 0.2.4



373
374
375
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 373

def requested_url
  @requested_url
end

Instance Method Details

#content_encodingArray<String>

Returns a list of encodings in Content-Encoding field as an array of strings.

The encodings are downcased for canonicalization.

Returns:

Since:

  • 0.2.4



426
427
428
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 426

def content_encoding
  headers.fetch(:content_encoding, "").split(',').map(&:strip).map(&:downcase)
end

Return links from the Link header.

Links can be returned in array form, or searched.

Examples:


d = RemoteDocument.new(...)
describedby = links.find_link(['rel', 'describedby']).href

Returns:

  • (::LinkHeader)

Since:

  • 0.2.4



441
442
443
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-1b20f426c35b/lib/rdf/util/file.rb', line 441

def links
  @links ||= LinkHeader.parse(@headers[:link])
end