Class: Hcloud::ImageResource

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



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hcloud/image_resource.rb', line 9

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/image_resource.rb', line 3

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

#find(id) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/hcloud/image_resource.rb', line 21

def find(id)
  Image.new(
    Oj.load(request("images/#{id.to_i}").run.body)["image"],
    self,
    client
  )
end

#find_by(name:) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/hcloud/image_resource.rb', line 39

def find_by(name:)
  j = Oj.load(request("images", q: {name: name}).run.body)["images"]
  return if j.none?
  j.each do |x|
    return Image.new(x, self, client)
  end
end

#where(sort: nil, type: nil, bound_to: nil, name: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/hcloud/image_resource.rb', line 29

def where(sort: nil, type: nil, bound_to: nil, name: nil)
  query = {}
  method(:where).parameters.inject(query) do |r,x| 
    (var = eval(x.last.to_s)).nil? ? r : r.merge!(x.last => var)
  end
  mj("images", q: query) do |j|
    j.flat_map{|x| x["images"].map{ |x| Image.new(x, self, client) } }
  end
end