Skip to content

Commit

Permalink
Merge 6f06689 into 8923d60
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Sep 3, 2019
2 parents 8923d60 + 6f06689 commit d7c4d8d
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .rubocop.yml
Expand Up @@ -35,3 +35,7 @@ RSpec/MultipleExpectations:
RSpec/ExpectActual:
Exclude:
- 'spec/routing/**'

RSpec/DescribeClass:
Exclude:
- 'spec/requests/**'
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -36,6 +36,8 @@ gem 'uuidtools', '~> 2.1.4'

# DLSS/domain-specific dependencies
gem 'dor-services', '~> 7.0'
gem 'dry-struct'
gem 'dry-types'

group :test, :development do
gem 'coveralls', '~> 0.8', require: false
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Expand Up @@ -173,6 +173,11 @@ GEM
dry-initializer (~> 3.0)
dry-logic (~> 1.0)
dry-types (~> 1.0)
dry-struct (1.0.0)
dry-core (~> 0.4, >= 0.4.3)
dry-equalizer (~> 0.2)
dry-types (~> 1.0)
ice_nine (~> 0.11)
dry-types (1.1.1)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
Expand Down Expand Up @@ -205,6 +210,7 @@ GEM
domain_name (~> 0.5)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
iso-639 (0.2.8)
jaro_winkler (1.5.3)
json (2.2.0)
Expand Down Expand Up @@ -448,6 +454,8 @@ DEPENDENCIES
coveralls (~> 0.8)
dlss-capistrano
dor-services (~> 7.0)
dry-struct
dry-types
equivalent-xml
faraday
honeybadger
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/objects_controller.rb
Expand Up @@ -46,6 +46,10 @@ def update
head :no_content
end

def show
render json: Cocina::Mapper.build(@item)
end

def publish
PublishMetadataService.publish(@item)
head :created
Expand Down
18 changes: 18 additions & 0 deletions app/models/cocina/dro.rb
@@ -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
8 changes: 8 additions & 0 deletions app/models/cocina/types.rb
@@ -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
36 changes: 36 additions & 0 deletions app/services/cocina/mapper.rb
@@ -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
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -20,7 +20,7 @@
get 'catkey', to: 'marcxml#catkey'
end

resources :objects, only: [:create, :update] do
resources :objects, only: [:create, :update, :show] do
member do
post 'publish'
post 'update_embargo'
Expand Down
23 changes: 23 additions & 0 deletions spec/requests/show_object_spec.rb
@@ -0,0 +1,23 @@
# 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
object.descMetadata.title_info.main_title = 'Hello'
object.label = 'foo'
allow(Dor).to receive(:find).and_return(object)
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

0 comments on commit d7c4d8d

Please sign in to comment.