Module: CsvParserClient

Included in:
SampleManifestExcel::Upload::Processor::TubeRack
Defined in:
lib/csv_parser_client.rb

Overview

For communicating with the csv-parser microservice (github.com/sanger/csv-parser)

Class Method Summary collapse

Class Method Details

.get_tube_rack_scan_results(tube_rack_barcode, object_to_add_errors_to) ⇒ Object

rubocop:todo Metrics/MethodLength



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/csv_parser_client.rb', line 8

def self.get_tube_rack_scan_results(tube_rack_barcode, object_to_add_errors_to) # rubocop:todo Metrics/AbcSize
  endpoint = configatron.tube_rack_scans_microservice_url
  response = Net::HTTP.get_response(URI(endpoint + tube_rack_barcode))

  if response.body.nil?
    error_message =
      # rubocop:todo Layout/LineLength
      "Scan could not be retrieved for tube rack with barcode #{tube_rack_barcode}. Service responded with status code #{response.code}"

    # rubocop:enable Layout/LineLength
    error_message += " and message #{response.message}."
    object_to_add_errors_to.errors.add(:base, error_message)
    return nil
  end

  begin
    scan_results = JSON.parse(response.body)
  rescue JSON::JSONError => e
    error_message =
      # rubocop:todo Layout/LineLength
      "Response when trying to retrieve scan (tube rack with barcode #{tube_rack_barcode}) was not valid JSON so could not be understood. Error message: #{e.message}"

    # rubocop:enable Layout/LineLength
    object_to_add_errors_to.errors.add(:base, error_message)
    return nil
  end

  unless response.code == '200'
    error_message =
      # rubocop:todo Layout/LineLength
      "Scan could not be retrieved for tube rack with barcode #{tube_rack_barcode}. Service responded with status code #{response.code}"

    # rubocop:enable Layout/LineLength
    error_message += " and the following message: #{scan_results['error']}"
    object_to_add_errors_to.errors.add(:base, error_message)
    return nil
  end
  return nil unless scan_results.key?('layout')

  tube_barcode_to_coordinate_without_no_reads = remove_no_read_results(scan_results['layout'])
  tube_barcode_to_coordinate_without_no_reads || nil
end

.no_read?(value_to_check) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/csv_parser_client.rb', line 58

def self.no_read?(value_to_check)
  return true if value_to_check.casecmp('no read').zero?

  false
end

.remove_no_read_results(tube_barcode_to_coordinate) ⇒ Object

rubocop:enable Metrics/MethodLength



53
54
55
56
# File 'lib/csv_parser_client.rb', line 53

def self.remove_no_read_results(tube_barcode_to_coordinate)
  tube_barcode_to_coordinate&.reject! { |key| no_read?(key) } unless tube_barcode_to_coordinate.nil? # rubocop:todo Style/SafeNavigation
  tube_barcode_to_coordinate
end