Sha256: 49602be099133dcfb47cadf4a2849b4e5132445a369fd0caad534058ae0365a2

Contents?: true

Size: 781 Bytes

Versions: 1

Compression:

Stored size: 781 Bytes

Contents

module Hcloud
  class LocationResource < AbstractResource
    include Enumerable

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

    def find(id)
      Location.new(
        Oj.load(request("locations/#{id}").run.body)["location"],
        self,
        client
      )
    end
    
    def find_by(name:)
      x = Oj.load(request("locations", q: {name: name}).run.body)["locations"]
      return nil if x.none?
      x.each do |s|
        return Location.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

1 entries across 1 versions & 1 rubygems

Version Path
hcloud-0.1.0.pre.alpha4 lib/hcloud/location_resource.rb