2019-06-13 20:17:15 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ProtocolImporters
|
|
|
|
class SearchProtocolsService
|
|
|
|
extend Service
|
2019-06-27 16:11:36 +08:00
|
|
|
require 'protocol_importers/protocols_io/v3/errors'
|
2019-06-13 20:17:15 +08:00
|
|
|
|
|
|
|
attr_reader :errors, :protocols_list
|
|
|
|
|
|
|
|
CONSTANTS = Constants::PROTOCOLS_IO_V3_API
|
|
|
|
|
|
|
|
def initialize(protocol_source:, query_params: {})
|
|
|
|
@protocol_source = protocol_source
|
|
|
|
@query_params = query_params
|
|
|
|
@errors = Hash.new { |h, k| h[k] = {} }
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
return self unless valid?
|
|
|
|
|
2019-06-18 20:01:57 +08:00
|
|
|
# Call api client
|
2019-06-27 16:11:36 +08:00
|
|
|
api_response = api_client.protocol_list(@query_params)
|
2019-06-13 20:17:15 +08:00
|
|
|
|
2019-06-18 20:01:57 +08:00
|
|
|
# Normalize protocols list
|
2019-06-27 16:11:36 +08:00
|
|
|
@protocols_list = normalizer.normalize_list(api_response)
|
2019-06-13 20:17:15 +08:00
|
|
|
|
2019-06-27 16:11:36 +08:00
|
|
|
self
|
|
|
|
rescue api_errors => e
|
|
|
|
@errors[e.error_type] = e.message
|
|
|
|
self
|
|
|
|
rescue normalizer_errors => e
|
|
|
|
@errors[e.error_type] = e.message
|
|
|
|
self
|
|
|
|
rescue StandardError => e
|
|
|
|
@errors[:build_protocol] = e.message
|
2019-06-13 20:17:15 +08:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def succeed?
|
|
|
|
@errors.none?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
# try if key is not empty
|
|
|
|
@errors[:invalid_params][:key] = 'Key cannot be empty' if @query_params[:key].blank?
|
|
|
|
|
|
|
|
# try if page id is ok
|
2019-06-14 16:41:51 +08:00
|
|
|
if @query_params[:page_id] && !@query_params[:page_id].to_i.positive?
|
2019-06-13 20:17:15 +08:00
|
|
|
@errors[:invalid_params][:page_id] = 'Page needs to be positive'
|
|
|
|
end
|
|
|
|
|
|
|
|
# try if order_field is ok
|
2019-06-14 17:07:34 +08:00
|
|
|
if @query_params[:order_field] && CONSTANTS[:available_order_fields].exclude?(@query_params[:order_field]&.to_sym)
|
2019-06-13 20:17:15 +08:00
|
|
|
@errors[:invalid_params][:order_field] = 'Order field is not ok'
|
|
|
|
end
|
|
|
|
|
|
|
|
# try if order dir is ok
|
2019-06-14 17:07:34 +08:00
|
|
|
if @query_params[:order_field] && CONSTANTS[:available_order_dirs].exclude?(@query_params[:order_dir]&.to_sym)
|
2019-06-13 20:17:15 +08:00
|
|
|
@errors[:invalid_params][:order_dir] = 'Order dir is not ok'
|
|
|
|
end
|
|
|
|
|
|
|
|
# try if endpints exists
|
|
|
|
@errors[:invalid_params][:source_endpoint] = 'Wrong source endpoint' unless endpoint_name&.is_a?(String)
|
|
|
|
|
|
|
|
succeed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_name
|
|
|
|
Constants::PROTOCOLS_ENDPOINTS.dig(*@protocol_source.split('/').map(&:to_sym))
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_client
|
|
|
|
"ProtocolImporters::#{endpoint_name}::ApiClient".constantize.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalizer
|
|
|
|
"ProtocolImporters::#{endpoint_name}::ProtocolNormalizer".constantize.new
|
|
|
|
end
|
2019-06-18 20:01:57 +08:00
|
|
|
|
|
|
|
def api_errors
|
2019-06-26 20:04:03 +08:00
|
|
|
"ProtocolImporters::#{endpoint_name}::V3Errors::Error".constantize
|
2019-06-18 20:01:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def normalizer_errors
|
2019-06-26 20:04:03 +08:00
|
|
|
"ProtocolImporters::#{endpoint_name}::V3Errors::NormalizerError".constantize
|
2019-06-18 20:01:57 +08:00
|
|
|
end
|
2019-06-13 20:17:15 +08:00
|
|
|
end
|
|
|
|
end
|