Class: Hcloud::ServerTypeResource

Inherits:
AbstractResource show all
Includes:
Enumerable
Defined in:
lib/hcloud/server_type_resource.rb

Instance Attribute Summary

Attributes inherited from AbstractResource

#base_path, #client, #parent

Instance Method Summary collapse

Methods inherited from AbstractResource

#each, #initialize, #limit, #mj, #order, #page, #per_page

Constructor Details

This class inherits a constructor from Hcloud::AbstractResource

Instance Method Details

#[](arg) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hcloud/server_type_resource.rb', line 26

def [](arg)
  case arg
  when Integer
   begin
     find(arg)
   rescue Error::NotFound
   end
  when String
    find_by(name: arg)
  end
end

#allObject



5
6
7
8
# File 'lib/hcloud/server_type_resource.rb', line 5

def all
  j = Oj.load(request("server_types").run.body)
  j["server_types"].map{|x| ServerType.new(x, self, client) }
end

#find(id) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/hcloud/server_type_resource.rb', line 10

def find(id)
  ServerType.new(
    Oj.load(request("server_types/#{id}").run.body)["server_type"],
    self,
    client
  )
end

#find_by(name:) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/hcloud/server_type_resource.rb', line 18

def find_by(name:)
  x = Oj.load(request("server_types", q: {name: name}).run.body)["server_types"]
  return nil if x.none?
  x.each do |s|
    return ServerType.new(s, self, client)
  end
end