Add ImportProtocolService

This commit is contained in:
Urban Rotnik 2019-05-30 20:24:11 +02:00
parent 2bb4f558b7
commit a15596a126
3 changed files with 105 additions and 0 deletions

View file

@ -0,0 +1,62 @@
# frozen_string_literal: true
module Protocols
class ImportProtocolFromClientService
extend Service
attr_reader :errors, :pio_protocol
def initialize(protocol_client_id:, protocol_source:, user_id:, team_id:)
@id = protocol_client_id
@protocol_source = protocol_source
@user = User.find user_id
@team = Team.find team_id
@errors = {}
end
def call
return self unless valid?
# Catch api errors here, json invalid, also wrong/not found normalizer?
normalized_hash = client.load_protocol(id: @id)
# Catch api errors here
pio = ProtocolImporters::ProtocolIntermediateObject.new(normalized_json: normalized_hash,
user: @user,
team: @team)
@pio_protocol = pio.build
if @pio_protocol.valid?
# catch errors during import here
pio.import
# catch errors during import here
else
# Add AR errors here
# @errors[:protcol] = pio.protocol.errors.to_json...? somehow
@errors[:protocol] = pio.protocol.errors
end
self
end
def succeed?
@errors.none?
end
private
def valid?
unless @id && @protocol_source && @user && @team
@errors[:invalid_arguments] = { '@id': @id, '@protocol_source': @protocol_source, 'user': @user, 'team': @team }
.map { |key, value| "Can't find #{key.capitalize}" if value.nil? }.compact
return false
end
true
end
def client
endpoint_name = Constants::PROTOCOLS_ENDPOINTS.dig(*@protocol_source.split('/').map(&:to_sym))
"ProtocolImporters::#{endpoint_name}::ProtocolNormalizer".constantize.new
end
end
end

View file

@ -193,6 +193,12 @@ class Constants
# Protocol importers
#=============================================================================
PROTOCOLS_ENDPOINTS = {
protocolsio: {
v3: 'ProtocolsIO::V3'
}
}.freeze
PROTOCOLS_IO_V3_API = {
base_uri: 'https://www.protocols.io/api/v3/',
default_timeout: 10,

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
require 'rails_helper'
describe Protocols::ImportProtocolFromClientService do
let(:user) { create :user }
let(:team) { create :team }
let(:service_call) do
Protocols::ImportProtocolFromClientService
.call(protocol_client_id: 'id', protocol_source: 'protocolsio/v3', user_id: user.id, team_id: team.id)
end
let(:normalized_response) do
JSON.parse(file_fixture('protocols_importer/protocols_io/v3/normalized_single_protocol.json').read)
.to_h.with_indifferent_access
end
context 'when have invalid arguments' do
it 'returns an error when can\'t find user' do
allow(User).to receive(:find).and_return(nil)
expect(service_call.errors).to have_key(:invalid_arguments)
end
end
context 'when have valid arguments' do
before do
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ProtocolNormalizer)
.to(receive(:load_protocol).and_return(normalized_response))
# Do not generate and request real images
allow(ProtocolImporters::AttachmentsBuilder).to(receive(:generate).and_return([]))
end
it 'Adds Protocol record' do
expect { service_call }.to(change { Protocol.all.count }.by(1))
end
# more tests will be implemented when add error handling to service
end
end