Sha256: 358c8eb01234e8a64355345731e9dc9dfa99668fd134d65122f073cf842c2946

Contents?: true

Size: 807 Bytes

Versions: 3

Compression:

Stored size: 807 Bytes

Contents

module Hcloud
  class ServerTypeResource < AbstractResource
    include Enumerable

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

    def find(id)
      ServerType.new(
        Oj.load(request("server_types/#{id}").run.body)["server_type"],
        self,
        client
      )
    end
    
    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
    
    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

3 entries across 3 versions & 1 rubygems

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