Module: LiveQA::Util

Defined in:
lib/liveqa/util.rb

Constant Summary collapse

DATE_FORMAT =
'%Y/%m/%d'.freeze
OBFUSCATED =
'[HIDDEN]'.freeze
DEFAULT_OBJECT_DEF =
%w[id name].freeze

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object

Convert string to camelize

Examples:

camelize('my_model') => 'MyModel'

Parameters:

  • (String)


27
28
29
# File 'lib/liveqa/util.rb', line 27

def camelize(string)
  string.split('_').map(&:capitalize).join
end

.compact(hash) ⇒ Hash

Remove nil value from hash

Returns:

  • (Hash)


35
36
37
# File 'lib/liveqa/util.rb', line 35

def compact(hash)
  hash.reject { |_, value| value.nil? }
end

.deep_compact(object) ⇒ Hash

Remove nil value from hash recursively

Returns:

  • (Hash)


43
44
45
46
47
48
49
50
51
52
# File 'lib/liveqa/util.rb', line 43

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

Parameters:

  • hash (Hash)

    to obfuscate

Returns:

  • (Hash)

    hash obfuscated



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/liveqa/util.rb', line 141

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

Parameters:

  • hash (Hash)

    to transform

Returns:

  • (Hash)

    transformed



109
110
111
112
113
114
115
116
117
# File 'lib/liveqa/util.rb', line 109

def deep_stringify_key(hash_object)
  deep_transform_keys_in_object(hash_object) do |key|
    begin
      key.to_s
    rescue StandardError => _e
      key
    end
  end
end

.deep_symbolize_key(hash_object) ⇒ Hash

Deep convert hash to symbol keys

Parameters:

  • hash (Hash)

    to transform

Returns:

  • (Hash)

    transformed



125
126
127
128
129
130
131
132
133
# File 'lib/liveqa/util.rb', line 125

def deep_symbolize_key(hash_object)
  deep_transform_keys_in_object(hash_object) do |key|
    begin
      key.to_sym
    rescue StandardError => _e
      key
    end
  end
end

.deep_underscore_key(hash_object) ⇒ Hash

Deep convert hash to underscore case keys

Parameters:

  • hash (Hash)

    to transform

Returns:

  • (Hash)

    transformed



93
94
95
96
97
98
99
100
101
# File 'lib/liveqa/util.rb', line 93

def deep_underscore_key(hash_object)
  deep_transform_keys_in_object(hash_object) do |key|
    begin
      underscore(key).to_sym
    rescue StandardError => _e
      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. ‘&`).



59
60
61
# File 'lib/liveqa/util.rb', line 59

def encode_parameters(params = {})
  params.map { |k, v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
end

.except_keys(hash, *keys) ⇒ Hash

Remove keys from a Hash

Parameters:

  • to (Hash)

    be excepted

  • to (List[String])

    be excepted

Returns:

  • (Hash)


16
17
18
# File 'lib/liveqa/util.rb', line 16

def except_keys(hash, *keys)
  hash.dup.delete_if { |(key, _value)| keys.include?(key) }
end

.properties(*args) ⇒ Hash

Convert object into Hash

Returns:

  • (Hash)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/liveqa/util.rb', line 160

def properties(*args)
  params = extract_params!(args)

  attributes = params.each_with_object({}) do |(key, value), hash|
    hash[key] = extract_object(value)
  end

  attributes.merge(
    args.each_with_object({}) do |object, hash|
      key = object.class.name.downcase
      next if key.nil?

      hash[key] = extract_object(object)
    end
  )
end

.safe_json_parse(data) ⇒ Object

Parse JSON without raise



180
181
182
183
184
# File 'lib/liveqa/util.rb', line 180

def safe_json_parse(data)
  JSON.parse(data.to_s)
rescue JSON::ParserError
  {}
end

.underscore(string) ⇒ Object

Convert string to underscore

Examples:

underscore('MyModel') => 'my_model'

Parameters:

  • (String)


78
79
80
81
82
83
84
85
# File 'lib/liveqa/util.rb', line 78

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.



67
68
69
# File 'lib/liveqa/util.rb', line 67

def url_encode(key)
  CGI.escape(key.to_s).gsub('%5B', '[').gsub('%5D', ']')
end