Sha256: 2533cea3091af5c3c9deb79dcfeb28446f1294d44203f95e618edd5964c8e133

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

require "nori"
require "date"
require "american_date"

module AllscriptsUnityClient
  class Utilities
    DATETIME_REGEX = /^((\d{1,2}[-\/]\d{1,2}[-\/]\d{4})|(\d{4}[-\/]\d{1,2}[-\/]\d{1,2})|(\d{1,2}-[A-Za-z]{3,4}-\d{4})|([A-Za-z]{3,4} \d{1,2} \d{2,4}))(T| +)(\d{1,2}:\d{2}(:\d{2})?(\.\d+)? ?(PM|AM|pm|am)?((-|\+)\d{2}:?\d{2})?Z?)$/
    DATE_REGEX = /^((\d{1,2}[-\/]\d{1,2}[-\/]\d{4})|(\d{4}[-\/]\d{1,2}[-\/]\d{1,2})|(\d{1,2}-[A-Za-z]{3,4}-\d{4})|([A-Za-z]{3,4} \d{1,2} \d{2,4}))$/

    def self.try_to_encode_as_date(possible_date)
      if possible_date.nil?
        return nil
      end

      if possible_date.is_a?(String) && possible_date =~ DATE_REGEX
        return Date.parse(possible_date)
      end

      if possible_date.is_a?(String) && possible_date =~ DATETIME_REGEX
        return DateTime.parse(possible_date)
      end

      possible_date
    end

    def self.encode_data(data)
      if data.nil?
        return nil
      end

      if data.respond_to?(:pack)
        return data.pack("m")
      else
        return [data].pack("m")
      end
    end

    def self.recursively_symbolize_keys(hash)
      # Base case: nil maps to nil
      if hash.nil?
        return nil
      end

      # Recurse case: value is a hash so symbolize keys
      if hash.is_a?(Hash)
        result = hash.map do |key, value|
          { key.snakecase.to_sym => recursively_symbolize_keys(value) }
        end

        return result.reduce(:merge)
      end

      # Recurse case: value is an array so symbolize keys for any hash
      # in it
      if hash.is_a?(Array)
        result = hash.map do |value|
          recursively_symbolize_keys(value)
        end

        return result
      end

      # Base case: value was not an array or a hash, so just
      # return it
      hash
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
allscripts_unity_client-1.2.3 lib/allscripts_unity_client/utilities.rb
allscripts_unity_client-1.2.2 lib/allscripts_unity_client/utilities.rb
allscripts_unity_client-1.2.1 lib/allscripts_unity_client/utilities.rb
allscripts_unity_client-1.2.0 lib/allscripts_unity_client/utilities.rb