Class: Hcloud::DatacenterResource

Inherits:
AbstractResource show all
Includes:
Enumerable
Defined in:
lib/hcloud/datacenter_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



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hcloud/datacenter_resource.rb', line 33

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
9
10
11
# File 'lib/hcloud/datacenter_resource.rb', line 5

def all
  j = Oj.load(request("datacenters").run.body)
  dc = j["datacenters"].map{|x| Datacenter.new(x, self, client) }
  dc.reject{|x| x.id == j["recommendation"]}.unshift(
    dc.find{|x| x.id == j["recommendation"]}
  )
end

#find(id) ⇒ Object



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

def find(id)
  Datacenter.new(
    Oj.load(request("datacenters/#{id}").run.body)["datacenter"],
    self,
    client
  )
end

#find_by(name:) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/hcloud/datacenter_resource.rb', line 25

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


13
14
15
# File 'lib/hcloud/datacenter_resource.rb', line 13

def recommended
  all.first
end