Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#eflatten(obj) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/small-setup.rb', line 70

def eflatten(obj)
  flat = {}
  if obj["dir"] then
    if obj["nodes"] then
      obj["nodes"].each { |n|
        flat = flat.merge(eflatten(n))
      }
    end
  else
    key = obj["key"].gsub("/","_").gsub("-","_")
    flat[key[1..key.length]]=obj["value"]
  end
  flat
end

#flatten(obj, sub) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/small-setup.rb', line 52

def flatten(obj,sub)
    flat={}
    sub=sub.gsub("-","_").downcase
    obj.keys.each {|k|
        key=k.gsub("-","_").gsub("/","_").downcase
        if obj[k].class == Array then
            if(obj[k][0].class == Hash) then
                flat=flat.merge(flatten(obj[k][0],"#{sub}/#{k}"))
            end
        elsif obj[k].class == Hash then
            flat=flat.merge(flatten(obj[k],"#{sub}/#{k}"))
        else
            flat["#{sub}/#{key}"] = obj[k].to_s
        end
    }
    flat
end

#http_delete(uri) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/small-setup.rb', line 44

def http_delete(uri)
  uri = URI.parse(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Delete.new(uri.request_uri)
  response = http.request(request)
  JSON.parse(response.body)
end

#http_get(uri) ⇒ Object



20
21
22
# File 'lib/small-setup.rb', line 20

def http_get(uri)
    JSON.parse(Net::HTTP.get(URI(uri)))
end

#http_put(uri, doc) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/small-setup.rb', line 24

def http_put(uri,doc) 
    uri = URI.parse(uri)
    header = {'Content-Type'=> 'application/x-www-form-urlencoded'}
    if doc.class == Hash then
        header = {'Content-Type'=> 'application/json'}
    end
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Put.new(uri.request_uri, header)
    request.body = doc
    if doc.class == Hash then
        request.body=doc.to_json
    end
    response = http.request(request)
    if response.body.length >= 2 then
        JSON.parse(response.body)
    else
        {}
    end
end

#nodes2obj(nodes, prefix) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/small-setup.rb', line 85

def nodes2obj(nodes,prefix)
    obj={}
    nodes.each {|node|
      if node['dir'] && node['nodes'] then
        obj[node['key'].gsub(prefix,'')]=nodes2obj( node['nodes'],"#{ node['key'] }/" )
      elsif node['value'] then
        obj[node['key'].gsub(prefix,'')]=node['value']
      end
    }
    obj
end