Top Level Namespace

Defined Under Namespace

Modules: Cpaas

Instance Method Summary collapse

Instance Method Details

#compose_error_from(response) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cpaas-sdk/util.rb', line 25

def compose_error_from(response)
  error_obj = deep_find(response, :message_id)

  if (error_obj)
    message = error_obj[:text]

    error_obj[:variables].each_with_index { |variable, index| message.gsub!("%#{index + 1}", variable) }

    return {
      name: error_obj[:name],
      exception_id: error_obj[:message_id],
      message: message
    }
  end


  {
    name: response[:error] || response.keys.first,
    message: response[:error_description] || response[:message]
  }
end

#convert_hash_keys(value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/cpaas-sdk/util.rb', line 68

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cpaas-sdk/util.rb', line 51

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



47
48
49
# File 'lib/cpaas-sdk/util.rb', line 47

def id_from (url)
  url.split('/').last
end

#process_response(res, remove_outer_key = true) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cpaas-sdk/util.rb', line 1

def process_response(res, remove_outer_key = true)
  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
  elsif remove_outer_key
    topLevelKey = res.keys.first
    response = res[topLevelKey].as_json
  else
    response = res
  end

  response
end

#reject(obj, key) ⇒ Object



91
92
93
# File 'lib/cpaas-sdk/util.rb', line 91

def reject(obj, key)
  obj.reject { |k,v| k == key }
end

#underscore(str) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/cpaas-sdk/util.rb', line 83

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



79
80
81
# File 'lib/cpaas-sdk/util.rb', line 79

def underscore_key(k)
  underscore(k.to_s).to_sym
end