Skip to content

Commit

Permalink
Merge pull request #8 from anusharanganathan/feature_iiif_manifest_data
Browse files Browse the repository at this point in the history
Feature iiif manifest data
  • Loading branch information
mejackreed committed Dec 16, 2015
2 parents f557286 + 1c2bfd5 commit 2c5dabf
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
65 changes: 65 additions & 0 deletions app/models/concerns/iiif_manifest_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module IiifManifestData
include JsonReader
extend ActiveSupport::Concern

attr_accessor :manifest_url, :manifest, :modsxml

def read_manifest
return nil unless self.manifest_url
self.manifest = JsonReader::Reader.new.from_url(self.manifest_url)
end

def title
return nil unless self.manifest_url
self.read_manifest unless self.manifest
return nil unless self.manifest
self.manifest["label"]
end

def druid
#druid is a 11 digit alphanumeric sring
return nil unless self.manifest_url
self.read_manifest unless self.manifest
return nil unless self.manifest
self.manifest["@id"].match( /\/([a-zA-Z0-9]{11})\// ).to_s.gsub('/', '')
end

def mods_url
return nil unless self.manifest_url
self.read_manifest unless self.manifest
return nil unless self.manifest
return nil unless self.manifest.has_key?("seeAlso")
if self.manifest["seeAlso"]["dcterms:format"] == "application/mods+xml"
return self.manifest["seeAlso"]["@id"]
elsif self.manifest["seeAlso"]["format"] == "application/mods+xml"
return self.manifest["seeAlso"]["@id"]
end
return nil
end

def annotation_lists
return [] unless self.manifest_url
self.read_manifest unless self.manifest
return [] unless self.manifest
annotations = []
self.manifest["sequences"].each do |s|
s["canvases"].each do |c|
if c.has_key?("otherContent")
annotations = annotations + c["otherContent"].select {|item| item["@type"] == "sc:AnnotationList"}.map{|item| item['label'] = c["label"]; item}
end
end
end
annotations
end

def get_modsxml
self.modsxml = nil
url = self.mods_url
return nil unless url
uri = URI.parse(url)
uri.scheme = "https"
require 'open-uri'
self.modsxml = open(uri.to_s).read
end

end
7 changes: 7 additions & 0 deletions app/models/iiif_manifest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class IiifManifest
include IiifManifestData

def initialize(manifest_url=nil)
self.manifest_url = manifest_url if manifest_url
end
end
18 changes: 18 additions & 0 deletions spec/fixtures/iiif_manifest_records/iiif_manifest_fixtures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# encoding: UTF-8
module IiifManifestFixtures
def manifest_url_001
'http://dms-data.stanford.edu/data/manifests/Stanford/kq131cs7229/manifest.json'
end

def manifest_url_002
'http://dms-data.stanford.edu/data/manifests/BnF/jr903ng8662/manifest.json'
end

def manifest_url_003
'http://dms-data.stanford.edu/data/manifests/Parker/fh878gz0315/manifest.json'
end

def manifest_url_004
'https://purl.stanford.edu/bb389yg4719/iiif/manifest.json'
end
end
6 changes: 6 additions & 0 deletions spec/fixtures/iiif_manifest_records/manifest_urls.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
https://purl.stanford.edu/bb389yg4719/iiif/manifest.json
https://purl.stanford.edu/bb475kg5932/iiif/manifest.json
https://purl.stanford.edu/bc991ym6224/iiif/manifest.json
https://purl.stanford.edu/bk254dq8652/iiif/manifest.json
https://purl.stanford.edu/bn235bx5422/iiif/manifest.json
https://purl.stanford.edu/bn247br1244/iiif/manifest.json
127 changes: 127 additions & 0 deletions spec/models/concerns/iiif_manifest_data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
require "spec_helper"

class IiifManifestDataTestClass
include IiifManifestData
end

describe IiifManifestData do
include IiifManifestFixtures
before(:all) do
@document_empty = IiifManifest.new()
@document_empty.read_manifest
@document = IiifManifest.new(manifest_url=manifest_url_001)
@document.read_manifest
@document2 = IiifManifest.new(manifest_url=manifest_url_004)
@document2.read_manifest
end

describe "#read_manifest" do
it "should be nil if no manifest url" do
expect(@document_empty.manifest).to be_nil
end

it "should be a Hash" do
expect(@document.manifest).to be_a Hash
expect(@document2.manifest).to be_a Hash
end

it "should not be an empty Hash" do
expect(@document.manifest).not_to be_empty
expect(@document2.manifest).not_to be_empty
end
end

describe "#title" do
it "should be nil if no manifest url" do
expect(@document_empty.title).to be_nil
end

it "should be a string" do
expect(@document.title).to be_a String
expect(@document2.title).to be_a String
end

it "should be a title string" do
expect(@document.title).to eq("Manuscript fragment of the Gospels and Canonical Epistles, glossed")
expect(@document2.title).to eq("Stanford University Libraries, M1814. Flat Box 2, Folder 02")
end
end

describe "#druid" do
it "should be nil if no manifest url" do
expect(@document_empty.druid).to be_nil
end

it "should be a string" do
expect(@document.druid).to be_a String
expect(@document2.druid).to be_a String
end

it "should be a druid string" do
expect(@document.druid).to eq("kq131cs7229")
expect(@document2.druid).to eq("bb389yg4719")
end
end

describe "#mods_url" do
it "should be nil if no manifest url" do
expect(@document_empty.mods_url).to be_nil
end

it "should be a string" do
expect(@document.mods_url).to be_a String
expect(@document2.mods_url).to be_a String
end

it "should be a mods url string" do
expect(@document.mods_url).to eq("http://purl.stanford.edu/kq131cs7229.mods")
expect(@document2.mods_url).to eq("https://purl.stanford.edu/bb389yg4719.mods")
end
end

describe "#annotation_lists" do
it "should be an array" do
expect(@document_empty.annotation_lists).to be_a Array
expect(@document.annotation_lists).to be_a Array
expect(@document2.annotation_lists).to be_a Array
end

it "should be an empty array if no manifest url" do
expect(@document_empty.annotation_lists).to be_empty
expect(@document2.annotation_lists).to be_empty
end

it "should not be an empty array" do
expect(@document.annotation_lists).not_to be_empty
end

it "should be of length 36" do
expect(@document.annotation_lists.length).to eq(36)
end

it "should be an array of hashes" do
expect(@document.annotation_lists.any? {|hash| hash.keys == ['@id', '@type', 'label']}).to be_true
end
end

describe "#get_modsxml" do
before(:all) do
@document.get_modsxml
@document2.get_modsxml
end

it "should be nil if no manifest url" do
expect(@document_empty.title).to be_nil
end

it "should be a String" do
expect(@document.modsxml).to be_a String
expect(@document2.modsxml).to be_a String
end

it "should be a xml doc" do
expect(@document.modsxml).to start_with ("<?xml")
expect(@document2.modsxml).to start_with ("<?xml")
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#require 'fixtures/marc_records/marc_metadata_fixtures'
require 'fixtures/mods_records/mods_fixtures'
require 'fixtures/annotation_records/annotation_fixtures'
require 'fixtures/iiif_manifest_records/iiif_manifest_fixtures'

Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {timeout: 60})
Expand Down

0 comments on commit 2c5dabf

Please sign in to comment.