Class: Html2rss::Article::Enclosure

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/article/enclosure.rb

Overview

Represents an enclosure attached to an article (RSS / JSON Feed media).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, type: nil, bytes_length: 0) ⇒ Enclosure

Returns a new instance of Enclosure.

Parameters:

  • url (Html2rss::Url)

    absolute enclosure URL

  • type (String, nil) (defaults to: nil)

    optional enclosure MIME type

  • bytes_length (Integer) (defaults to: 0)

    enclosure length in bytes

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
# File 'lib/html2rss/article/enclosure.rb', line 30

def initialize(url:, type: nil, bytes_length: 0)
  raise ArgumentError, 'An Enclosure requires an absolute URL' if !url || !url.absolute?

  @url = url
  @type = type
  @bytes_length = bytes_length
end

Instance Attribute Details

#bytes_lengthInteger (readonly)

Returns enclosure length in bytes.

Returns:

  • (Integer)

    enclosure length in bytes



42
43
44
# File 'lib/html2rss/article/enclosure.rb', line 42

def bytes_length
  @bytes_length
end

#urlHtml2rss::Url (readonly)

Returns absolute enclosure URL.

Returns:



45
46
47
# File 'lib/html2rss/article/enclosure.rb', line 45

def url
  @url
end

Class Method Details

.guess_content_type_from_url(url, default: 'application/octet-stream') ⇒ String

Guesses the content type based on the file extension of the URL.

Parameters:

  • url (Html2rss::Url)
  • default (String) (defaults to: 'application/octet-stream')

    default content type

Returns:

  • (String)

    guessed content type, or default



16
17
18
19
20
21
22
23
24
25
# File 'lib/html2rss/article/enclosure.rb', line 16

def self.guess_content_type_from_url(url, default: 'application/octet-stream')
  return default unless url

  path = url.path
  ext = File.extname(path)
  ext = ext[1..] if ext.start_with?('.')

  content_type = MIME::Types.type_for(ext)
  content_type.first&.to_s || 'application/octet-stream'
end

Instance Method Details

#typeString

Returns explicit MIME type or one inferred from URL extension.

Returns:

  • (String)

    explicit MIME type or one inferred from URL extension



39
# File 'lib/html2rss/article/enclosure.rb', line 39

def type = @type || self.class.guess_content_type_from_url(url)