module FHIR::STU3

Constants

PRIMITIVES
RESOURCES
TYPES

Public Class Methods

default_logger() click to toggle source
# File lib/fhir_stu3_models/fhir.rb, line 14
def self.default_logger
  @default_logger ||= Logger.new(ENV['FHIR_LOGGER'] || STDOUT)
end
from_contents(contents) click to toggle source
# File lib/fhir_stu3_models/fhir.rb, line 18
def self.from_contents(contents)
  doc = Nokogiri::XML(contents)
  if doc.errors.empty?
    FHIR::STU3::Xml.from_xml(contents)
  else
    FHIR::STU3::Json.from_json(contents)
  end
end
logger() click to toggle source
# File lib/fhir_stu3_models/fhir.rb, line 6
def self.logger
  @logger || default_logger
end
logger=(logger) click to toggle source
# File lib/fhir_stu3_models/fhir.rb, line 10
def self.logger=(logger)
  @logger = logger
end
primitive?(datatype:, value:) click to toggle source

TODO: pull regexes from metadata

# File lib/fhir_stu3_models/fhir.rb, line 28
def self.primitive?(datatype:, value:)
  # Remaining data types: handle special cases before checking type StructureDefinitions
  case datatype.downcase
  when 'boolean'
    !(value.to_s =~ /\A(true|false)\Z/).nil?
  when 'integer'
    !(value.to_s =~ /\A(0|[-+]?[1-9][0-9]*)\Z/).nil?
  when 'string', 'markdown'
    value.is_a?(String)
  when 'decimal'
    !(value.to_s =~ /\A([-+]?([0]|([1-9][0-9]*))(\.[0-9]+)?)\Z/).nil?
  when 'uri'
    begin
      !URI.parse(value).nil?
    rescue
      false
    end
  when 'base64binary'
    # According to RFC-4648 base64binary encoding includes digits 0-9, a-z, A-Z, =, +, /, and whitespace
    # an empty string is considered valid
    # whitespace is not significant so we strip it out before doing the regex so that we can be sure that
    # the number of characters is a multiple of 4.
    # https://tools.ietf.org/html/rfc4648
    !(value.to_s.gsub(/\s/, '') =~ %r{\A(|[0-9a-zA-Z\+=/]{4}+)\Z}).nil?
  when 'instant'
    formatted_value = value.respond_to?(:xmlschema) ? value.xmlschema : value.to_s
    !(formatted_value =~ /\A([0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))))))\Z/).nil?
  when 'date'
    !(value.to_s =~ /\A(-?[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1]))?)?)\Z/).nil?
    # NOTE: we don't try to instantiate and verify  a Date because ruby does not natively suppport
    # partial dates, which the FHIR standard allows.
  when 'datetime'
    !(value.to_s =~ /\A(-?[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?)\Z/).nil?
    # NOTE: we don't try to instantiate and verify a DateTime because ruby does not natively suppport
    # partial dates, which the FHIR standard allows.
  when 'time'
    !(value.to_s =~ /\A(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?)\Z/).nil?
  when 'code'
    !(value.to_s =~ /\A[^\s]+([\s]?[^\s]+)*\Z/).nil?
  when 'oid'
    !(value.to_s =~ /\Aurn:oid:[0-2](\.[1-9]\d*)+\Z/).nil?
  when 'id'
    !(value.to_s =~ /\A[A-Za-z0-9\-\.]{1,64}\Z/).nil?
  when 'xhtml'
    fragment = Nokogiri::HTML::DocumentFragment.parse(value)
    value.is_a?(String) && fragment.errors.size.zero?
  when 'unsignedint'
    !(value.to_s =~ /\A([0]|([1-9][0-9]*))\Z/).nil?
  when 'positiveint'
    !(value.to_s =~ /\A+?[1-9][0-9]*\Z/).nil?
  else
    FHIR::STU3.logger.warn "Unable to check #{value} for datatype #{datatype}"
    false
  end
end