-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
206 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# frozen_string_literal: true | ||
|
||
module Chemotion | ||
class DeviceDescriptionAPI < Grape::API | ||
resource :device_descriptions do | ||
# create a device description | ||
params do | ||
optional :name, type: String | ||
optional :short_label, type: String | ||
optional :vendor_name, type: String | ||
optional :vendor_id, type: String | ||
optional :vendor_url, type: String | ||
optional :serial_number, type: String | ||
optional :doi, type: String | ||
optional :doi_url, type: String | ||
optional :device_type, type: String | ||
optional :device_type_detail, type: String | ||
optional :operation_mode, type: String | ||
optional :installation_start_date, type: DateTime, allow_blank: true | ||
optional :installation_end_date, type: DateTime, allow_blank: true | ||
optional :description_and_comments, type: String | ||
optional :technical_operator, type: Hash | ||
optional :administrative_operator, type: Hash | ||
optional :university_campus, type: String | ||
optional :institute, type: String | ||
optional :building, type: String | ||
optional :room, type: String | ||
optional :infrastructure_assignment, type: String | ||
optional :access_options, type: String | ||
optional :comments, type: String | ||
optional :size, type: String | ||
optional :weight, type: String | ||
optional :application_name, type: String | ||
optional :application_version, type: String | ||
optional :description_for_method_part, type: String | ||
end | ||
post do | ||
attributes = declared(params, include_missing: false) | ||
device_description = DeviceDescription.create!(attributes) | ||
|
||
present device_description, with: Entities::DeviceDescriptionEntity, root: :device_description | ||
end | ||
|
||
# Return serialized device description by id | ||
params do | ||
requires :id, type: Integer, desc: 'Device description id' | ||
end | ||
route_param :id do | ||
get do | ||
device_description = DeviceDescription.find(params[:id]) | ||
|
||
present device_description, with: Entities::DeviceDescriptionEntity, root: :device_description | ||
end | ||
end | ||
|
||
# update a device description | ||
params do | ||
requires :id, type: Integer | ||
optional :device_id, type: Integer, description: 'Linked device' | ||
optional :name, type: String | ||
optional :short_label, type: String | ||
optional :vendor_name, type: String | ||
optional :vendor_id, type: String | ||
optional :vendor_url, type: String | ||
optional :serial_number, type: String | ||
optional :doi, type: String | ||
optional :doi_url, type: String | ||
optional :device_type, type: String | ||
optional :device_type_detail, type: String | ||
optional :operation_mode, type: String | ||
optional :installation_start_date, type: DateTime, allow_blank: true | ||
optional :installation_end_date, type: DateTime, allow_blank: true | ||
optional :description_and_comments, type: String | ||
optional :technical_operator, type: Hash | ||
optional :administrative_operator, type: Hash | ||
optional :university_campus, type: String | ||
optional :institute, type: String | ||
optional :building, type: String | ||
optional :room, type: String | ||
optional :infrastructure_assignment, type: String | ||
optional :access_options, type: String | ||
optional :comments, type: String | ||
optional :size, type: String | ||
optional :weight, type: String | ||
optional :application_name, type: String | ||
optional :application_version, type: String | ||
optional :description_for_methods_part, type: String | ||
end | ||
put ':id' do | ||
device_description = DeviceDescription.find(params[:id]) | ||
attributes = declared(params, include_missing: false) | ||
device_description.update!(attributes) | ||
|
||
present device_description, with: Entities::DeviceDescriptionEntity, root: :device_description | ||
end | ||
|
||
# delete a device description | ||
delete ':id' do | ||
device_description = DeviceDescription.find(params[:id]) | ||
error!('Device could not be deleted', 400) unless device_description.present? && device_description.destroy | ||
|
||
{ deleted: device_description.id } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Entities | ||
class DeviceDescriptionEntity < Grape::Entity | ||
expose :id | ||
expose :device_id | ||
expose :name | ||
expose :short_label | ||
expose :vendor_name | ||
expose :vendor_id | ||
expose :vendor_url | ||
expose :serial_number | ||
expose :doi | ||
expose :doi_url | ||
expose :device_type | ||
expose :device_type_detail | ||
expose :operation_mode | ||
expose :installation_start_date | ||
expose :installation_end_date | ||
expose :description_and_comments | ||
expose :technical_operator | ||
expose :administrative_operator | ||
expose :university_campus | ||
expose :institute | ||
expose :building | ||
expose :room | ||
expose :infrastructure_assignment | ||
expose :access_options | ||
expose :comments | ||
expose :size | ||
expose :weight | ||
expose :application_name | ||
expose :application_version | ||
expose :description_for_methods_part | ||
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,54 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Chemotion::DeviceDescriptionAPI do | ||
include_context 'api request authorization context' | ||
|
||
let(:device_description) { create(:device_description) } | ||
|
||
describe 'POST /api/v1/device_descriptions' do | ||
let(:device_description_params) { attributes_for(:device_description) } | ||
|
||
it 'creates a device description' do | ||
post '/api/v1/device_descriptions', params: device_description_params | ||
|
||
expect(parsed_json_response['device_description']['short_label']).to include('DD') | ||
end | ||
end | ||
|
||
describe 'GET /api/v1/device_descriptions/:id' do | ||
before do | ||
device_description | ||
end | ||
|
||
it 'fetches an device description by id' do | ||
get "/api/v1/device_descriptions/#{device_description.id}" | ||
|
||
expect(parsed_json_response['device_description']['name']).to eql(device_description.name) | ||
end | ||
end | ||
|
||
describe 'PUT /api/v1/device_descriptions/:id' do | ||
context 'when updating an device description' do | ||
let(:params) do | ||
{ | ||
name: 'new name', | ||
short_label: 'DD1-2', | ||
} | ||
end | ||
|
||
it 'returns the updated device description' do | ||
put "/api/v1/device_descriptions/#{device_description.id}", params: params | ||
|
||
expect(parsed_json_response['device_description']).to include(params.stringify_keys) | ||
end | ||
end | ||
end | ||
|
||
describe 'DELETE /api/v1/device_descriptions/:id' do | ||
it 'deletes a device description' do | ||
delete "/api/v1/device_descriptions/#{device_description.id}" | ||
|
||
expect(parsed_json_response).to include('deleted' => device_description.id) | ||
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :device_description do | ||
sequence(:name) { |i| "Device description #{i}" } | ||
sequence(:short_label) { |i| "DD#{i}" } | ||
serial_number { '123abc456def' } | ||
end | ||
end |