lib/submodules/ably-ruby/lib/ably/modules/model_common.rb in ably-rest-0.8.14 vs lib/submodules/ably-ruby/lib/ably/modules/model_common.rb in ably-rest-0.8.15
- old
+ new
@@ -30,16 +30,41 @@
# @return [String]
def to_json(*args)
as_json.to_json(*args)
end
+ # Like to_json but encodes all binary fields to hex
+ def to_safe_json(*args)
+ as_json.
+ each_with_object({}) do |(key, val), obj|
+ obj[key] = to_safe_jsonable_val(val)
+ end.to_json(*args)
+ end
+
# @!attribute [r] hash
# @return [Integer] Compute a hash-code for this hash. Two hashes with the same content will have the same hash code
def hash
attributes.hash
end
private
+ def to_safe_jsonable_val(val)
+ case val
+ when Array
+ val.map { |array_val| to_safe_jsonable_val(array_val) }
+ when Hash
+ val.each_with_object({}) { |(key, hash_val), obj| obj[key] = to_safe_jsonable_val(hash_val) }
+ when String
+ if val.encoding == Encoding::ASCII_8BIT
+ val.unpack("H*").first
+ else
+ val
+ end
+ else
+ val
+ end
+ end
+
def ensure_utf8_string_for(attribute, value)
if value
raise ArgumentError, "#{attribute} must be a String" unless value.kind_of?(String)
raise ArgumentError, "#{attribute} cannot use ASCII_8BIT encoding, please use UTF_8 encoding" unless value.encoding == Encoding::UTF_8
end