Class: Html2rss::RssBuilder::Enclosure
- Inherits:
-
Object
- Object
- Html2rss::RssBuilder::Enclosure
- Defined in:
- lib/html2rss/rss_builder/enclosure.rb
Overview
Represents an enclosure for an RSS item.
Instance Attribute Summary collapse
-
#url ⇒ Html2rss::Url
readonly
Absolute enclosure URL.
Class Method Summary collapse
- .add(enclosure, maker) ⇒ void
-
.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
-
#bits_length ⇒ Integer
Enclosure length in bytes (legacy reader name).
-
#bytes_length ⇒ Integer
Enclosure length in bytes.
-
#initialize(url:, type: nil, bits_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, bits_length: 0) ⇒ Enclosure
Returns a new instance of Enclosure.
41 42 43 44 45 46 47 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 41 def initialize(url:, type: nil, bits_length: 0) raise ArgumentError, 'An Enclosure requires an absolute URL' if !url || !url.absolute? @url = url @type = type @bits_length = bits_length end |
Instance Attribute Details
#url ⇒ Html2rss::Url (readonly)
Returns absolute enclosure URL.
59 60 61 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 59 def url @url end |
Class Method Details
.add(enclosure, maker) ⇒ void
This method returns an undefined value.
28 29 30 31 32 33 34 35 36 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 28 def self.add(enclosure, maker) return unless enclosure maker.enclosure.tap do |enclosure_maker| enclosure_maker.url = enclosure.url.to_s enclosure_maker.type = enclosure.type enclosure_maker.length = enclosure.bits_length end end |
.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 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 16 def self.guess_content_type_from_url(url, default: 'application/octet-stream') return default unless url url = url.path.split('?').first content_type = MIME::Types.type_for(File.extname(url).delete('.')) content_type.first&.to_s || 'application/octet-stream' end |
Instance Method Details
#bits_length ⇒ Integer
Returns enclosure length in bytes (legacy reader name).
56 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 56 def bits_length = bytes_length |
#bytes_length ⇒ Integer
Returns enclosure length in bytes.
53 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 53 def bytes_length = @bits_length |
#type ⇒ String
Returns explicit MIME type or one inferred from URL extension.
50 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 50 def type = @type || self.class.guess_content_type_from_url(url) |