Class: Bixby::Model::Base
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = nil) ⇒ Base
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
|
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.["Content-Type"] = "application/json"
req.["Accept"] = "application/json" Bixby.client.sign_http_request(req)
res = HTTPI.get(req)
if res.error? then
raise "error" 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
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
|