Sha256: 7c322af8ba510e6f1474c8d5581c021475c652f1c85db8652e815028b67e5646

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

module ChargeBee
  module Util

    def self.serialize(value, prefix = nil, idx = nil)
      serialized = {}
      case value
        when Hash
          value.each do |k, v|
            if(v.kind_of? Hash)
              serialized.merge!(serialize(v, k))
            elsif(v.kind_of? Array)
              arrayContainsValue = false
              v.each_with_index do |v1, i1|
                if(v1.kind_of? Hash)
                  serialized.merge!(serialize(v1, k, i1))
                else
                  arrayContainsValue = true
                end
              end
              if(arrayContainsValue == true)
                key = "#{(prefix!=nil) ? prefix:''}#{(prefix!=nil) ? '['+k.to_s+']' : k}#{(idx != nil) ? '['+idx.to_s+']':''}"
                serialized.merge!({key => as_str(v)})
              end
            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

    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

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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chargebee-2.14.0 lib/chargebee/util.rb
chargebee-2.13.1 lib/chargebee/util.rb
chargebee-2.13.0 lib/chargebee/util.rb