Class: AllscriptsUnityClient::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/allscripts_unity_client/utilities.rb

Constant Summary collapse

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}))$/

Class Method Summary collapse

Class Method Details

.encode_data(data) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/allscripts_unity_client/utilities.rb', line 26

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

.recursively_symbolize_keys(hash) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/allscripts_unity_client/utilities.rb', line 38

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

.try_to_encode_as_date(possible_date) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/allscripts_unity_client/utilities.rb', line 10

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