Sha256: b340e81ed31e1a6865813445bf3b5295105c2e0592c7f9905265eab7b177cdec

Contents?: true

Size: 967 Bytes

Versions: 4

Compression:

Stored size: 967 Bytes

Contents

module Hcloud
  class DatacenterResource < AbstractResource
    include Enumerable

    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

    def recommended
      all.first
    end

    def find(id)
      Datacenter.new(
        Oj.load(request("datacenters/#{id}").run.body)["datacenter"],
        self,
        client
      )
    end
    
    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
    
    def [](arg)
      case arg
      when Integer
       begin
         find(arg)
       rescue Error::NotFound
       end
      when String
        find_by(name: arg)
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hcloud-0.1.1 lib/hcloud/datacenter_resource.rb
hcloud-0.1.0 lib/hcloud/datacenter_resource.rb
hcloud-0.1.0.pre.alpha4 lib/hcloud/datacenter_resource.rb
hcloud-0.1.0.pre.alpha3 lib/hcloud/datacenter_resource.rb