Skip to content

Commit

Permalink
Allow applications to mount multiple RIIIF endpoints for different mo…
Browse files Browse the repository at this point in the history
…dels
  • Loading branch information
cbeer committed Feb 11, 2015
1 parent 243026c commit 8088332
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 28 deletions.
19 changes: 11 additions & 8 deletions app/controllers/riiif/images_controller.rb
Expand Up @@ -3,11 +3,11 @@ class ImagesController < ::ApplicationController
before_filter :link_header, only: [:show, :info]
def show
begin
image = Image.new(params[:id])
image = model.new(image_id)
status = :ok
rescue ImageNotFoundError
if Riiif.not_found_image.present?
image = Riiif::Image.new(params[:id], Riiif::File.new(Riiif.not_found_image))
image = model.new(image_id, Riiif::File.new(Riiif.not_found_image))
status = :not_found
else
raise
Expand All @@ -18,17 +18,20 @@ def show
end

def info
image = Image.new(params[:id])
image = model.new(image_id)
render json: image.info.merge(server_info)
end

def view
@image = Image.new(params[:id])
end

protected

LEVEL2 = 'http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level2'
def model
params.fetch(:model, "riiif/image").camelize.constantize
end

def image_id
params[:id]
end

def link_header
response.headers["Link"] = "<#{LEVEL2}>;rel=\"profile\""
Expand All @@ -38,7 +41,7 @@ def server_info
{
"@context" => "http://library.stanford.edu/iiif/image-api/1.1/context.json",
"@id" => request.original_url.sub('/info.json', ''),
"formats" => Image::OUTPUT_FORMATS,
"formats" => model::OUTPUT_FORMATS,
"profile" => "#{LEVEL2}"

}
Expand Down
10 changes: 1 addition & 9 deletions config/routes.rb
@@ -1,11 +1,3 @@
Riiif::Engine.routes.draw do
ALLOW_DOTS ||= /[\w.]+/
SIZES ||= /(pct:)?[\w.,]+/
get "/:id/:region/:size/:rotation/:quality.:format" => "images#show",
constraints: { rotation: ALLOW_DOTS, size: SIZES },
defaults: { format: 'jpg', rotation: '0', region: 'full', quality: 'native' },
as: 'image'

get "/:id/info.json" => "images#info", defaults: { format: 'json' }, as: 'info'
get "/:id/view(.:format)" => "images#view"
iiif_for 'riiif/image'
end
1 change: 1 addition & 0 deletions lib/riiif.rb
Expand Up @@ -6,6 +6,7 @@ module Riiif
autoload :Image
autoload :FileSystemFileResolver
autoload :HTTPFileResolver
autoload :Routes

class Error < RuntimeError; end
class InvalidAttributeError < Error; end
Expand Down
2 changes: 1 addition & 1 deletion lib/riiif/engine.rb
@@ -1,6 +1,6 @@
module Riiif
class Engine < ::Rails::Engine
isolate_namespace Riiif
require 'riiif/rails/routes'

# How long to cache the tiles for.
config.cache_duration_in_days = 3
Expand Down
12 changes: 12 additions & 0 deletions lib/riiif/rails/routes.rb
@@ -0,0 +1,12 @@
module ActionDispatch::Routing
class Mapper
# example
# iiif_for :image
def iiif_for(*resources)
options = resources.extract_options!

Riiif::Routes.new(self, options.merge(resource: resources.first)).draw

end
end
end
29 changes: 29 additions & 0 deletions lib/riiif/routes.rb
@@ -0,0 +1,29 @@
module Riiif
class Routes
ALLOW_DOTS ||= /[\w.]+/
SIZES ||= /(pct:)?[\w.,]+/

def initialize(router, options)
@router = router
@options = options
end

def add_routes &blk
@router.instance_exec(@options, &blk)
end

def draw
add_routes do |options|
resource = options.fetch(:resource)
route_prefix = options[:at]
route_prefix ||= "/#{options[:as]}" if options[:as]
get "#{route_prefix}/:id/:region/:size/:rotation/:quality.:format" => "riiif/images#show",
constraints: { rotation: ALLOW_DOTS, size: SIZES },
defaults: { format: 'jpg', rotation: '0', region: 'full', quality: 'native', model: resource },
as: options[:as] || "image"

get "#{route_prefix}/:id/info.json" => "riiif/images#info", defaults: { format: 'json', model: resource }, as: [options[:as], "info"].compact.join("_")
end
end
end
end
2 changes: 1 addition & 1 deletion spec/controllers/images_controller_spec.rb
Expand Up @@ -25,7 +25,7 @@
expect(response).to be_successful
json = JSON.parse(response.body)
expect(json).to eq "@context"=>"http://library.stanford.edu/iiif/image-api/1.1/context.json",
"@id" =>"http://test.host/image-service/abcd1234",
"@id" =>"http://test.host/abcd1234",
"width" =>6000,
"height" =>4000,
"formats" => [ "jpg", "png" ],
Expand Down
16 changes: 8 additions & 8 deletions spec/routing/resize_routes_spec.rb
Expand Up @@ -9,38 +9,38 @@
get: "/abcd1234/full/full/0/native.jpg"
).to route_to(controller: "riiif/images", id: 'abcd1234', action: "show",
region: 'full', size: 'full', rotation: '0',
quality: 'native', format: 'jpg')
quality: 'native', format: 'jpg', model: "riiif/image")
end

it "routes requests with floating point percent size" do
expect(
get: "/abcd1234/full/pct:12.5/22.5/native.jpg"
).to route_to(controller: "riiif/images", id: 'abcd1234', action: "show",
region: 'full', size: 'pct:12.5', rotation: '22.5',
quality: 'native', format: 'jpg')
quality: 'native', format: 'jpg', model: "riiif/image")
end
it "routes requests with pixel size" do
expect(
get: "/abcd1234/full/100,50/22.5/native.jpg"
).to route_to(controller: "riiif/images", id: 'abcd1234', action: "show",
region: 'full', size: '100,50', rotation: '22.5',
quality: 'native', format: 'jpg')
quality: 'native', format: 'jpg', model: "riiif/image")
end
it "routes requests with dashes in the id" do
expect(
get: "/abcd-1234-5678/full/full/0/native.jpg"
).to route_to(controller: "riiif/images", id: 'abcd-1234-5678', action: "show",
region: 'full', size: 'full', rotation: '0',
quality: 'native', format: 'jpg')
quality: 'native', format: 'jpg', model: "riiif/image")
end

describe "route helper" do
it "takes all the options" do
expect(image_path('abcd1234', region: 'full', size: '100,50', rotation: '22.5', quality: 'native',
format: 'jpg')).to eq '/image-service/abcd1234/full/100,50/22.5/native.jpg'
format: 'jpg')).to eq '/abcd1234/full/100,50/22.5/native.jpg'
end
it "has defaults" do
expect(image_path('abcd1234', size: '100,50')).to eq '/image-service/abcd1234/full/100,50/0/native.jpg'
expect(image_path('abcd1234', size: '100,50')).to eq '/abcd1234/full/100,50/0/native.jpg'
end
end
end
Expand All @@ -50,10 +50,10 @@
expect(
get: "/abcd1234/info.json"
).to route_to(controller: "riiif/images", id: 'abcd1234',
action: "info", format: 'json')
action: "info", format: 'json', model: "riiif/image")
end
it "should have a route helper" do
expect(info_path('abcd1234')).to eq '/image-service/abcd1234/info.json'
expect(info_path('abcd1234')).to eq '/abcd1234/info.json'
end
end
end
Expand Up @@ -4,7 +4,7 @@ class TestAppGenerator < Rails::Generators::Base
source_root "spec/test_app_templates"

def add_routes
route "mount Riiif::Engine => '/image-service'"
route "iiif_for 'riiif/image', at: '/image-service'"
end

end

0 comments on commit 8088332

Please sign in to comment.