-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an endpoint for exposing the data as JSON
- Loading branch information
Showing
15 changed files
with
176 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
# Responds to queries about objects | ||
class QueriesController < ApplicationController | ||
before_action :load_item, only: [:collections] | ||
|
||
# Returns a list of collections this object is in. | ||
def collections | ||
# If we move to Valkyrie this can be find_inverse_references_by | ||
@collections = @item.collections.map { |collection| Cocina::Mapper.build(collection) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cocina | ||
# A digital repository object. See https://github.com/sul-dlss-labs/taco/blob/master/maps/DRO.json | ||
class DRO < Dry::Struct | ||
attribute :externalIdentifier, Types::Strict::String | ||
attribute :type, Types::Strict::String | ||
attribute :label, Types::Strict::String | ||
|
||
def as_json(*) | ||
{ | ||
externalIdentifier: externalIdentifier, | ||
type: type, | ||
label: label | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cocina | ||
# This defines the data types supported by the model | ||
module Types | ||
include Dry.Types() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cocina | ||
# Maps Dor::Items to Cocina objects | ||
class Mapper | ||
def self.build(item) | ||
new(item).build | ||
end | ||
|
||
def initialize(item) | ||
@item = item | ||
end | ||
|
||
def build | ||
Cocina::DRO.new(externalIdentifier: item.pid, | ||
type: type, | ||
label: item.label) | ||
end | ||
|
||
private | ||
|
||
attr_reader :item | ||
|
||
# @todo This should have more speicific type such as found in identityMetadata.objectType | ||
def type | ||
case item | ||
when Dor::Item | ||
'object' | ||
when Dor::Collection | ||
'collection' | ||
else | ||
raise "Unknown type for #{item.class}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.collections @collections, :externalIdentifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'Get the object' do | ||
let(:payload) { { sub: 'argo' } } | ||
let(:jwt) { JWT.encode(payload, Settings.dor.hmac_secret, 'HS256') } | ||
let(:basic_auth) { ActionController::HttpAuthentication::Basic.encode_credentials(user, password) } | ||
let(:object) { instance_double(Dor::Item, collections: [collection]) } | ||
let(:collection_id) { 'druid:999123' } | ||
let(:collection) do | ||
Dor::Collection.new(pid: collection_id, label: 'collection #1') | ||
end | ||
|
||
before do | ||
allow(Dor).to receive(:find).and_return(object) | ||
end | ||
|
||
describe 'as used by WAS crawl seed registration' do | ||
it 'returns (at a minimum) the identifiers of the collections ' do | ||
get '/v1/objects/druid:mk420bs7601/query/collections', | ||
headers: { 'X-Auth' => "Bearer #{jwt}" } | ||
expect(response).to be_successful | ||
expect(response.body).to eq '{"collections":[{"externalIdentifier":"druid:999123"}]}' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'Get the object' do | ||
let(:payload) { { sub: 'argo' } } | ||
let(:jwt) { JWT.encode(payload, Settings.dor.hmac_secret, 'HS256') } | ||
let(:basic_auth) { ActionController::HttpAuthentication::Basic.encode_credentials(user, password) } | ||
let(:object) { Dor::Item.new(pid: 'druid:1234') } | ||
|
||
before do | ||
allow(Dor).to receive(:find).and_return(object) | ||
end | ||
|
||
context 'when the object exists' do | ||
before do | ||
object.descMetadata.title_info.main_title = 'Hello' | ||
object.label = 'foo' | ||
end | ||
|
||
it 'returns the object' do | ||
get '/v1/objects/druid:mk420bs7601', | ||
headers: { 'X-Auth' => "Bearer #{jwt}" } | ||
expect(response).to be_successful | ||
expect(response.body).to eq '{"externalIdentifier":"druid:1234","type":"object","label":"foo"}' | ||
end | ||
end | ||
end |