imw 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. data/.gitignore +4 -1
  2. data/Rakefile +10 -0
  3. data/TODO +18 -0
  4. data/VERSION +1 -1
  5. data/bin/imw +1 -1
  6. data/etc/imwrc.rb +0 -50
  7. data/examples/dataset.rb +12 -0
  8. data/lib/imw/boot.rb +55 -9
  9. data/lib/imw/dataset/paths.rb +15 -24
  10. data/lib/imw/dataset/workflow.rb +131 -72
  11. data/lib/imw/dataset.rb +94 -186
  12. data/lib/imw/parsers/html_parser.rb +1 -1
  13. data/lib/imw/parsers.rb +1 -1
  14. data/lib/imw/repository.rb +3 -27
  15. data/lib/imw/resource.rb +190 -0
  16. data/lib/imw/resources/archive.rb +97 -0
  17. data/lib/imw/resources/archives_and_compressed/bz2.rb +18 -0
  18. data/lib/imw/resources/archives_and_compressed/gz.rb +18 -0
  19. data/lib/imw/resources/archives_and_compressed/rar.rb +23 -0
  20. data/lib/imw/resources/archives_and_compressed/tar.rb +23 -0
  21. data/lib/imw/resources/archives_and_compressed/tarbz2.rb +78 -0
  22. data/lib/imw/resources/archives_and_compressed/targz.rb +78 -0
  23. data/lib/imw/resources/archives_and_compressed/zip.rb +57 -0
  24. data/lib/imw/resources/archives_and_compressed.rb +32 -0
  25. data/lib/imw/resources/compressed_file.rb +89 -0
  26. data/lib/imw/resources/compressible.rb +77 -0
  27. data/lib/imw/resources/formats/delimited.rb +92 -0
  28. data/lib/imw/resources/formats/excel.rb +125 -0
  29. data/lib/imw/resources/formats/json.rb +53 -0
  30. data/lib/imw/resources/formats/sgml.rb +72 -0
  31. data/lib/imw/resources/formats/yaml.rb +53 -0
  32. data/lib/imw/resources/formats.rb +32 -0
  33. data/lib/imw/resources/local.rb +198 -0
  34. data/lib/imw/resources/remote.rb +110 -0
  35. data/lib/imw/resources/schemes/hdfs.rb +242 -0
  36. data/lib/imw/resources/schemes/http.rb +161 -0
  37. data/lib/imw/resources/schemes/s3.rb +137 -0
  38. data/lib/imw/resources/schemes.rb +19 -0
  39. data/lib/imw/resources.rb +118 -0
  40. data/lib/imw/runner.rb +5 -4
  41. data/lib/imw/transforms/archiver.rb +215 -0
  42. data/lib/imw/transforms/transferer.rb +103 -0
  43. data/lib/imw/transforms.rb +8 -0
  44. data/lib/imw/utils/error.rb +26 -30
  45. data/lib/imw/utils/extensions/array.rb +5 -15
  46. data/lib/imw/utils/extensions/hash.rb +6 -16
  47. data/lib/imw/utils/extensions/hpricot.rb +0 -14
  48. data/lib/imw/utils/extensions/string.rb +5 -15
  49. data/lib/imw/utils/extensions/symbol.rb +0 -13
  50. data/lib/imw/utils/extensions.rb +65 -0
  51. data/lib/imw/utils/log.rb +14 -13
  52. data/lib/imw/utils/misc.rb +0 -6
  53. data/lib/imw/utils/paths.rb +101 -42
  54. data/lib/imw/utils/version.rb +8 -9
  55. data/lib/imw/utils.rb +2 -18
  56. data/lib/imw.rb +92 -17
  57. data/spec/data/sample.csv +1 -1
  58. data/spec/data/sample.json +1 -0
  59. data/spec/data/sample.tsv +1 -1
  60. data/spec/data/sample.txt +1 -1
  61. data/spec/data/sample.xml +1 -1
  62. data/spec/data/sample.yaml +1 -1
  63. data/spec/imw/dataset/paths_spec.rb +32 -0
  64. data/spec/imw/dataset/workflow_spec.rb +41 -0
  65. data/spec/imw/resource_spec.rb +79 -0
  66. data/spec/imw/resources/archive_spec.rb +69 -0
  67. data/spec/imw/resources/archives_and_compressed/bz2_spec.rb +15 -0
  68. data/spec/imw/resources/archives_and_compressed/gz_spec.rb +15 -0
  69. data/spec/imw/resources/archives_and_compressed/rar_spec.rb +16 -0
  70. data/spec/imw/resources/archives_and_compressed/tar_spec.rb +16 -0
  71. data/spec/imw/resources/archives_and_compressed/tarbz2_spec.rb +24 -0
  72. data/spec/imw/resources/archives_and_compressed/targz_spec.rb +21 -0
  73. data/spec/imw/resources/archives_and_compressed/zip_spec.rb +16 -0
  74. data/spec/imw/resources/compressed_file_spec.rb +48 -0
  75. data/spec/imw/resources/compressible_spec.rb +36 -0
  76. data/spec/imw/resources/formats/delimited_spec.rb +33 -0
  77. data/spec/imw/resources/formats/json_spec.rb +32 -0
  78. data/spec/imw/resources/formats/sgml_spec.rb +24 -0
  79. data/spec/imw/resources/formats/yaml_spec.rb +41 -0
  80. data/spec/imw/resources/local_spec.rb +98 -0
  81. data/spec/imw/resources/remote_spec.rb +35 -0
  82. data/spec/imw/resources/schemes/hdfs_spec.rb +61 -0
  83. data/spec/imw/resources/schemes/http_spec.rb +19 -0
  84. data/spec/imw/resources/schemes/s3_spec.rb +19 -0
  85. data/spec/imw/transforms/archiver_spec.rb +120 -0
  86. data/spec/imw/transforms/transferer_spec.rb +113 -0
  87. data/spec/imw/utils/paths_spec.rb +5 -33
  88. data/spec/imw/utils/shared_paths_spec.rb +29 -0
  89. data/spec/spec_helper.rb +5 -5
  90. data/spec/support/paths_matcher.rb +67 -0
  91. data/spec/support/random.rb +39 -36
  92. metadata +88 -75
  93. data/lib/imw/dataset/task.rb +0 -41
  94. data/lib/imw/files/archive.rb +0 -113
  95. data/lib/imw/files/basicfile.rb +0 -122
  96. data/lib/imw/files/binary.rb +0 -28
  97. data/lib/imw/files/compressed_file.rb +0 -93
  98. data/lib/imw/files/compressed_files_and_archives.rb +0 -334
  99. data/lib/imw/files/compressible.rb +0 -103
  100. data/lib/imw/files/csv.rb +0 -113
  101. data/lib/imw/files/directory.rb +0 -62
  102. data/lib/imw/files/excel.rb +0 -84
  103. data/lib/imw/files/json.rb +0 -41
  104. data/lib/imw/files/sgml.rb +0 -46
  105. data/lib/imw/files/text.rb +0 -68
  106. data/lib/imw/files/yaml.rb +0 -46
  107. data/lib/imw/files.rb +0 -125
  108. data/lib/imw/packagers/archiver.rb +0 -126
  109. data/lib/imw/packagers/s3_mover.rb +0 -36
  110. data/lib/imw/packagers.rb +0 -8
  111. data/lib/imw/utils/components.rb +0 -61
  112. data/lib/imw/utils/config.rb +0 -46
  113. data/lib/imw/utils/extensions/class/attribute_accessors.rb +0 -8
  114. data/lib/imw/utils/extensions/core.rb +0 -27
  115. data/lib/imw/utils/extensions/dir.rb +0 -24
  116. data/lib/imw/utils/extensions/file_core.rb +0 -64
  117. data/lib/imw/utils/extensions/typed_struct.rb +0 -22
  118. data/lib/imw/utils/extensions/uri.rb +0 -59
  119. data/lib/imw/utils/view/dump_csv.rb +0 -112
  120. data/lib/imw/utils/view/dump_csv_older.rb +0 -117
  121. data/lib/imw/utils/view.rb +0 -113
  122. data/spec/imw/dataset/datamapper/uri_spec.rb +0 -43
  123. data/spec/imw/dataset/datamapper_spec_helper.rb +0 -11
  124. data/spec/imw/files/archive_spec.rb +0 -118
  125. data/spec/imw/files/basicfile_spec.rb +0 -121
  126. data/spec/imw/files/bz2_spec.rb +0 -32
  127. data/spec/imw/files/compressed_file_spec.rb +0 -96
  128. data/spec/imw/files/compressible_spec.rb +0 -100
  129. data/spec/imw/files/file_spec.rb +0 -144
  130. data/spec/imw/files/gz_spec.rb +0 -32
  131. data/spec/imw/files/rar_spec.rb +0 -33
  132. data/spec/imw/files/tar_spec.rb +0 -31
  133. data/spec/imw/files/text_spec.rb +0 -23
  134. data/spec/imw/files/zip_spec.rb +0 -31
  135. data/spec/imw/files_spec.rb +0 -38
  136. data/spec/imw/packagers/archiver_spec.rb +0 -125
  137. data/spec/imw/packagers/s3_mover_spec.rb +0 -7
  138. data/spec/imw/utils/extensions/file_core_spec.rb +0 -72
  139. data/spec/imw/utils/extensions/find_spec.rb +0 -113
  140. data/spec/imw/workflow/rip/local_spec.rb +0 -89
  141. data/spec/imw/workflow/rip_spec.rb +0 -27
  142. data/spec/support/archive_contents_matcher.rb +0 -94
  143. data/spec/support/directory_contents_matcher.rb +0 -61
@@ -0,0 +1,79 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe IMW::Resource do
4
+
5
+ describe "handling missing methods" do
6
+ before do
7
+ @resource = IMW::Resource.new('/home/foof.txt', :skip_modules => true)
8
+ end
9
+
10
+ it "should return false when querying with a method that isn't defined" do
11
+ @resource.is_remote?.should be_false
12
+ end
13
+
14
+ it "should raise an IMW::NoMethodError in any other case" do
15
+ lambda { @resource.do_seomthing }.should raise_error(IMW::NoMethodError)
16
+ end
17
+
18
+ it "should print the modules it's been extended by when raising an IMW::NoMethodError" do
19
+ begin
20
+ @resource.extend(IMW::Resources::LocalObj)
21
+ @resource.do_something
22
+ rescue IMW::NoMethodError => e
23
+ e.message.should match(/extended by IMW::Resources::LocalObj/)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "parsing various and sundry URIs should correctly parse a" do
29
+
30
+ before do
31
+ IMW::Resources.should_receive(:extend_resource!).with(an_instance_of(IMW::Resource))
32
+ end
33
+
34
+ it "local file path" do
35
+ resource = IMW::Resource.new("/home/foo.txt")
36
+ resource.stub!(:path).and_return("/home/foo.txt")
37
+
38
+ resource.scheme.should be_nil
39
+ resource.dirname.should == '/home'
40
+ resource.basename.should == 'foo.txt'
41
+ resource.extname.should == '.txt'
42
+ resource.extension.should == 'txt'
43
+ resource.name.should == 'foo'
44
+ end
45
+
46
+ it "local file path with spaces in the name" do
47
+ resource = IMW::Resource.new("/home/foo bar.txt")
48
+ resource.stub!(:path).and_return("/home/foo bar.txt")
49
+ resource.name.should == 'foo bar'
50
+ end
51
+
52
+ it "local file path with an explicit file:// scheme" do
53
+ resource = IMW::Resource.new("file:///home/foo.txt")
54
+ resource.scheme.should == 'file'
55
+ end
56
+
57
+ it "web URL with query and fragment" do
58
+ resource = IMW::Resource.new("http://mysite.com/some/page?param=value#frag")
59
+ resource.stub!(:path).and_return("/some/page")
60
+ resource.scheme.should == 'http'
61
+ resource.dirname.should == '/some'
62
+ resource.basename.should == 'page'
63
+ resource.extname.should == ''
64
+ resource.extension.should == ''
65
+ resource.name.should == 'page'
66
+ end
67
+
68
+ end
69
+
70
+ it "should open a URI without attempting to extend with modules if so asked" do
71
+ IMW::Resources.should_not_receive(:extend_resource!)
72
+ IMW::Resource.new("/path/to/some/file.txt", :skip_modules => true)
73
+ end
74
+
75
+ end
76
+
77
+
78
+
79
+
@@ -0,0 +1,69 @@
1
+ require File.join(File.dirname(__FILE__),'../../spec_helper')
2
+
3
+ # To use this shared example group define instance variables
4
+ # <tt>@extension</tt> and <tt>@cannot_append</tt> in your tests:
5
+ #
6
+ # @cannot_append = true
7
+ # before do
8
+ # # Notice that there is NO leading '.'
9
+ # @extension = 'tar.bz2'
10
+ # end
11
+ #
12
+ # it_should_behave_like "an archive of files"
13
+ #
14
+ # The <tt>@extension</tt> should correspond to an IMW::Resources
15
+ # module with a registered handler.
16
+ #
17
+ # If <tt>@cannot_append</tt> evaluates to true then the specs for
18
+ # appending to files will check for an error (this is because one
19
+ # typically cannot append to compressed archives). This instance
20
+ # variable should be defined OUTSIDE a before block.
21
+
22
+ share_examples_for "an archive of files" do
23
+ include Spec::Matchers::IMW
24
+
25
+ before do
26
+ @root = File.join(IMWTest::TMP_DIR, 'an_archive_of_files_shared_example_group')
27
+ @initial_directory = 'initial'
28
+ @appending_directory = 'appending'
29
+ @extraction_directory = 'extraction'
30
+ FileUtils.mkdir_p(@root)
31
+ FileUtils.cd(@root)
32
+ IMWTest::Random.directory_with_files(@initial_directory)
33
+ IMWTest::Random.directory_with_files(@appending_directory)
34
+ FileUtils.mkdir(@extraction_directory)
35
+ @archive = IMW::Resource.new("archive.#{@extension}") # define @extension in another spec
36
+ end
37
+
38
+ it "can create an archive" do
39
+ @archive.create(*Dir[@initial_directory + '/**/*'])
40
+ @archive.should contain_paths_like(@initial_directory, :relative_to => @root)
41
+ end
42
+
43
+ if @cannot_append
44
+ it "cannot append to an archive which already exists" do
45
+ @archive.create(*Dir[@initial_directory + "/**/*"])
46
+ lambda { @archive.append(*Dir[@appending_directory + "/**/*"]) }.should raise_error(IMW::Error)
47
+ end
48
+ else
49
+ it "can append to an archive which already exists" do
50
+ @archive.create(*Dir[@initial_directory + "/**/*"])
51
+ @archive.append(*Dir[@appending_directory + "/**/*"])
52
+ @archive.should contain_paths_like([@initial_directory,@appending_directory], :relative_to => @root)
53
+ end
54
+
55
+ it "can append to an archive which doesn't already exist" do
56
+ @archive.append(*Dir[@appending_directory + "/**/*"])
57
+ @archive.should contain_paths_like(@appending_directory, :relative_to => @root)
58
+ end
59
+ end
60
+
61
+ it "can extract files which match the original ones it archived" do
62
+ @archive.create(*Dir[@initial_directory + "/**/*"])
63
+ FileUtils.cd @extraction_directory do
64
+ @archive.extract
65
+ end
66
+ @initial_directory.should contain_paths_like(@extraction_directory, :given_base => File.join(@root, @extraction_directory, @initial_directory), :to_match_base => File.join(@root, @initial_directory))
67
+ end
68
+
69
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../../../spec_helper"
2
+ require File.dirname(__FILE__) + "/../compressed_file_spec"
3
+
4
+ describe IMW::Resources::CompressedFiles::Bz2 do
5
+
6
+ before do
7
+ @extension = 'bz2'
8
+ end
9
+
10
+ it_should_behave_like 'a compressed file'
11
+ end
12
+
13
+
14
+
15
+
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../../../spec_helper"
2
+ require File.dirname(__FILE__) + "/../compressed_file_spec"
3
+
4
+ describe IMW::Resources::CompressedFiles::Gz do
5
+
6
+ before do
7
+ @extension = 'gz'
8
+ end
9
+
10
+ it_should_behave_like 'a compressed file'
11
+ end
12
+
13
+
14
+
15
+
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + "/../../../spec_helper"
2
+ require File.dirname(__FILE__) + "/../archive_spec"
3
+
4
+ describe IMW::Resources::Archives::Rar do
5
+
6
+ before do
7
+ @extension = 'rar'
8
+ end
9
+
10
+ it_should_behave_like 'an archive of files'
11
+
12
+ end
13
+
14
+
15
+
16
+
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + "/../../../spec_helper"
2
+ require File.dirname(__FILE__) + "/../archive_spec"
3
+
4
+ describe IMW::Resources::Archives::Tar do
5
+
6
+ before do
7
+ @extension = 'tar'
8
+ end
9
+
10
+ it_should_behave_like 'an archive of files'
11
+
12
+ end
13
+
14
+
15
+
16
+
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + "/../../../spec_helper"
2
+ require File.dirname(__FILE__) + "/../archive_spec"
3
+ require File.dirname(__FILE__) + "/../compressed_file_spec"
4
+
5
+ describe IMW::Resources::Archives::Tarbz2 do
6
+ @cannot_append = true
7
+ before do
8
+ @extension = 'tar.bz2'
9
+ end
10
+
11
+ it_should_behave_like 'an archive of files'
12
+ end
13
+
14
+ describe IMW::Resources::Archives::Tarbz2 do
15
+ before do
16
+ @extension = 'tar.bz2'
17
+ end
18
+ it_should_behave_like 'a compressed file'
19
+ end
20
+
21
+
22
+
23
+
24
+
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + "/../../../spec_helper"
2
+ require File.dirname(__FILE__) + "/../archive_spec"
3
+ require File.dirname(__FILE__) + "/../compressed_file_spec"
4
+
5
+ describe IMW::Resources::Archives::Targz do
6
+ @cannot_append = true
7
+ before do
8
+ @extension = 'tar.gz'
9
+ end
10
+
11
+ it_should_behave_like 'an archive of files'
12
+ end
13
+
14
+
15
+ describe IMW::Resources::Archives::Targz do
16
+ before do
17
+ @extension = 'tar.gz'
18
+ end
19
+
20
+ it_should_behave_like 'a compressed file'
21
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + "/../../../spec_helper"
2
+ require File.dirname(__FILE__) + "/../archive_spec"
3
+
4
+ describe IMW::Resources::Archives::Zip do
5
+
6
+ before do
7
+ @extension = 'zip'
8
+ end
9
+
10
+ it_should_behave_like 'an archive of files'
11
+
12
+ end
13
+
14
+
15
+
16
+
@@ -0,0 +1,48 @@
1
+ require File.join(File.dirname(__FILE__),'../../spec_helper')
2
+
3
+ # To use this shared example group define an instance variable
4
+ # <tt>@extension</tt> in your tests:
5
+ #
6
+ # before do
7
+ # # Notice that there is NO leading '.'
8
+ # @extension = 'gz'
9
+ # end
10
+ #
11
+ # it_should_behave_like "a compressed file"
12
+ #
13
+ # The <tt>@extension</tt> should correspond to an IMW::Resources
14
+ # module with a registered handler.
15
+
16
+ share_examples_for "a compressed file" do
17
+ include Spec::Matchers::IMW
18
+
19
+ before do
20
+ @root = File.join(IMWTest::TMP_DIR, 'a_compressed_file_shared_example_group')
21
+ FileUtils.mkdir_p(@root)
22
+ FileUtils.cd(@root)
23
+ IMWTest::Random.file("compressed_file.#{@extension}") # define @extension in another spec
24
+ @compressed_file = IMW::Resource.new("compressed_file.#{@extension}")
25
+ end
26
+
27
+ it "should know that it is compressed" do
28
+ @compressed_file.is_compressed?.should be_true
29
+ @compressed_file.is_compressible?.should be_false
30
+ end
31
+
32
+ it "can decompress the file in place" do
33
+ uncompressed_file = @compressed_file.decompress!
34
+ @compressed_file.exist?.should be_false
35
+ uncompressed_file.exist?.should be_true
36
+ uncompressed_file.is_compressed?.should be_false
37
+ uncompressed_file.is_compressible?.should be_true
38
+ end
39
+
40
+ it "can decompress the file without deleting the original file" do
41
+ uncompressed_file = @compressed_file.decompress
42
+ @compressed_file.exist?.should be_true
43
+ uncompressed_file.exist?.should be_true
44
+ uncompressed_file.is_compressed?.should be_false
45
+ uncompressed_file.is_compressible?.should be_true
46
+ end
47
+
48
+ end
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__),'../../spec_helper')
2
+
3
+ describe IMW::Resources::Compressible do
4
+
5
+ before do
6
+ IMWTest::Random.file('foobar.txt')
7
+ @resource = IMW::Resource.new('foobar.txt')
8
+ end
9
+
10
+ it "should extend a local resource " do
11
+ @resource.is_compressible?.should be_true
12
+ @resource.is_compressed?.should be_false
13
+ end
14
+
15
+ it "can compress a resource in place" do
16
+ compressed_file = @resource.compress!
17
+
18
+ # only the compressed file should now exist
19
+ compressed_file.exist?.should be_true
20
+ @resource.exist?.should be_false
21
+
22
+ compressed_file.is_compressed?.should be_true
23
+ compressed_file.is_compressible?.should be_false
24
+ end
25
+
26
+ it "can compress a resource without overwriting the original file" do
27
+ compressed_file = @resource.compress
28
+
29
+ # both files should now exist
30
+ compressed_file.exist?.should be_true
31
+ @resource.exist?.should be_true
32
+
33
+ compressed_file.is_compressed?.should be_true
34
+ compressed_file.is_compressible?.should be_false
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__),'../../../spec_helper')
2
+
3
+ describe IMW::Resources::Formats::Csv do
4
+ # we don't test Tsv as the differences from Csv are trivial and
5
+ # effect only code within the FasterCSV library
6
+
7
+ before do
8
+ @sample = IMW.open(File.join(IMWTest::DATA_DIR, 'sample.csv'))
9
+ end
10
+
11
+ it "should be able to parse the CSV" do
12
+ @sample.load[1].last.should == 'lemurinus'
13
+ end
14
+
15
+ it "should be able to write CSV" do
16
+ data = [['foobar', 1, 2], ['bazbooz', 3, 4]]
17
+ IMW.open!('test.csv').dump(data)
18
+ IMW.open('test.csv').load[1].last.should == "4"
19
+ end
20
+
21
+ it "should yield each row when load is given a block" do
22
+ @sample.load do |row|
23
+ row.class.should == Array
24
+ break
25
+ end
26
+ end
27
+
28
+ it "can map each row with a block" do
29
+ @sample.map do |row|
30
+ row.first
31
+ end.class.should == Array
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__),'../../../spec_helper')
2
+
3
+ describe IMW::Resources::Formats::Json do
4
+
5
+ before do
6
+ @sample = IMW.open(File.join(IMWTest::DATA_DIR, 'sample.json'))
7
+ end
8
+
9
+ it "should be able to parse the JSON" do
10
+ @sample.load['monkeys'].first['monkey']['id'].should == 1
11
+ end
12
+
13
+ it "should be able to write JSON" do
14
+ IMW.open!('test.json').dump({ 'foobar' => 3, 'bazbooz' => 4 })
15
+ IMW.open('test.json').load['foobar'].should == 3
16
+ end
17
+
18
+ it "should yield each key and value when the JSON is a hash and it's given a block" do
19
+ @sample.load do |key, value|
20
+ value.size.should == 130
21
+ end
22
+ end
23
+
24
+ it "should yield each element when the JSON is an array and it's given a block" do
25
+ IMW.open!('test.json').dump([1,2,3])
26
+ num = 1
27
+ IMW.open('test.json').load do |parsed_num|
28
+ parsed_num.should == num
29
+ num +=1
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__),'../../../spec_helper')
2
+
3
+ describe IMW::Resources::Formats::Xml do
4
+ # just spec Xml now as the others are identical
5
+
6
+ before do
7
+ @sample = IMW.open(File.join(IMWTest::DATA_DIR, 'sample.xml'))
8
+ end
9
+
10
+ it "should be able to load the XML" do
11
+ ((@sample.load/"monkey").first/"genus").inner_text.should == 'Aotus'
12
+ end
13
+
14
+ it "should yield the XML when load is given a block" do
15
+ @sample.load do |xml|
16
+ ((xml/"monkey").first/"genus").inner_text.should == 'Aotus'
17
+ end
18
+ end
19
+
20
+ it "should parse the XML" do
21
+ @sample.parse(:monkeys => ['monkey'])[:monkeys].size.should == 130
22
+ end
23
+ end
24
+
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__),'../../../spec_helper')
2
+
3
+ describe IMW::Resources::Formats::Yaml do
4
+
5
+ before do
6
+ @sample = IMW.open(File.join(IMWTest::DATA_DIR, 'sample.yaml'))
7
+ end
8
+
9
+ it "should be able to parse the YAML" do
10
+ @sample.load['monkeys'].first['monkey']['id'].should == 1
11
+ end
12
+
13
+ it "should be able to write YAML" do
14
+ data = { 'foobar' => 3, 'bazbooz' => 4 }
15
+ IMW.open!('test.yaml').dump(data)
16
+ IMW.open('test.yaml').load['foobar'].should == 3
17
+ end
18
+
19
+ it "should yield each key and value when the YAML is a hash and it's given a block" do
20
+ @sample.load do |key, value|
21
+ value.size.should == 130
22
+ end
23
+ end
24
+
25
+ it "should yield each element when the YAML is an array and it's given a block" do
26
+ IMW.open!('test.yaml').dump([1,2,3])
27
+ num = 1
28
+ IMW.open('test.yaml').load do |parsed_num|
29
+ parsed_num.should == num
30
+ num +=1
31
+ end
32
+ end
33
+
34
+ it "should yield a string when the YAML is a string and it's given a block" do
35
+ IMW.open!('test.yaml').dump('foobar')
36
+ IMW.open('test.yaml').load do |string|
37
+ string.should == 'foobar'
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,98 @@
1
+ require File.join(File.dirname(__FILE__),'../../spec_helper')
2
+
3
+ describe IMW::Resources::LocalObj do
4
+
5
+ it "should not extend a local file with LocalDirectory" do
6
+ @file = IMW::Resource.new('foo.txt', :skip_modules => true)
7
+ @file.should_not_receive(:extend).with(IMW::Resources::LocalDirectory)
8
+ @file.extend_appropriately!
9
+ end
10
+
11
+ it "should not extend a local directory with LocalFile" do
12
+ @dir = IMW::Resource.new(IMWTest::TMP_DIR, :skip_modules => true)
13
+ @dir.should_not_receive(:extend).with(IMW::Resources::LocalFile)
14
+ @dir.extend_appropriately!
15
+ end
16
+
17
+ it "should correctly resolve relative paths" do
18
+ IMW.open('foobar').dirname.should == IMWTest::TMP_DIR
19
+ end
20
+ end
21
+
22
+ describe IMW::Resources::LocalFile do
23
+ before do
24
+ IMWTest::Random.file('original.txt')
25
+ @file = IMW::Resource.new('original.txt')
26
+ end
27
+
28
+ it "can delete the file" do
29
+ @file.rm
30
+ @file.exist?.should be_false
31
+ end
32
+
33
+
34
+ it "can read a file" do
35
+ @file.read.size.should > 0
36
+ end
37
+
38
+ it "can load the lines of a file" do
39
+ data = @file.load
40
+ data.size.should > 0
41
+ data.class.should == Array
42
+ end
43
+
44
+ it "can iterate over the lines of a file" do
45
+ @file.load do |line|
46
+ line.class.should == String
47
+ break
48
+ end
49
+ end
50
+
51
+ it "can map the lines of a file" do
52
+ @file.map do |line|
53
+ line[0..5]
54
+ end.class.should == Array
55
+ end
56
+ end
57
+
58
+ describe IMW::Resources::LocalDirectory do
59
+ before do
60
+ FileUtils.mkdir_p('dir')
61
+ FileUtils.mkdir_p('dir/subdir')
62
+ FileUtils.cd('dir') do
63
+ IMWTest::Random.file('file1.tsv')
64
+ IMWTest::Random.file('file2.tsv')
65
+ FileUtils.cd('subdir') do
66
+ IMWTest::Random.file('file3.csv')
67
+ end
68
+ end
69
+ @dir = IMW::Resource.new('dir')
70
+ end
71
+
72
+ it "can delete an empty directory" do
73
+ FileUtils.mkdir('empty')
74
+ dir = IMW.open('empty')
75
+ dir.rmdir
76
+ dir.exist?.should be_false
77
+ end
78
+
79
+ it "can recursively delete a directory" do
80
+ @dir.rm_rf
81
+ @dir.exist?.should be_false
82
+ end
83
+
84
+ it "can list its contents" do
85
+ @dir.contents.size.should == 3
86
+ end
87
+
88
+ it "can list its contents recursively" do
89
+ @dir.all_contents.size.should == 4
90
+ end
91
+
92
+ it "can list its contents recursively as IMW::Resource objects" do
93
+ @dir.resources.map(&:class).uniq.first.should == IMW::Resource
94
+ end
95
+
96
+ end
97
+
98
+
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__),'../../spec_helper')
2
+
3
+ describe IMW::Resources::RemoteObj do
4
+ end
5
+
6
+ describe IMW::Resources::RemoteFile do
7
+
8
+ before { @file = IMW.open('http://www.google.com') }
9
+
10
+ describe 'with the file' do
11
+
12
+ it "can read a remote file" do
13
+ @file.read.size.should > 0
14
+ end
15
+
16
+ it "can load the lines of a remote file" do
17
+ data = @file.load
18
+ data.size.should > 0
19
+ data.class.should == Array
20
+ end
21
+
22
+ it "can iterate over the lines of a remote file" do
23
+ @file.load do |line|
24
+ line.class.should == String
25
+ break
26
+ end
27
+ end
28
+
29
+ it "can map the lines of a remote file" do
30
+ @file.map do |line|
31
+ line[0..5]
32
+ end.class.should == Array
33
+ end
34
+ end
35
+ end