Module: ChargeBee::Util

Defined in:
lib/chargebee/util.rb

Class Method Summary collapse

Class Method Details

.as_str(value) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/chargebee/util.rb', line 47

def self.as_str(value)
  if(value == nil)
    return ''
  else
    return value.to_s
  end
end

.serialize(value, prefix = nil, idx = nil) ⇒ Object



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
# File 'lib/chargebee/util.rb', line 4

def self.serialize(value, prefix = nil, idx = nil)
  serialized = {}
  case value
    when Hash
      value.each do |k, v|
        if(v.kind_of? Hash or v.kind_of? Array)
          serialized.merge!(serialize(v, k))
        else
          key = "#{(prefix!=nil) ? prefix:''}#{(prefix!=nil) ? '['+k.to_s+']' : k}#{(idx != nil) ? '['+idx.to_s+']':''}"
          serialized.merge!({key => as_str(v)})
        end
      end
    when Array
      value.each_with_index do |v, i|
        serialized.merge!(serialize(v, prefix, i))
      end
    else
       if(idx != nil and prefix != nil)
          key = "#{prefix}[#{idx.to_s}]"
          serialized.merge!({key => as_str(value)})
       else
         raise ArgumentError.new("only hash or arrays are allowed as value")
       end
  end
  serialized
end

.symbolize_keys(object) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chargebee/util.rb', line 31

def self.symbolize_keys(object)
  case object
  when Hash
    new = {}
    object.each do |key, value|
      key = (key.to_sym rescue key) || key
      new[key] = symbolize_keys(value)
    end
    new
  when Array
    object.map { |value| symbolize_keys(value) }
  else
    object
  end
end