Class: Hcloud::LocationResource

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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hcloud/location_resource.rb', line 25

def [](arg)
  case arg
  when Integer
   begin
     find(arg)
   rescue Error::NotFound
   end
  when String
    find_by(name: arg)
  end
end

#allObject



3
4
5
6
7
# File 'lib/hcloud/location_resource.rb', line 3

def all
  mj("locations") do |j|
    j.flat_map{|x| x["locations"].map{ |x| Location.new(x, self, client) } }
  end
end

#find(id) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/hcloud/location_resource.rb', line 9

def find(id)
  Location.new(
    Oj.load(request("locations/#{id}").run.body)["location"],
    self,
    client
  )
end

#find_by(name:) ⇒ Object



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

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