Sha256: cdadf0ddb1648ce473169646da9d4484e9c6652a2f48e43483a39cc996681e87

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module M3u8
  # KeyItem represents a set of EXT-X-KEY attributes
  class KeyItem
    extend M3u8
    attr_accessor :method, :uri, :iv, :key_format, :key_format_versions

    def initialize(params = {})
      params.each do |key, value|
        instance_variable_set("@#{key}", value)
      end
    end

    def self.parse(text)
      attributes = parse_attributes text
      options = { method: attributes['METHOD'], uri: attributes['URI'],
                  iv: attributes['IV'], key_format: attributes['KEYFORMAT'],
                  key_format_versions: attributes['KEYFORMATVERSIONS'] }
      KeyItem.new options
    end

    def to_s
      attributes = [method_format,
                    uri_format,
                    iv_format,
                    key_format_format,
                    key_format_versions_format].compact.join(',')
      "#EXT-X-KEY:#{attributes}"
    end

    private

    def method_format
      "METHOD=#{method}"
    end

    def uri_format
      %(URI="#{uri}") unless uri.nil?
    end

    def iv_format
      "IV=#{iv}" unless iv.nil?
    end

    def key_format_format
      %(KEYFORMAT="#{key_format}") unless key_format.nil?
    end

    def key_format_versions_format
      return if key_format_versions.nil?

      %(KEYFORMATVERSIONS="#{key_format_versions}")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
m3u8-0.6.1 lib/m3u8/key_item.rb
m3u8-0.5.3 lib/m3u8/key_item.rb
m3u8-0.5.2 lib/m3u8/key_item.rb
m3u8-0.5.1 lib/m3u8/key_item.rb