Class: AllscriptsUnityClient::Utilities
- Inherits:
-
Object
- Object
- AllscriptsUnityClient::Utilities
- Defined in:
- lib/allscripts_unity_client/utilities.rb
Overview
Utilities for massaging the data that comes back from Unity.
Constant Summary collapse
- DATETIME_REGEX =
/\A((\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?)\z/
- DATE_REGEX =
/\A((\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}))\z/
Class Method Summary collapse
-
.encode_data(data) ⇒ Object
Encode binary data into Base64 encoding.
-
.recursively_symbolize_keys(hash) ⇒ Object
Transform string keys into symbols and convert CamelCase to snake_case.
-
.try_to_encode_as_date(timezone, possible_date) ⇒ Object
Try to encode a string into a Date or ActiveSupport::TimeWithZone object.
Class Method Details
.encode_data(data) ⇒ Object
Encode binary data into Base64 encoding.
- data
-
Data to encode.
The Base64 encoding of the data.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/allscripts_unity_client/utilities.rb', line 42 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
Transform string keys into symbols and convert CamelCase to snake_case.
- hash
-
The hash to transform.
Returns the transformed hash.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/allscripts_unity_client/utilities.rb', line 59 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(timezone, possible_date) ⇒ Object
Try to encode a string into a Date or ActiveSupport::TimeWithZone object.
Uses DATETIME_REGEX and DATE_REGEX to match possible date string.
- timezone
-
An ActiveSupport::TimeZone instance.
- possible_data
-
A string that could contain a date.
Returns Date or ActiveSupport::TimeWithZone, or the string if it did not contain a date.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/allscripts_unity_client/utilities.rb', line 20 def self.try_to_encode_as_date(timezone, possible_date) case possible_date when DATE_REGEX Date.parse(possible_date) when DATETIME_REGEX timezone.parse(possible_date) else possible_date end # Since we know in either of the cases above we only attempt to # parse a string this is either an "invalid date" from # `Date.parse` or an "argument out of range" from # `ActiveSupport::TimeZone#parse`. rescue ArgumentError possible_date end |