Sha256: 02abc5ffffcfa770497e96209543e9c53befe72863f69d9cf7494cdb5caa4355

Contents?: true

Size: 1.11 KB

Versions: 44

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'logger'

# Utility methods for dealing with ActiveSupport::Parameters objects.
module MalawiHivProgramReports
  module Utils
    module ParameterUtils
      LOGGER = Logger.new($stdout)

      # Fetches field from ActiveSupport::Parameters.
      #
      # This method is just a wrapper around the fetch or raise bad request
      # routine for retrieving parameter values.
      #
      # @throws InvalidParameterError - When field is not found in stock_obs
      def fetch_parameter(parameters, field)
        parameters.fetch(field)
      rescue KeyError => e
        LOGGER.error("Failed to fetch parameter `#{field}` due to #{e}")
        raise ::InvalidParameterError, "`#{field}` not found in parameters: #{parameters.to_json}"
      end

      def fetch_parameter_as_date(parameters, field, default = nil)
        parameters.fetch(field, default)&.to_date
      rescue ArgumentError => e
        LOGGER.error("Failed to fetch parameter `#{field}` due to #{e}")
        raise ::InvalidParameterError, "Could not parse #{field} as date from: #{parameters.to_json}"
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
malawi_hiv_program_reports-1.1.18 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.17 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.16 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.15 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.14 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.13 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.12 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.11 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.10 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.9 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.8 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.7 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.6 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.5 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.4 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.3 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.2 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.1 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.1.0 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb
malawi_hiv_program_reports-1.0.27 app/services/malawi_hiv_program_reports/utils/parameter_utils.rb