diff --git a/.rubocop.yml b/.rubocop.yml index 09cc71b4..dfb8ba9c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,6 +6,7 @@ Rails/FilePath: Layout/LineLength: Exclude: - "config/initializers/content_security_policy.rb" + - "spec/services/card_image_loading_service_spec.rb" Metrics/BlockLength: Exclude: diff --git a/app/models/card_image.rb b/app/models/card_image.rb new file mode 100644 index 00000000..105d243a --- /dev/null +++ b/app/models/card_image.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class CardImage < ApplicationRecord +end diff --git a/app/models/sub_guide_card.rb b/app/models/sub_guide_card.rb index 58375a04..847b8446 100644 --- a/app/models/sub_guide_card.rb +++ b/app/models/sub_guide_card.rb @@ -7,7 +7,5 @@ def parent GuideCard.find_by(sortid: parentid) || SubGuideCard.find_by(sortid: parentid) end include HasChildren - def url - 'https://puliiif.princeton.edu/iiif/2/imagecat-disk5-0338-A5977-0000.0073/full/,500/0/default.jpg' - end + def image_urls; end end diff --git a/app/services/card_image_loading_service.rb b/app/services/card_image_loading_service.rb new file mode 100644 index 00000000..b44028e4 --- /dev/null +++ b/app/services/card_image_loading_service.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +# Class for card image loading service +class CardImageLoadingService + # For each SubGuideCard, take its path and query s3 to get all of the image names + # for that path. For each image file, create a CardImage object with the path and + # image name. + def import + SubGuideCard.all.each do |sgc| + ci = CardImage.new + ci.path = sgc.path + ci.save + end + end +end diff --git a/db/migrate/20230810192642_create_card_images.rb b/db/migrate/20230810192642_create_card_images.rb new file mode 100644 index 00000000..36940267 --- /dev/null +++ b/db/migrate/20230810192642_create_card_images.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# db migration for card image loading service +class CreateCardImages < ActiveRecord::Migration[7.0] + def change + create_table :card_images do |t| + t.text :path + t.text :image_name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index a036dd8d..d7b7a8f0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,10 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_06_02_135223) do +ActiveRecord::Schema[7.0].define(version: 2023_08_10_192642) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "card_images", force: :cascade do |t| + t.text "path" + t.text "image_name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "guide_cards", force: :cascade do |t| t.string "heading" t.string "sortid" diff --git a/spec/fixtures/s3/imagecat-disk9-0091-A3037-1358.0111.tif b/spec/fixtures/s3/imagecat-disk9-0091-A3037-1358.0111.tif new file mode 100644 index 00000000..a198ed26 Binary files /dev/null and b/spec/fixtures/s3/imagecat-disk9-0091-A3037-1358.0111.tif differ diff --git a/spec/models/card_image_spec.rb b/spec/models/card_image_spec.rb new file mode 100644 index 00000000..2a752176 --- /dev/null +++ b/spec/models/card_image_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe CardImage, type: :model do + it 'can be instantiated' do + ci = CardImage.new + ci.path = '5/0338/A5977' + ci.image_name = 'imagecat-disk5-0338-A5977-0000.0001.tif' + ci.save + end +end diff --git a/spec/models/sub_guide_card_spec.rb b/spec/models/sub_guide_card_spec.rb index 42b99108..2efb0749 100644 --- a/spec/models/sub_guide_card_spec.rb +++ b/spec/models/sub_guide_card_spec.rb @@ -46,17 +46,17 @@ end end - context 'when we use a card from disk5 with path 5/0338/A5977' do - it 'returns a URL' do - sub_guide_image = SubGuideCard.create(path: '5/0338/A5977') - expect(sub_guide_image.url).to eq 'https://puliiif.princeton.edu/iiif/2/imagecat-disk5-0338-A5977-0000.0073/full/,500/0/default.jpg' + context 'when we display a subguide whose path is 5/0338/A5977' do + xit 'has an array of image urls' do + sub_guide_card = SubGuideCard.create(path: '5/0338/A5977') + expect(sub_guide_card.image_urls.count).to eq 169 end end - context 'when we use a card from disk4 with path 4/1466/B5962' do - xit 'returns a URL' do - sub_guide_image = SubGuideCard.create(path: '4/1466/B5962') - expect(sub_guide_image.url).to eq 'https://puliiif.princeton.edu/iiif/2/imagecat-disk4-1466-B5962-0000.0064/full/,500/0/default.jpg' + context 'when we display a subguide whose path is 4/1466/B5962' do + xit 'has an array of image urls' do + sub_guide_card = SubGuideCard.create(path: '4/1466/B5962') + expect(sub_guide_card.image_urls.count).to eq 102 end end end diff --git a/spec/services/card_image_loading_service_spec.rb b/spec/services/card_image_loading_service_spec.rb new file mode 100644 index 00000000..97f52179 --- /dev/null +++ b/spec/services/card_image_loading_service_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe CardImageLoadingService do + let(:cils) { described_class.new } + let(:sgls) do + SubGuideLoadingService.new(csv_location: Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv')) + end + let(:s3_response) do + "2023-07-19 14:39:38 3422 imagecat-disk9-0091-A3037-1358.0110.tif\n2023-07-19 14:39:38 7010 imagecat-disk9-0091-A3037-1358.0111.tif\n" + end + it 'can instantiate' do + expect(cils).to be_instance_of described_class + end + + it 'imports all card images' do + sgls.import + expect(CardImage.count).to eq 0 + cils.import + expect(CardImage.count).to eq 6 + end +end