Class: Html2rss::Article::Enclosure
- Inherits:
-
Object
- Object
- Html2rss::Article::Enclosure
- Defined in:
- lib/html2rss/article/enclosure.rb
Overview
Represents an enclosure attached to an article (RSS / JSON Feed media).
Instance Attribute Summary collapse
-
#bytes_length ⇒ Integer
readonly
Enclosure length in bytes.
-
#url ⇒ Html2rss::Url
readonly
Absolute enclosure URL.
Class Method Summary collapse
-
.guess_content_type_from_url(url, default: 'application/octet-stream') ⇒ String
Guesses the content type based on the file extension of the URL.
Instance Method Summary collapse
-
#initialize(url:, type: nil, bytes_length: 0) ⇒ Enclosure
constructor
A new instance of Enclosure.
-
#type ⇒ String
Explicit MIME type or one inferred from URL extension.
Constructor Details
#initialize(url:, type: nil, bytes_length: 0) ⇒ Enclosure
Returns a new instance of Enclosure.
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_length ⇒ Integer (readonly)
Returns enclosure length in bytes.
42 43 44 |
# File 'lib/html2rss/article/enclosure.rb', line 42 def bytes_length @bytes_length end |
#url ⇒ Html2rss::Url (readonly)
Returns absolute enclosure URL.
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.
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
#type ⇒ String
Returns 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) |