Class: Matterhorn::MediaPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/matterhorn/media_package.rb

Overview

Matterhorn::MediaPackage ===

Constant Summary collapse

XML_NS_MEDIAPACKAGE =

————————————————————————– const definitions —

"http://mediapackage.opencastproject.org"

Instance Method Summary collapse

Constructor Details

#initialize(xml = nil, options = {}) ⇒ MediaPackage

—————————————————————————– initialization —



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/matterhorn/media_package.rb', line 15

def initialize(xml = nil, options = {})
  if xml
    @document = Nokogiri::XML(xml)
  else
    @document = Nokogiri::XML::Builder.new do |xml|
      xml.mediapackage('xmlns' => XML_NS_MEDIAPACKAGE) do
        xml.media
        xml.
        xml.attachments
      end
    end
    .doc
  end
  @path     = if (path = options[:path])
                path + (path[-1] == '/' ? '' : '/')        # guarantee that path ends with a slash
              else
                nil
              end
  @prefix   = options[:prefix]
  @filename = if @prefix
                "#{@prefix}_manifest.xml"
              else
                'manifest.xml'
              end
end

Instance Method Details

#add_attachment(file, flavor) ⇒ Object

<attachments>

  <attachment type="switchcastrecorder/metadata">
    <tags/>
    <url>metadata.plist</url>
  </attachment>
</attachments>


66
67
68
69
70
71
72
73
# File 'lib/matterhorn/media_package.rb', line 66

def add_attachment(file, flavor)
  Nokogiri::XML::Builder.with(@document.at('attachments')) do |xml|
    xml.attachment(:type => flavor) {
      xml.tags
      xml.url file.sub(@path, '')
    }
  end
end

#add_catalog(file, flavor, mimetype = 'text/xml') ⇒ Object

<metadata>

<catalog type="dublincore/episode">
  <mimetype>text/xml</mimetype>
  <tags/>
  <url>dublincore.xml</url>
</catalog>

</metadata>



84
85
86
87
88
89
90
91
92
# File 'lib/matterhorn/media_package.rb', line 84

def add_catalog(file, flavor, mimetype = 'text/xml')
  Nokogiri::XML::Builder.with(@document.at('metadata')) do |xml|
    xml.catalog(:type => flavor) {
      xml.mimetype mimetype
      xml.tags
      xml.url file.sub(@path, '')
    }
  end
end

#add_dc_catalog(dublin_core) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/matterhorn/media_package.rb', line 95

def add_dc_catalog(dublin_core)
  filename = @prefix ? "#{@prefix}_dublincore.xml" : 'dublincore.xml'
  flavor   = 'dublincore/episode'
  dc_doc   = Nokogiri::XML(dublin_core)
  dc_file  = File.join(@path, filename)
  File.open(dc_file, 'w') do |file|
    file.write(dc_doc.to_xml)
  end
  add_catalog(dc_file, flavor)
end

#add_track(file, flavor) ⇒ Object

<media>

<track type="presenter/source+partial">
  <tags/>
  <url>source1/mux_2013_12-17T14_51_29_738.mov</url>
</track>

</media>



125
126
127
128
129
130
131
132
# File 'lib/matterhorn/media_package.rb', line 125

def add_track(file, flavor)
  Nokogiri::XML::Builder.with(@document.at('media')) do |xml|
    xml.track(:type => flavor) {
      xml.tags
      xml.url file.sub(@path, '')
    }
  end
end

#dc_catalog_urlObject



107
108
109
110
111
112
113
114
115
# File 'lib/matterhorn/media_package.rb', line 107

def dc_catalog_url
  url_elem = @document.at_xpath('//xmlns:catalog[@type="dublincore/episode"]/xmlns:url',
                                {'xmlns' => XML_NS_MEDIAPACKAGE})
  if url_elem
    url_elem.content
  else
    nil
  end
end

#documentObject

———————————————————————————– methodes —



44
45
46
# File 'lib/matterhorn/media_package.rb', line 44

def document
  @document
end

#identifierObject

Returns the id attribute of mediapackage element. <mediapackage xmlns=“mediapackage.opencastproject.org” id=“1” duration=“2704016” start=“2014-04-23T12:35:00Z”>



149
150
151
# File 'lib/matterhorn/media_package.rb', line 149

def identifier
  @document.xpath('/xmlns:mediapackage/@id', {'xmlns' => XML_NS_MEDIAPACKAGE}).first.value
end

#inspectObject



54
55
56
# File 'lib/matterhorn/media_package.rb', line 54

def inspect
  to_xml.to_s
end

#save(path = @path, filename = @filename) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/matterhorn/media_package.rb', line 154

def save(path = @path, filename = @filename)
  unless path
    raise(Matterhorn::Error, "No path was set, where manifest file should be saved!")
  end
  manifest_file = File.join(path, filename)
  File.open(manifest_file, 'w') do |file|
    file.write(to_xml)
  end
end

#to_xmlObject



49
50
51
# File 'lib/matterhorn/media_package.rb', line 49

def to_xml
  @document.to_xml
end

#track_url(flavor) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/matterhorn/media_package.rb', line 135

def track_url(flavor)
  url_elem = @document.at_xpath("//xmlns:track[contains(@type, \"#{flavor}\")]/xmlns:url",
                                {'xmlns' => Matterhorn::MediaPackage::XML_NS_MEDIAPACKAGE})
  if url_elem
    url_elem.content
  else
    nil
  end
end