-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutility_methods.rb
More file actions
61 lines (55 loc) · 2.01 KB
/
utility_methods.rb
File metadata and controls
61 lines (55 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# encoding: UTF-8
# frozen_string_literal: true
require 'active_support/inflector'
require 'json'
#
# A module that has common utility methods used by this project
# @author:: Nayyara Samuel (mailto: nayyara.samuel@opower.com)
#
module MockServer::UtilityMethods
# Does the following filter/transform operations on a hash
# - exclude null or empty valued keys from the hash
# - camelize the keys of the hash
# @param obj [Object] an object which will be used to create the hash. Must support :to_hash method
# @return [Hash] the transformed hash
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
def camelized_hash(obj)
obj = obj&.respond_to?(:to_hash) ? obj.to_hash : obj
if obj.is_a?(Hash)
obj.each_with_object({}) do |(k, v), acc|
is_empty = v.nil? || (v.respond_to?(:empty?) ? v.empty? : false)
acc[camelize(k)] = camelized_hash(v) unless is_empty
end
elsif obj.respond_to?(:map)
obj.map { |element| camelized_hash(element) }
else
obj
end
end
# Converts keys to symbols
# @param hash [Hash] a hash
# @return [Hash] a copy of the hash where keys are symbols
def symbolize_keys(hash)
if hash.is_a?(Hash)
Hash[hash.map { |k, v| [k.to_s.underscore.to_sym, symbolize_keys(v)] }]
elsif hash.respond_to?(:map)
hash.map { |obj| symbolize_keys(obj) }
else
hash
end
end
# @param str [Object] an object to camelize the string representation of
# @return [String] the string converted to camelcase with first letter in lower case
def camelize(str)
str.to_s.camelize(:lower)
end
# Parse string response into JSON
# @param response [Response] from RestClient response
# @return [Hash] the parsed response or the object unmodified if parsing is not possible
def parse_string_to_json(response)
JSON.parse(response)
rescue JSON::ParserError
response
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength