wax_iiif 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +29 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +11 -0
  5. data/Gemfile +4 -0
  6. data/Guardfile +10 -0
  7. data/LICENSE.md +30 -0
  8. data/README.md +8 -0
  9. data/Rakefile +24 -0
  10. data/lib/iiif_s3/base_properties.rb +95 -0
  11. data/lib/iiif_s3/builder.rb +254 -0
  12. data/lib/iiif_s3/collection.rb +61 -0
  13. data/lib/iiif_s3/config.rb +142 -0
  14. data/lib/iiif_s3/errors.rb +37 -0
  15. data/lib/iiif_s3/full_image.rb +20 -0
  16. data/lib/iiif_s3/image_info.rb +96 -0
  17. data/lib/iiif_s3/image_record.rb +141 -0
  18. data/lib/iiif_s3/image_tile.rb +46 -0
  19. data/lib/iiif_s3/image_variant.rb +126 -0
  20. data/lib/iiif_s3/manifest.rb +151 -0
  21. data/lib/iiif_s3/thumbnail.rb +35 -0
  22. data/lib/iiif_s3/utilities.rb +12 -0
  23. data/lib/iiif_s3/utilities/helpers.rb +96 -0
  24. data/lib/iiif_s3/utilities/pdf_splitter.rb +50 -0
  25. data/lib/iiif_s3/version.rb +5 -0
  26. data/lib/wax_iiif.rb +83 -0
  27. data/spec/base_properties_spec.rb +22 -0
  28. data/spec/data/blank.csv +0 -0
  29. data/spec/data/invalid.csv +1 -0
  30. data/spec/data/no_header.csv +1 -0
  31. data/spec/data/test.csv +2 -0
  32. data/spec/data/test.jpg +0 -0
  33. data/spec/data/test.pdf +0 -0
  34. data/spec/iiif_s3/builder_spec.rb +152 -0
  35. data/spec/iiif_s3/collection_spec.rb +68 -0
  36. data/spec/iiif_s3/config_spec.rb +15 -0
  37. data/spec/iiif_s3/image_info_spec.rb +57 -0
  38. data/spec/iiif_s3/image_record_spec.rb +96 -0
  39. data/spec/iiif_s3/image_variant_spec.rb +71 -0
  40. data/spec/iiif_s3/manifest_spec.rb +97 -0
  41. data/spec/iiif_s3/utilities/pdf_splitter_spec.rb +17 -0
  42. data/spec/shared_contexts.rb +115 -0
  43. data/spec/spec_helper.rb +12 -0
  44. data/test.rb +77 -0
  45. data/wax_iiif.gemspec +29 -0
  46. metadata +218 -0
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require "base_properties_spec"
3
+
4
+
5
+ describe IiifS3::ImageRecord do
6
+ let(:opts) {{
7
+ id: 1
8
+ }}
9
+ let(:image_record) {IiifS3::ImageRecord.new(opts)}
10
+
11
+ it "initializes without an error" do
12
+ expect{IiifS3::ImageRecord.new}.not_to raise_error
13
+ end
14
+ it "initializes without an error when provided a hash" do
15
+ opts = {id: 1}
16
+ expect{IiifS3::ImageRecord.new(opts)}.not_to raise_error
17
+ end
18
+
19
+ context "#viewing_direction" do
20
+ it "has a sensible default" do
21
+ expect(image_record.viewing_direction).to eq IiifS3::DEFAULT_VIEWING_DIRECTION
22
+ end
23
+
24
+ it "rejects invalid viewing directions on init" do
25
+ opts = {viewing_direction: "wonky"}
26
+ expect{IiifS3::ImageRecord.new(opts)}.to raise_error(IiifS3::Error::InvalidViewingDirection)
27
+ end
28
+
29
+ it "rejects setting invalid viewing directions" do
30
+ expect{image_record.viewing_direction = "wonky"}.to raise_error(IiifS3::Error::InvalidViewingDirection)
31
+ end
32
+ end
33
+
34
+ it "initializes with provided hash values" do
35
+ expect(image_record.id).to eq opts[:id]
36
+ end
37
+ it "ignores unknown data" do
38
+ opts["junk_data"] = "bozo"
39
+ expect{IiifS3::ImageRecord.new(opts)}.not_to raise_error
40
+ end
41
+ context "#is_primary" do
42
+ it "defaults to false" do
43
+ image_record.page_number = 2
44
+ expect(image_record.is_primary).to equal(false)
45
+ end
46
+ it "defaults to true for first pages" do
47
+ image_record.page_number = 1
48
+ expect(image_record.is_primary).to equal(true)
49
+ end
50
+ it "has an alias" do
51
+ image_record.page_number = 1
52
+ expect(image_record.is_primary?).to equal(true)
53
+ end
54
+ it "forces is_primary to boolean" do
55
+ image_record.is_primary = "banana"
56
+ expect(image_record.is_primary).to equal(true)
57
+ end
58
+ it "uses page_number for intellegent defaults" do
59
+ image_record.page_number = 1
60
+ expect(image_record.is_primary).to equal(true)
61
+ end
62
+ it "allows page_number default to be overridded" do
63
+ image_record.page_number = 1
64
+ image_record.is_primary = false
65
+ expect(image_record.is_primary).to equal(false)
66
+ end
67
+ end
68
+ context "#image_path" do
69
+ it "raises on a blan path" do
70
+ expect{image_record.path = nil}.to raise_error(IiifS3::Error::InvalidImageData)
71
+ end
72
+ it "raises an error for a bad file name" do
73
+ expect{image_record.path = "imaginary_file.jpg"}.to raise_error(IiifS3::Error::InvalidImageData)
74
+ end
75
+ end
76
+ context "#section" do
77
+ it "uses the default for the name" do
78
+ expect(image_record.section).to eq DEFAULT_CANVAS_LABEL
79
+ end
80
+ it "uses the default for the label" do
81
+ expect(image_record.section_label).to eq DEFAULT_CANVAS_LABEL
82
+ end
83
+ end
84
+ context "#is_document" do
85
+ it "defaults to false" do
86
+ expect(image_record.is_document).to equal(false)
87
+ end
88
+ it "has_an_alias" do
89
+ expect(image_record.is_document?).to equal(false)
90
+ end
91
+ it "forces is_document to boolean" do
92
+ image_record.is_document = "banana"
93
+ expect(image_record.is_document).to equal(true)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe IiifS3::ImageVariant do
4
+ let(:config) {IiifS3::Config.new}
5
+ let(:data) { IiifS3::ImageRecord.new({
6
+ "path" => "./spec/data/test.jpg",
7
+ "id" => 1,
8
+ "page_number" => 1
9
+ }) }
10
+
11
+ context "initialization errors" do
12
+ it "raises if the image does not have an ID" do
13
+ data.id =nil
14
+ expect{IiifS3::ImageVariant.new(data, config)}.to raise_error(IiifS3::Error::InvalidImageData)
15
+ end
16
+ it "raises if the image has a blank ID" do
17
+ data.id = ""
18
+ expect{IiifS3::ImageVariant.new(data, config)}.to raise_error(IiifS3::Error::InvalidImageData)
19
+ end
20
+
21
+ it "raises if the image is not a valid image file" do
22
+ data.path = "./spec/data/test.csv"
23
+ expect{IiifS3::ImageVariant.new(data, config)}.to raise_error(IiifS3::Error::InvalidImageData)
24
+ end
25
+
26
+ end
27
+
28
+ context "basic data" do
29
+ before(:all) do
30
+ data = IiifS3::ImageRecord.new({
31
+ "path" => "./spec/data/test.jpg",
32
+ "id" => 1,
33
+ "page_number" => 1
34
+ })
35
+ config = IiifS3::Config.new
36
+ @img = IiifS3::ImageVariant.new(data, config, 100, 100)
37
+ end
38
+
39
+ it "has a uri" do
40
+ expect(@img.uri).to eq("#{@img.generate_image_id(1,1)}/full/83,/0/default.jpg")
41
+ end
42
+ it "has an id" do
43
+ expect(@img.id).to eq("#{@img.generate_image_id(1,1)}")
44
+ end
45
+ it "has a width" do
46
+ expect(@img.width).to eq(83)
47
+ end
48
+ it "has a height" do
49
+ expect(@img.height).to eq(100)
50
+ end
51
+ it "has a mime type" do
52
+ expect(@img.mime_type).to eq("image/jpeg")
53
+ end
54
+ end
55
+
56
+ context "Full Image" do
57
+ before(:all) do
58
+ data = IiifS3::ImageRecord.new({
59
+ "path" => "./spec/data/test.jpg",
60
+ "id" => 1,
61
+ "page_number" => 1
62
+ })
63
+ config = IiifS3::Config.new
64
+ @img = IiifS3::FullImage.new(data, config)
65
+ end
66
+ it "has the default filestring" do
67
+ expect(@img.uri).to include "full/full"
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+ require "base_properties_spec"
3
+
4
+
5
+ describe IiifS3::Manifest do
6
+ include_context("fake data")
7
+
8
+ context "base" do
9
+ before(:each) do
10
+ @object = m
11
+ end
12
+ it_behaves_like "base properties"
13
+ end
14
+
15
+ let (:config) {IiifS3::Config.new()}
16
+ let (:m) {IiifS3::Manifest.new([@fake_data],config)}
17
+ let (:output) {JSON.parse(m.to_json)}
18
+
19
+ it "initializes without an error" do
20
+ expect(m).to be_a(IiifS3::Manifest)
21
+ end
22
+
23
+
24
+ it "exports JSON-LD as a valid JSON string" do
25
+ expect(m.to_json).to be_a(String)
26
+ expect{JSON.parse(m.to_json)}.not_to raise_error
27
+ end
28
+
29
+ it "has a @context" do
30
+ expect(output["@context"]).to eq(IiifS3::PRESENTATION_CONTEXT)
31
+ end
32
+ it "has a @type" do
33
+ expect(output["@type"]).to eq(IiifS3::Manifest::TYPE)
34
+ end
35
+ it "has an @id" do
36
+ expect(output["@id"]).to eq("#{IiifS3::Config::DEFAULT_URL}/1/manifest.json")
37
+ end
38
+
39
+ context "error checking" do
40
+ it "throws an error if not provided ImageRecords" do
41
+ expect{IiifS3::Manifest.new(["not an image record"],config)}.to raise_error(IiifS3::Error::InvalidImageData)
42
+ end
43
+ it "throws an error unless there's a primary image" do
44
+ data = @fake_data.clone
45
+ data.is_primary = false
46
+ expect{IiifS3::Manifest.new([data],config)}.to raise_error(IiifS3::Error::InvalidImageData)
47
+ end
48
+ it "throws an error if there are two primary images" do
49
+ data1 = IiifS3::ImageRecord.new({is_primary: true})
50
+ data2 = IiifS3::ImageRecord.new({is_primary: true})
51
+
52
+ expect{IiifS3::Manifest.new([data1, data2],config)}.to raise_error(IiifS3::Error::MultiplePrimaryImages)
53
+ end
54
+ end
55
+
56
+ context "config variables" do
57
+ let (:config) {IiifS3::Config.new({:use_extensions => false})}
58
+ it "the @id has an extension if configured thusly" do
59
+ expect(output["@id"]).to eq("#{IiifS3::Config::DEFAULT_URL}/1/manifest")
60
+ end
61
+ end
62
+
63
+ context "base_url config variable" do
64
+ let (:config) {IiifS3::Config.new({base_url: "http://www.example.com"})}
65
+ it "uses non-test uris" do
66
+ expect(output["@id"]).to eq("http://www.example.com/1/manifest.json")
67
+ end
68
+ end
69
+
70
+ context "spec requirements" do
71
+ it "has a label" do
72
+ expect(output["label"].length).to be > 0
73
+ end
74
+ it "does not have a format" do
75
+ expect(output["format"]).to be_nil
76
+ end
77
+ it "does not have a height" do
78
+ expect(output["height"]).to be_nil
79
+ end
80
+ it "does not have a width" do
81
+ expect(output["width"]).to be_nil
82
+ end
83
+ it "does not have a startCanvas" do
84
+ expect(output["startCanvas"]).to be_nil
85
+ end
86
+ it "accepts valid viewing directions" do
87
+ dir = "right-to-left"
88
+ new_data = @fake_data
89
+ new_data.viewing_direction = dir
90
+ m = IiifS3::Manifest.new([new_data],config)
91
+ o = JSON.parse(m.to_json)
92
+ expect(o["viewingDirection"]).to eq(dir)
93
+
94
+ end
95
+ end
96
+
97
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe IiifS3::Utilities::PdfSplitter do
5
+ context "comparing" do
6
+ it "generates the proper number of files" do
7
+ skip("skipping expensive tests") if ENV["SKIP_EXPENSIVE_TESTS"]
8
+ Dir.mktmpdir do |dir|
9
+ results = IiifS3::Utilities::PdfSplitter.split("./spec/data/test.pdf", output_dir: dir)
10
+ expect(results.count).to eq(3)
11
+ results.each do |file|
12
+ File.delete(file)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,115 @@
1
+ RSpec.shared_context "fake variants" do
2
+ before(:example) do
3
+ @fake_variants = {
4
+ "full" => OpenStruct.new(:id => "http://www.example.com/images/1", :width => 1000, :height => 1200),
5
+ "thumbnail" => OpenStruct.new(:id => "http://www.example.com/images/1", :width => 100, :height => 120)
6
+ }
7
+ @fake_image_info = '{
8
+ "@context": "http://iiif.io/api/image/2/context.json",
9
+ "@id": "http://www.example.com/test/1",
10
+ "protocol": "http://iiif.io/api/image",
11
+ "width": 1000,
12
+ "height": 1200,
13
+ "sizes": [
14
+ {
15
+ "width": 1000,
16
+ "height": 1200
17
+ },
18
+ {
19
+ "width": 100,
20
+ "height": 120
21
+ }
22
+ ],
23
+ "profile": [
24
+ "http://iiif.io/api/image/2/level0.json",
25
+ {
26
+ "supports": [
27
+ "cors",
28
+ "sizeByWhListed",
29
+ "baseUriRedirect"
30
+ ]
31
+ }
32
+ ]
33
+ }'
34
+ end
35
+ end
36
+
37
+ RSpec.shared_context "fake data" do
38
+ include_context("fake variants")
39
+ before(:example) do
40
+
41
+ @fake_collection = '{
42
+ "@context": "http://iiif.io/api/presentation/2/context.json",
43
+ "@id": "http://0.0.0.0/collection/name.json",
44
+ "@type": "sc:Collection",
45
+ "label": "Test Data",
46
+ "manifests": [
47
+ {
48
+ "@id": "http://0.0.0.0/1/manifest.json",
49
+ "@type": "sc:Manifest",
50
+ "label": "test label"
51
+ }
52
+ ]
53
+ }'
54
+
55
+ @fake_data = ImageRecord.new({
56
+ "id" => 1,
57
+ "page_number" => "1",
58
+ "image_path" => "./spec/data/test.jpg",
59
+ "is_primary" => true,
60
+ "variants" => @fake_variants,
61
+ "label" => "test label",
62
+ "attribution" => "All rights reserved",
63
+ "logo" => "http://www.example.com/logo.jpg"
64
+ })
65
+ @fake_manifest = '{
66
+ "@context": "http://iiif.io/api/presentation/2/context.json",
67
+ "@id": "http://0.0.0.0/1/manifest.json",
68
+ "@type": "sc:Manifest",
69
+ "label": "test label",
70
+ "attribution": "All rights reserved",
71
+ "logo": "http://www.example.com/logo.jpg",
72
+ "thumbnail": "http://www.example.com/images/1/full/100,/0/default.jpg",
73
+ "viewingDirection": "left-to-right",
74
+ "viewingHint": "individuals",
75
+ "sequences": [
76
+ {
77
+ "@id": "http://0.0.0.0/1/sequence/default.json",
78
+ "@type": "sc:Sequence",
79
+ "canvases": [
80
+ {
81
+ "@type": "sc:Canvas",
82
+ "@id": "http://0.0.0.0/1/canvas/front.json",
83
+ "label": "front",
84
+ "width": 2000,
85
+ "height": 2400,
86
+ "thumbnail": "http://www.example.com/images/1/full/100,/0/default.jpg",
87
+ "images": [
88
+ {
89
+ "@type": "oa:Annotation",
90
+ "@id": "http://0.0.0.0/1/annotation/front.json",
91
+ "motivation": "sc:painting",
92
+ "resource": {
93
+ "@id": "http://www.example.com/images/1/full/full/0/default.jpg",
94
+ "@type": "dcterms:Image",
95
+ "format": "image/jpeg",
96
+ "service": {
97
+ "@context": "http://iiif.io/api/image/2/context.json",
98
+ "@id": "http://www.example.com/images/1",
99
+ "profile": "http://iiif.io/api/image/2/level0.json"
100
+ },
101
+ "width": 1000,
102
+ "height": 1200
103
+ },
104
+ "on": "http://0.0.0.0/1/canvas/front.json"
105
+ }
106
+ ]
107
+ }
108
+ ]
109
+ }
110
+ ]
111
+ }'
112
+
113
+
114
+ end
115
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'dotenv'
3
+ Dotenv.load
4
+
5
+ require 'simplecov'
6
+ SimpleCov.start
7
+
8
+ require "shared_contexts"
9
+ require 'wax_iiif'
10
+
11
+ ENV["TEST_INTERNET_CONNECTIVITY"] ||= nil
12
+ ENV["SKIP_EXPENSIVE_TESTS"] ||= nil
data/test.rb ADDED
@@ -0,0 +1,77 @@
1
+ require 'dotenv'
2
+ Dotenv.load
3
+
4
+ require_relative 'lib/wax_iiif'
5
+
6
+ # Set up configuration variables
7
+ opts = {}
8
+ opts[:image_directory_name] = "img"
9
+ opts[:variants] = { "reference" => 600, "access" => 1200}
10
+ opts[:image_types] = [".jpg", ".tif", ".jpeg", ".tiff"]
11
+ opts[:document_file_types] = [".pdf"]
12
+
13
+ # Setup Temporary stores
14
+ @data = []
15
+ @cleanup_list = []
16
+ @dir = "./data"
17
+
18
+
19
+ def add_image(file, is_doc = false)
20
+ name = File.basename(file, File.extname(file))
21
+ name_parts = name.split("_")
22
+ is_paged = name_parts.length == 8
23
+ page_num = is_paged ? name_parts[7].to_i : 1
24
+ name_parts.pop if is_paged
25
+ id = name_parts.join("_")
26
+
27
+ obj = {
28
+ "path" => "#{file}",
29
+ "id" => id,
30
+ "label" => name_parts.join("."),
31
+ "is_master" => page_num == 1,
32
+ "page_number" => page_num,
33
+ "is_document" => false,
34
+ "description" => "This is a test file generated as part of the development on the ruby IiifS3 Gem. <b> This should be bold.</b>"
35
+ }
36
+
37
+ if is_paged
38
+ obj["section"] = "p#{page_num}"
39
+ obj["section_label"] = "Page #{page_num}"
40
+ end
41
+
42
+ if is_doc
43
+ obj["is_document"] = true
44
+ end
45
+ @data.push IiifS3::ImageRecord.new(obj)
46
+ end
47
+
48
+ def add_to_cleanup_list(img)
49
+ @cleanup_list.push(img)
50
+ end
51
+
52
+ def cleanup
53
+ @cleanup_list.each do |file|
54
+ File.delete(file)
55
+ end
56
+ end
57
+
58
+
59
+ iiif = IiifS3::Builder.new(opts)
60
+ iiif.create_build_directories
61
+
62
+ Dir.foreach(@dir) do |file|
63
+ if opts[:image_file_types].include? File.extname(file)
64
+ add_image("#{@dir}/#{file}")
65
+ elsif opts[:document_file_types].include? File.extname(file)
66
+ path = "#{@dir}/#{file}"
67
+ images = IiifS3::Utilities::PdfSplitter.split(path)
68
+ images.each do |img|
69
+ add_image(img, true)
70
+ add_to_cleanup_list(img)
71
+ end
72
+ end
73
+ end
74
+
75
+ iiif.load(@data)
76
+ iiif.process_data
77
+ cleanup