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.
43 44 45 46 47 48 49 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 43 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.
61 62 63 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 61 def url @url end |
Class Method Details
.add(enclosure, maker) ⇒ void
This method returns an undefined value.
30 31 32 33 34 35 36 37 38 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 30 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 24 25 |
# 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 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
#bits_length ⇒ Integer
Returns enclosure length in bytes (legacy reader name).
58 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 58 def bits_length = bytes_length |
#bytes_length ⇒ Integer
Returns enclosure length in bytes.
55 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 55 def bytes_length = @bits_length |
#type ⇒ String
Returns explicit MIME type or one inferred from URL extension.
52 |
# File 'lib/html2rss/rss_builder/enclosure.rb', line 52 def type = @type || self.class.guess_content_type_from_url(url) |