Sha256: cf8f7d88dfeb4980b77587c73d112b81eb55f0c5b938e7a7b3dd0634d7654de9

Contents?: true

Size: 767 Bytes

Versions: 2

Compression:

Stored size: 767 Bytes

Contents

module Hcloud
  class LocationResource < AbstractResource
    def all
      mj("locations") do |j|
        j.flat_map{|x| x["locations"].map{ |x| Location.new(x, self, client) } }
      end
    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

2 entries across 2 versions & 1 rubygems

Version Path
hcloud-0.1.1 lib/hcloud/location_resource.rb
hcloud-0.1.0 lib/hcloud/location_resource.rb