Commits

Anonymous committed ca1453c

add support for pre-existing erc file

  • Participants
  • Parent commits 42bb37f

Comments (0)

Files changed (2)

File lib/mrt_ingest/iobject.rb

   module Ingest
     # An object ready for ingest into Merritt.
     class IObject
+      
+      attr_accessor :primary_identifier, :local_identifier, :erc, :erc_file
+
       def initialize(options={})
         @primary_identifier = options[:primary_identifier]
         @local_identifier = options[:local_identifier]
         @erc = options[:erc] || Hash.new
         @file_components = []
         @uri_components = []
-        @server = Mrt::Ingest::OneTimeServer.new
+        @server = options[:server] || Mrt::Ingest::OneTimeServer.new
       end
       
       def add_component(component, name)
       
       # Make a Mrt::Ingest::Request object for this mrt-object
       def mk_request(profile, submitter)
-        erc_url = @server.add_file do |f|
-          @erc.each do |k, v|
-            f.write("#{k}: #{v}\n")
-          end
-        end
+        erc_url = case @erc
+                  when URI
+                    @erc
+                  when File
+                    @server.add_file(@erc)
+                  when Hash
+                    @server.add_file do |f|
+                      @erc.each do |k, v|
+                        f.write("#{k}: #{v}\n")
+                      end
+                    end
+                  end
         manifest_file = Tempfile.new("mrt-ingest")
         mk_manifest(manifest_file, erc_url)
         # reset to beginning
               :primary_identifier => @primary_identifier)
       end
 
-      # Starts the web server for this object in a new thread. Returns
-      # the thread.
       def start_server
-        Thread.new do
-          @server.run
-        end
+        return @server.start_server()
+      end
+
+      def join_server
+        return @server.join_server()
       end
         
       def mk_manifest(manifest, erc_url)
         manifest.write("#%prefix | nfo: | http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#\n")
         manifest.write("#%fields | nfo:fileUrl | nfo:hashAlgorithm | nfo:hashValue | nfo:fileSize | nfo:fileLastModified | nfo:fileName | mrt:mimeType\n")
         @uri_components.each { |uri|
-          manifest.write("#{uri[0]} | | | | | #{url[1]} |\n")
+          manifest.write("#{uri[0]} | | | | | #{url[1]} | \n")
         }
-        manifest.write("#{erc_url} | | | | | mrt-erc.txt\n")
+        manifest.write("#{erc_url} | | | | | mrt-erc.txt | \n")
         manifest.write("#%EOF\n")
       end
     end

File test/test_iobject.rb

 require 'open-uri'
 
 class TestIObject < Test::Unit::TestCase
+  def parse_object_manifest(iobject)
+    req = iobject.mk_request("profile", "submitter")
+    args = req.mk_args
+    return Checkm::Manifest.new(args['file'].read())
+  end
+
   context "when creating an iobject" do
     setup do
       @iobject = Mrt::Ingest::IObject.new
   context "the created request" do
     setup do
       @iobject = Mrt::Ingest::IObject.new
-      @req = @iobject.mk_request("profile", "submitter")
-      @args = @req.mk_args
-      @manifest = Checkm::Manifest.new(@args['file'].read())
+      @manifest = parse_object_manifest(@iobject)
+      @erc_pos = @manifest.entries.find_index { |entry| 
+        entry.values[-2] == "mrt-erc.txt" 
+      }
     end
     
     should "generate a valid manifest file with more than one line" do
     end
     
     should "have a mrt-erc.txt entry, and it should be fetchable" do
-      erc_pos = @manifest.entries.find_index { |entry| 
-        entry.values[-2] == "mrt-erc.txt" }
+      if @erc_pos.nil?
+        assert(false, "Could not find mrt-erc.txt file!")
+      else
+        erc_url = @manifest.entries[@erc_pos].values[0]
+        @iobject.start_server()
+        erc_lines = open(erc_url).read().lines().to_a
+        @iobject.join_server()
+      end
+    end
+  end
+
+  ERC_CONTENT = <<EOS
+who: John Doe
+what: Something
+when: now
+EOS
+
+  context "different ERC options" do
+    should "be able to specify a file for ERC" do
+      erc_tempfile = Tempfile.new('test_iobject')
+      erc_tempfile << ERC_CONTENT
+      # reset to beginning
+      erc_tempfile.open
+      iobject = Mrt::Ingest::IObject.new(:erc=>File.new(erc_tempfile.path))
+      manifest = parse_object_manifest(iobject)
+      erc_pos = manifest.entries.find_index { |entry| 
+        entry.values[-2] == "mrt-erc.txt" 
+      }
       if erc_pos.nil?
         assert(false, "Could not find mrt-erc.txt file!")
       else
-        erc_url = @manifest.entries[erc_pos].values[0]
-        t = @iobject.start_server
-        erc_lines = open(erc_url).read().lines().to_a
-        t.join
+        iobject.start_server
+        erc_url = manifest.entries[erc_pos].values[0]
+        assert_equal(ERC_CONTENT, open(erc_url).read())
+        iobject.join_server
       end
     end
   end