Module: Cloopen::REST::Utils

Included in:
Client, InstanceResource, ListResource
Defined in:
lib/cloopen/rest/utils.rb

Instance Method Summary collapse

Instance Method Details

#beautify_hash(hash) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/cloopen/rest/utils.rb', line 26

def beautify_hash(hash)
  hash = decloopfy hash
  hash.each do |k, v|
    hash[k] = if v.is_a? Hash
      beautify_hash v
    else
      v
    end
  end
end

#build_body(something, resource) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cloopen/rest/utils.rb', line 5

def build_body(something, resource)
  builder = Builder::XmlMarkup.new
  builder.instruct! :xml, version: '1.0', encoding: 'UTF-8'

  builder.tag! resource.instance_key.chop do |resource|
    something.each do |k, v|
      attr = downcase_first cloopfy(k)
      resource.tag! attr, v
    end
    resource.appId @app_id
  end
end

#cloopfy(something) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/cloopen/rest/utils.rb', line 42

def cloopfy(something)
  if something.is_a? Hash
    Hash[*something.to_a.map {|a| [cloopfy(a[0]).to_sym, a[1]]}.flatten]
  else
    something.to_s.split('_').map do |s|
      [s[0,1].capitalize, s[1..-1]].join
    end.join
  end
end

#decloopfy(something) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/cloopen/rest/utils.rb', line 52

def decloopfy(something)
  if something.is_a? Hash
    Hash[*something.to_a.map {|pair| [decloopfy(pair[0]).to_sym, pair[1]]}.flatten]
  else
    something.to_s.gsub(/[A-Z][a-z]*/) {|s| "_#{s.downcase}"}.gsub(/^_/, '')
  end
end

#downcase_first(something) ⇒ Object



38
39
40
# File 'lib/cloopen/rest/utils.rb', line 38

def downcase_first(something)
  [something[0, 1].downcase, something[1..-1]].join('')
end

#prase_body(something) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/cloopen/rest/utils.rb', line 18

def prase_body(something)
  if something.is_a? Hash
    beautify_hash something
  else
    something
  end
end