lib/flow_client/utils.rb in flow_client-0.1.2 vs lib/flow_client/utils.rb in flow_client-0.2.0
- old
+ new
@@ -1,8 +1,9 @@
# frozen_string_literal: true
module FlowClient
+ # A collection of utilities.
module Utils
# Left pads a byte array with 0 to length
def self.left_pad_bytes(byte_array, length)
required_pad_count = length - byte_array.count
padding = []
@@ -32,7 +33,46 @@
aliases.each do |key, value|
new_string = new_string.gsub(key.to_s, value.to_s)
end
new_string
end
+
+ def self.strip_address_prefix(address)
+ address[0..1]
+ end
+
+ def self.parse_protobuf_timestamp(timestamp)
+ epoch_micros = timestamp.nanos / 10 ** 6
+ Time.at(timestamp.seconds, epoch_micros)
+ end
+
+ def self.openstruct_to_json(struct)
+ struct.deep_to_h.to_json
+ # struct.each_pair.map do |key, value|
+ # [
+ # key,
+ # case value
+ # when OpenStruct then value.deep_to_h
+ # when Array then value.map {|el| el.class == OpenStruct ? el.deep_to_h : el}
+ # else value
+ # end
+ # ]
+ # end.to_h.to_json
+ end
+
end
end
+
+class OpenStruct
+ def deep_to_h
+ each_pair.map do |key, value|
+ [
+ key,
+ case value
+ when OpenStruct then value.deep_to_h
+ when Array then value.map {|el| el.class == OpenStruct ? el.deep_to_h : el}
+ else value
+ end
+ ]
+ end.to_h
+ end
+end
\ No newline at end of file