Module: Lab::Lims::Utils

Included in:
AcknowledgementSerializer, AcknowledgementWorker, OrderDTO, OrderSerializer, PullWorker, PushWorker
Defined in:
app/services/lab/lims/utils.rb

Overview

Various helper methods for modules in the Lims namespaces…

Constant Summary collapse

LIMS_LOG_PATH =
Rails.root.join('log', 'lims')
TEST_NAME_MAPPINGS =
{
  # For some weird reason(s) some tests have multiple names in LIMS,
  # this is used to sanitize those names.
  'hiv_viral_load' => 'HIV Viral Load',
  'viral laod' => 'HIV Viral Load',
  'viral load' => 'HIV Viral Load',
  'i/ink' => 'India ink',
  'indian ink' => 'India ink'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_concept_by_name(name) ⇒ Object



83
84
85
86
87
88
# File 'app/services/lab/lims/utils.rb', line 83

def self.find_concept_by_name(name)
  ConceptName.joins(:concept)
             .merge(Concept.all) # Filter out voided
             .where(name: CGI.unescapeHTML(name))
             .first
end

.lab_userObject



45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/lab/lims/utils.rb', line 45

def self.lab_user
  user = User.find_by_username('lab_daemon')
  return user if user

  god_user = User.first
  User.current = god_user
  person = Person.create!(creator: god_user.user_id, birthdate: '1980-01-01')
  PersonName.create!(person: person, given_name: 'Lab', family_name: 'Daemon', creator: god_user.user_id)

  User.create!(username: 'lab_daemon', person: person, creator: god_user.user_id)
end

.parse_date(str_date, fallback_date = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/services/lab/lims/utils.rb', line 57

def self.parse_date(str_date, fallback_date = nil)
  str_date = str_date&.to_s

  if str_date.blank? && fallback_date.blank?
    raise "Can't parse blank date"
  end

  return parse_date(fallback_date) if str_date.blank?

  str_date = str_date.gsub(/^00/, '20').gsub(/^180/, '20')

  case str_date
  when /\d{4}-\d{2}-\d{2}/
    str_date
  when /\d{2}-\d{2}-\d{2}/
    Date.strptime(str_date, '%d-%m-%Y').strftime('%Y-%m-%d')
  when /(\d{4}\d{2}\d{2})\d+/
    Date.strptime(str_date, '%Y%m%d').strftime('%Y-%m-%d')
  when %r{\d{2}/\d{2}/\d{4}}
    str_date.to_date.to_s
  else
    Rails.logger.warn("Invalid date: #{str_date}")
    parse_date(fallback_date)
  end
end

.structify(object) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/lab/lims/utils.rb', line 31

def self.structify(object)
  if object.is_a?(Hash)
    object.each_with_object(OpenStruct.new) do |kv_pair, struct|
      key, value = kv_pair

      struct[key] = structify(value)
    end
  elsif object.respond_to?(:map)
    object.map { |item| structify(item) }
  else
    object
  end
end

.translate_test_name(test_name) ⇒ Object



27
28
29
# File 'app/services/lab/lims/utils.rb', line 27

def self.translate_test_name(test_name)
  TEST_NAME_MAPPINGS.fetch(test_name.downcase, test_name)
end

Instance Method Details

#loggerObject



13
14
15
# File 'app/services/lab/lims/utils.rb', line 13

def logger
  Rails.logger
end