Sha256: 2a2f156929417e50f145e4d55be1f7b376921689b210b3ad6a65233885c934a2

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

require 'nokogiri'
require 'hashie'

module Duracloud
  class ContentManifest < Hashie::Dash

    property :space_id, required: true
    property :manifest_id, required: true
    property :store_id

    def self.find(**kwargs)
      new(**kwargs).tap do |manifest|
        manifest.content
      end
    end

    def content
      @content ||= Content.new(space_id: space_id, content_id: manifest_id, store_id: store_id).tap do |c|
        c.load_properties
      end
    end

    def source
      @source ||= Source.new(self)
    end

    def xml
      @xml ||= content.download.body
    end

    protected

    def method_missing(name, *args, &block)
      if content.respond_to?(name)
        content.send(name, *args, &block)
      else
        super
      end
    end

    class Source
      attr_reader :manifest

      def initialize(manifest)
        @manifest = manifest
      end

      def doc
        @doc ||= Nokogiri::XML(manifest.xml)
      end

      def md5
        doc.css("sourceContent md5").text
      end

      def content_id
        doc.css("sourceContent").first["contentId"]
      end

      def size
        doc.css("sourceContent byteSize").text.to_i
      end

      def content_type
        doc.css("sourceContent mimetype").text
      end

      def download(&block)
        chunks.each do |chunk|
          chunk.download(&block)
        end
      end

      def chunks
        Enumerator.new do |e|
          doc.css("chunk").each do |chunk_xml|
            e << Content.find(space_id: manifest.space_id,
                              content_id: chunk_xml["chunkId"],
                              store_id: manifest.store_id,
                              md5: chunk_xml.css("md5").text)
          end
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
duracloud-client-0.10.2 lib/duracloud/content_manifest.rb
duracloud-client-0.10.1 lib/duracloud/content_manifest.rb
duracloud-client-0.10.0 lib/duracloud/content_manifest.rb