Class: Bixby::Model::Base

Inherits:
Object show all
Defined in:
lib/bixby-client/model/base.rb

Direct Known Subclasses

Agent, Check, Command, Host, Metric

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ Base

self



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bixby-client/model/base.rb', line 35

def initialize(hash=nil)
  return if hash.nil?

  hash.each do |k,v|
    instance_variable_set("@#{k}", v)
    next if self.respond_to?(k.to_sym)
    code = <<-EOF
    def #{k}()
      @#{k}
    end
    EOF
    eval(code)
  end

  def [](key)
    if self.respond_to?(key.to_sym) then
      return self.send(key.to_sym)
    end
    return nil
  end

end

Class Method Details

.api_uri(*args) ⇒ Object



26
27
28
29
30
31
# File 'lib/bixby-client/model/base.rb', line 26

def api_uri(*args)
  if args.first !~ %r{^/rest/} then
    args.unshift "/rest#{args.shift}"
  end
  URI.join(Bixby.client.manager_uri, *args).to_s
end

.get(url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bixby-client/model/base.rb', line 8

def get(url)
  req = HTTPI::Request.new(api_uri(url))
  req.headers["Content-Type"] = "application/json"
  req.headers["Accept"] = "application/json" # temp workaround, manager should always return JSON on REST urls
  Bixby.client.sign_http_request(req)
  res = HTTPI.get(req)
  if res.error? then
    raise "error" # TODO
  end

  data = MultiJson.load(res.body)
  if data.kind_of? Array then
    return data.map{ |d| self.new(d) }
  else
    return self.new(d)
  end
end

Instance Method Details

#[](key) ⇒ Object



49
50
51
52
53
54
# File 'lib/bixby-client/model/base.rb', line 49

def [](key)
  if self.respond_to?(key.to_sym) then
    return self.send(key.to_sym)
  end
  return nil
end