Module: CommonHelper

Included in:
MobHTTP, PushV3Client
Defined in:
lib/mobpush/utils/common_helper.rb

Instance Method Summary collapse

Instance Method Details

#hash2os(h) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mobpush/utils/common_helper.rb', line 24

def hash2os(h)
    if h.is_a?(Hash)
        os = OpenStruct.new()
        h.each do |k, v|
            if v.is_a?(Hash)
                os.send("#{k}=", hash2os(v))
            elsif v.is_a?(Array)
                os.send("#{k}=", v.map{ |e| hash2os(e)})
            else
                os.send("#{k}=", v)
            end
        end
        os
    elsif h.is_a?(Array)
        h.map do |e|
            hash2os(e)
        end
    else
        h
    end
end

#os2hash(os) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mobpush/utils/common_helper.rb', line 12

def os2hash(os)
    h = {}
    os.each_pair do |k, v|
        if v.is_a?(OpenStruct)
            h[k] = os2hash(v)
        else
            h[k] = v
        end
    end
    h
end

#sanitze_hash(h) ⇒ Object



6
7
8
9
10
# File 'lib/mobpush/utils/common_helper.rb', line 6

def sanitze_hash(h)
    h.delete_if do |k, v|
        v.nil? || (v.respond_to?('empty?') ? v.empty? : false)
    end
end