Top Level Namespace
Defined Under Namespace
Modules: Cpaas
Instance Method Summary collapse
- #compose_error_from(err_response) ⇒ Object
- #convert_hash_keys(value) ⇒ Object
- #deep_find(object, key, parentKey = '') ⇒ Object
- #id_from(url) ⇒ Object
- #process_response(res) ⇒ Object
- #reject(obj, key) ⇒ Object
- #underscore(str) ⇒ Object
- #underscore_key(k) ⇒ Object
Instance Method Details
#compose_error_from(err_response) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cpaas/util.rb', line 23 def compose_error_from(err_response) error_obj = deep_find(err_response, :message_id) if (error_obj) = error_obj[:text] error_obj[:variables].each_with_index { |variable, index| .gsub!("%#{index + 1}", variable) } response = { name: error_obj[:name], exception_id: error_obj[:message_id], message: } else response = { name: err_response.keys.first, exception_id: 'Unknown', message: err_response[:message] } end end |
#convert_hash_keys(value) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cpaas/util.rb', line 66 def convert_hash_keys(value) case value when Array value.map { |v| convert_hash_keys(v) } when Hash Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }] else value end end |
#deep_find(object, key, parentKey = '') ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cpaas/util.rb', line 49 def deep_find(object, key, parentKey = '') result = nil if object.respond_to?(:key?) && object.key?(key) object[:name] = parentKey return object elsif object.is_a? Enumerable object.each do |k, v| result = deep_find(v, key, k) return result if !result.nil? end end return result end |
#id_from(url) ⇒ Object
45 46 47 |
# File 'lib/cpaas/util.rb', line 45 def id_from (url) url.split('/').last end |
#process_response(res) ⇒ Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/cpaas/util.rb', line 1 def process_response(res) if res.key? :exception_id response = res elsif res && res.dig(:__for_test__) custom_body = res.dig(:custom_body) reject(res, :custom_body) if !res.dig(:custom_body).nil? && block_given? custom_body = yield res[:custom_body] end response = custom_body.nil? ? res : res.merge(custom_body) elsif block_given? response = yield res else topLevelKey = res.keys.first response = res[topLevelKey].as_json end response end |
#reject(obj, key) ⇒ Object
89 90 91 |
# File 'lib/cpaas/util.rb', line 89 def reject(obj, key) obj.reject { |k,v| k == key } end |
#underscore(str) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/cpaas/util.rb', line 81 def underscore(str) str.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |
#underscore_key(k) ⇒ Object
77 78 79 |
# File 'lib/cpaas/util.rb', line 77 def underscore_key(k) underscore(k.to_s).to_sym end |