hybag 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjJmNGNhNzQ1Y2U1MTYyZDFiNDJlYzk5ZWU2MGUxODI0OTFjZTNhOA==
4
+ MDA1ZjhjNWEzYWE5NTEwNGQ4ODM4NGVkMDY1N2E2MmI4YjVhYWFlOQ==
5
5
  data.tar.gz: !binary |-
6
- MjViZjdlZDAzMzJmMDFiYmE0OTVkZjg1NTBmMDlhYmM5MDhkOTNkZg==
6
+ MjdiYWIzZGIwNDRmOWJjYjFhODFiMDY2NzBiNDM2NmI0OTRiNjIwMA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- OWRmMDdiNDBkNjUyMGViZjU0YzI3NTQxNmQwMjMxYjRlNGJjMjM5OGI0ZTE2
10
- ZTRjZThjNjhkMmZiMDNiYzc5NDA0MzRkMDUxNjFmMWEyMmFkY2RmMTk5OTI3
11
- YjM2NDllZWVlYTk3OThhOGY2YzBlNTZmYjNjN2I5MzU0ZjA4YWE=
9
+ ZmVjOGUxZTVkODQyOGJlOTMxZjM1YWU1MzgzNmQ3ODhhOGM2Yjc5NmJkMzUy
10
+ NDUyYTI1OTM2MDQyZDY0OWVmNjQxMjI2MDdkYTM5MTBlNjFmN2JjNTNhMmVl
11
+ MTMxZTVhYWM5MTYxZWFmZTBlMTg0NTVmOTc2MGNlNzIzZWU4MjA=
12
12
  data.tar.gz: !binary |-
13
- ODBhOTY5OWQ0NzJhMjNkZWZkMmQ2YjBlYmRjNGIyODUyYTA3YmVkMTQyZTdl
14
- NjFjZjlmZDQwMGVmMDc2OTNkM2RiNzI3OWZjN2QzY2I5ZDQyZTE5OWNlN2Y4
15
- ZjkzZjhkYTdhNGU5Y2M4YjMxNWFiZTU1MWMyNjQwMWEzMTcyZGE=
13
+ Y2IyMmIyODI0NjA3YWI3MWE5YTgxODNjYTY2YmNjZmRjZmQ0NTMzN2UzMDVk
14
+ ZTk5MjQ0MmZhNmI3MzAxY2IyMjA3MTcwYjY5ZTIwOGQ3MzlkODI4M2Q4Njdl
15
+ YjJlOTAyYWUzYjQ2NzI5ZTAwNGE1N2U2MTk5MDkyNGRjNmMxMTY=
data/lib/hybag.rb CHANGED
@@ -7,14 +7,30 @@ require 'hybag/baggable'
7
7
  require 'hybag/validator'
8
8
  require 'hybag/bag_writer'
9
9
  require 'hybag/ingester'
10
+ require 'hybag/bulk_ingester'
10
11
 
11
12
  module Hybag
12
13
  def self.ingest(bag)
14
+ return self.bulk_ingest(bag) if self.bulk_directory?(bag)
15
+ bag = BagIt::Bag.new(bag.to_s) unless bag.kind_of?(BagIt::Bag)
13
16
  ingester = Hybag::Ingester.new(bag)
14
17
  yield(ingester) if block_given?
15
18
  ingester.ingest
16
19
  end
17
20
 
21
+ def self.bulk_ingest(directory)
22
+ objects = []
23
+ Hybag::BulkIngester.new(directory).each do |ingester|
24
+ yield(ingester) if block_given?
25
+ objects << ingester.ingest
26
+ end
27
+ return objects
28
+ end
29
+
30
+ def self.bulk_directory?(directory)
31
+ Dir.glob(File.join(directory,"*")).select{|x| File.directory?(x) && File.exist?(File.join(x, "bagit.txt"))}.length > 0
32
+ end
33
+
18
34
  # Error Classes
19
35
  class UndiscoverableModelName < StandardError
20
36
  def initialize(bag)
@@ -0,0 +1,13 @@
1
+ class Hybag::BulkIngester
2
+ include Enumerable
3
+ def initialize(directory)
4
+ @bags = Dir.glob(File.join(directory,"*")).select{|f| File.directory? f}.map{|x| BagIt::Bag.new(x) unless !File.exists?(File.join(x, "bagit.txt"))}.compact
5
+ end
6
+
7
+ def each
8
+ return enum_for(:each) unless block_given?
9
+ @bags.each do |bag|
10
+ yield Hybag::Ingester.new(bag) if bag.complete?
11
+ end
12
+ end
13
+ end
data/lib/hybag/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hybag
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ require 'open-uri'
3
+ require 'pry'
4
+ require File.join(DUMMY_PATH, "baggable_dummy")
5
+ # Override Kernel.open
6
+ module ::Kernel
7
+ private
8
+
9
+ alias :original_open :open
10
+ def open(name, *rest, &block)
11
+ if name =~ /\|.*/ then
12
+ original_open(name, *rest, &block)
13
+ else
14
+ FakeFS::File.open(name, *rest, &block)
15
+ end
16
+ end
17
+ module_function :open
18
+ end
19
+ describe Hybag::BulkIngester do
20
+ before(:each) do
21
+ @items = []
22
+ 5.times do |n|
23
+ item = BaggableDummy.new(:pid => "filler#{n}")
24
+ item.descMetadata.content = File.open(File.join(FIXTURE_PATH,"example_datastream.nt")).read.strip
25
+ content_file = File.open(File.join(FIXTURE_PATH,"hydra.png"))
26
+ item.add_file_datastream(content_file, :dsid => "content", :mimeType => "image/png")
27
+ item.rels_ext.content = File.open(File.join(FIXTURE_PATH,"rels.rdf")).read
28
+ item.load_relationships
29
+ item.stub(:persisted?).and_return(true)
30
+ @items << item
31
+ end
32
+ # Stub Rails root
33
+ rails = double("Rails")
34
+ Rails = rails unless defined?(Rails)
35
+ Rails.stub(:root).and_return(Pathname.new('/test'))
36
+ # Stub ActiveFedora assign_pid
37
+ ActiveFedora::Base.stub(:assign_pid).and_return(*(5.times.map{|x| "new_filler_#{x}"}))
38
+ end
39
+ 5.times do |n|
40
+ let("item_#{n}".to_sym) {@items[n]}
41
+ let("bag_#{n}".to_sym) {@items[n].write_bag}
42
+ end
43
+ subject {Hybag::BulkIngester.new("/test/tmp/bags")}
44
+ describe "functionality" do
45
+ include FakeFS::SpecHelpers
46
+ context "when given a directory of bags" do
47
+ before(:each) do
48
+ bag_0
49
+ bag_1
50
+ bag_2
51
+ bag_3
52
+ bag_4
53
+ end
54
+ describe ".each" do
55
+ it "should return an enumerator" do
56
+ expect(subject.each).to be_kind_of(Enumerator)
57
+ end
58
+ it "should return an ingest object" do
59
+ expect(subject.each.first).to be_kind_of(Hybag::Ingester)
60
+ end
61
+ end
62
+ it "should only ingest bagit bags" do
63
+ FileUtils.mkdir('/test/tmp/bags/6')
64
+ expect(subject.each.to_a.length).to eq 5
65
+ end
66
+ it "should be able to ingest a bunch of bags" do
67
+ result = subject.map{|ingester| ingester.ingest}
68
+ expect(result.length).to eq 5
69
+ expect(result.first.pid).to eq "new_filler_0"
70
+ expect(result.first.title).to eq ["Mexican workers"]
71
+ end
72
+ end
73
+ end
74
+ end
@@ -54,32 +54,7 @@ describe Hybag::Ingester do
54
54
  expect(subject.send(:model_name)).to eq "TestModel"
55
55
  end
56
56
  end
57
- #context "when there is a hybag.yml" do
58
- # before(:each) do
59
- # FileUtils.rm(subject.send(:fedora_rels), :force => true)
60
- # # Add the hybag.yml from fixture
61
- # FakeFS.deactivate!
62
- # hybag_content = File.read(File.join(FIXTURE_PATH,"hybag.yml"))
63
- # FakeFS.activate!
64
- # File.open(File.join(bag.bag_dir,"hybag.yml"),'w') {|f| f.puts hybag_content}
65
- # end
66
- # it "should pull the data out of hybag.yml" do
67
- # subject.send(:model_name).should == "TestModel"
68
- # end
69
- #end
70
57
  end
71
- #context "when there is a rels and a hybag.yml" do
72
- # before(:each) do
73
- # # Add the hybag.yml from fixture
74
- # FakeFS.deactivate!
75
- # hybag_content = File.read(File.join(FIXTURE_PATH,"hybag.yml"))
76
- # FakeFS.activate!
77
- # File.open(File.join(bag.bag_dir,"hybag.yml"),'w') {|f| f.puts hybag_content}
78
- # end
79
- # it "should prioritize the rels" do
80
- # subject.send(:model_name).should == "BaggableDummy"
81
- # end
82
- #end
83
58
  end
84
59
  describe ".ingest" do
85
60
  context "when there is no discoverable model" do
@@ -1,11 +1,60 @@
1
1
  require 'spec_helper'
2
+ require 'open-uri'
3
+ require 'pry'
4
+ require File.join(DUMMY_PATH, "baggable_dummy")
2
5
 
3
6
  describe Hybag do
4
7
  describe ".ingest" do
8
+ include FakeFS::SpecHelpers
5
9
  it "should call Hybag::Ingester.ingest" do
6
10
  Hybag::Ingester.any_instance.should_receive(:ingest).and_return("bla")
7
11
  Hybag.ingest("empty")
8
12
  end
13
+ it "should make a bag out of a directory" do
14
+ BagIt::Bag.should_receive(:new).with("/bag/directory")
15
+ Hybag::Ingester.any_instance.should_receive(:ingest).and_return("bla")
16
+ Hybag.ingest("/bag/directory")
17
+ end
18
+ context "full test" do
19
+ before(:each) do
20
+ FileUtils.mkdir_p("/bag/directory")
21
+ FakeFS.deactivate!
22
+ @item = BaggableDummy.new(:pid => '1')
23
+ @item.descMetadata.content = File.open(File.join(FIXTURE_PATH,"example_datastream.nt")).read.strip
24
+ content_file = File.open(File.join(FIXTURE_PATH,"hydra.png"))
25
+ @item.add_file_datastream(content_file, :dsid => "content", :mimeType => "image/png")
26
+ @item.rels_ext.content = File.open(File.join(FIXTURE_PATH,"rels.rdf")).read
27
+ @item.load_relationships
28
+ @item.stub(:persisted?).and_return(true)
29
+ # Stub Rails root
30
+ rails = double("Rails")
31
+ Rails = rails unless defined?(Rails)
32
+ Rails.stub(:root).and_return(Pathname.new('/'))
33
+ # Stub ActiveFedora assign_pid
34
+ ActiveFedora::Base.stub(:assign_pid).and_return("new_pid")
35
+ @item.stub(:bag_path_namespace).and_return(File.join("bag","directory"))
36
+ FakeFS.activate!
37
+ @bag = @item.write_bag
38
+ end
39
+ it "should return objects" do
40
+ objects = Hybag.ingest("/bag/directory")
41
+ expect(objects.length).to eq 1
42
+ expect(objects.first).to be_kind_of(ActiveFedora::Base)
43
+ end
44
+ end
45
+ context "when given a directory of bags" do
46
+ before(:each) do
47
+ Hybag.should_receive(:bulk_directory?).and_return(true)
48
+ end
49
+ it "should not call Ingester" do
50
+ Hybag::Ingester.any_instance.should_not_receive(:ingest)
51
+ Hybag.ingest("/bag/directory")
52
+ end
53
+ it "should call Bulk Ingester" do
54
+ Hybag::BulkIngester.should_receive(:new).with("/bag/directory").and_return([])
55
+ Hybag.ingest("/bag/directory")
56
+ end
57
+ end
9
58
  it "should allow the ingester to be configurable" do
10
59
  ingesting = nil
11
60
  Hybag::Ingester.any_instance.should_receive(:ingest).and_return("bla")
@@ -16,4 +65,4 @@ describe Hybag do
16
65
  expect(ingesting.model_name).to eq "TestModel"
17
66
  end
18
67
  end
19
- end
68
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hybag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trey Terrell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-11 00:00:00.000000000 Z
11
+ date: 2013-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -173,6 +173,7 @@ files:
173
173
  - lib/hybag.rb
174
174
  - lib/hybag/bag_writer.rb
175
175
  - lib/hybag/baggable.rb
176
+ - lib/hybag/bulk_ingester.rb
176
177
  - lib/hybag/ingester.rb
177
178
  - lib/hybag/validator.rb
178
179
  - lib/hybag/version.rb
@@ -182,6 +183,7 @@ files:
182
183
  - spec/fixtures/hydra.png
183
184
  - spec/fixtures/rels.rdf
184
185
  - spec/lib/hybag/baggable_spec.rb
186
+ - spec/lib/hybag/bulk_ingester_spec.rb
185
187
  - spec/lib/hybag/ingester_spec.rb
186
188
  - spec/lib/hybag/validator_spec.rb
187
189
  - spec/lib/hybag_spec.rb
@@ -218,6 +220,7 @@ test_files:
218
220
  - spec/fixtures/hydra.png
219
221
  - spec/fixtures/rels.rdf
220
222
  - spec/lib/hybag/baggable_spec.rb
223
+ - spec/lib/hybag/bulk_ingester_spec.rb
221
224
  - spec/lib/hybag/ingester_spec.rb
222
225
  - spec/lib/hybag/validator_spec.rb
223
226
  - spec/lib/hybag_spec.rb