Module: LiveQA::Util
- Defined in:
- lib/liveqa/util.rb
Constant Summary collapse
- DATE_FORMAT =
'%Y/%m/%d'.freeze
- OBFUSCATED =
'[HIDDEN]'.freeze
Class Method Summary collapse
-
.compact(hash) ⇒ Hash
Remove nil value from hash.
-
.deep_compact(object) ⇒ Hash
Remove nil value from hash recursively.
-
.deep_obfuscate_value(object, fields, obfuscate_name = OBFUSCATED) ⇒ Hash
Deep remove key from hash.
-
.deep_stringify_key(hash_object) ⇒ Hash
Deep convert hash to string keys.
-
.deep_symbolize_key(hash_object) ⇒ Hash
Deep convert hash to symbol keys.
-
.deep_underscore_key(hash_object) ⇒ Hash
Deep convert hash to underscore case keys.
-
.encode_parameters(params = {}) ⇒ Object
Encodes a hash of parameters in a way that’s suitable for use as query parameters in a URI or as form parameters in a request body.
-
.underscore(string) ⇒ Object
Convert string to underscore.
-
.url_encode(key) ⇒ Object
Encodes a string in a way that makes it suitable for use in a set of query parameters in a URI or in a set of form parameters in a request body.
Class Method Details
.compact(hash) ⇒ Hash
Remove nil value from hash
12 13 14 |
# File 'lib/liveqa/util.rb', line 12 def compact(hash) hash.reject { |_, value| value.nil? } end |
.deep_compact(object) ⇒ Hash
Remove nil value from hash recursively
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/liveqa/util.rb', line 20 def deep_compact(object) object.each_with_object({}) do |(key, value), result| if value.is_a?(Hash) value = deep_compact(value) result[key] = value if !value.nil? && !value.empty? else result[key] = value unless value.nil? end end end |
.deep_obfuscate_value(object, fields, obfuscate_name = OBFUSCATED) ⇒ Hash
Deep remove key from hash
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/liveqa/util.rb', line 118 def deep_obfuscate_value(object, fields, obfuscate_name = OBFUSCATED) case object when Hash object.each_with_object({}) do |(key, value), result| result[key] = fields.include?(key.to_s) ? obfuscate_name : deep_obfuscate_value(value, fields) end when Array object.map { |e| deep_obfuscate_value(e, fields) } else object end end |
.deep_stringify_key(hash_object) ⇒ Hash
Deep convert hash to string keys
86 87 88 89 90 91 92 93 94 |
# File 'lib/liveqa/util.rb', line 86 def deep_stringify_key(hash_object) deep_transform_keys_in_object(hash_object) do |key| begin key.to_s rescue key end end end |
.deep_symbolize_key(hash_object) ⇒ Hash
Deep convert hash to symbol keys
102 103 104 105 106 107 108 109 110 |
# File 'lib/liveqa/util.rb', line 102 def deep_symbolize_key(hash_object) deep_transform_keys_in_object(hash_object) do |key| begin key.to_sym rescue key end end end |
.deep_underscore_key(hash_object) ⇒ Hash
Deep convert hash to underscore case keys
70 71 72 73 74 75 76 77 78 |
# File 'lib/liveqa/util.rb', line 70 def deep_underscore_key(hash_object) deep_transform_keys_in_object(hash_object) do |key| begin underscore(key).to_sym rescue key end end end |
.encode_parameters(params = {}) ⇒ Object
Encodes a hash of parameters in a way that’s suitable for use as query parameters in a URI or as form parameters in a request body. This mainly involves escaping special characters from parameter keys and values (e.g. ‘&`).
36 37 38 |
# File 'lib/liveqa/util.rb', line 36 def encode_parameters(params = {}) params.map { |k, v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&') end |
.underscore(string) ⇒ Object
Convert string to underscore
55 56 57 58 59 60 61 62 |
# File 'lib/liveqa/util.rb', line 55 def underscore(string) string .gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |
.url_encode(key) ⇒ Object
Encodes a string in a way that makes it suitable for use in a set of query parameters in a URI or in a set of form parameters in a request body.
44 45 46 |
# File 'lib/liveqa/util.rb', line 44 def url_encode(key) CGI.escape(key.to_s).gsub('%5B', '[').gsub('%5D', ']') end |