Class: Hcloud::AbstractResource

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hcloud/abstract_resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, parent: nil, base_path: "") ⇒ AbstractResource

Returns a new instance of AbstractResource.



7
8
9
10
11
12
13
14
# File 'lib/hcloud/abstract_resource.rb', line 7

def initialize(client:, parent: nil, base_path: "")
  @client = client
  @parent = parent
  @page = 1
  @per_page = 25
  @order = []
  @base_path = base_path
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



5
6
7
# File 'lib/hcloud/abstract_resource.rb', line 5

def base_path
  @base_path
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/hcloud/abstract_resource.rb', line 5

def client
  @client
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/hcloud/abstract_resource.rb', line 5

def parent
  @parent
end

Instance Method Details

#each(&block) ⇒ Object



65
66
67
68
69
# File 'lib/hcloud/abstract_resource.rb', line 65

def each(&block)
  all.each do |member|
    block.call(member)
  end
end

#limit(limit) ⇒ Object



26
27
28
29
# File 'lib/hcloud/abstract_resource.rb', line 26

def limit(limit)
  @limit = limit
  self
end

#mj(path, **o, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hcloud/abstract_resource.rb', line 44

def mj(path, **o, &block)
  if !client.nil? and client.auto_pagination
    requests = __entries__(path, **o)
    if requests.all?{|x| x.is_a? Hash }
      m = MultiReply.new(j: requests, pagination: :auto)
      m.cb = block
      return m
    end
    client.hydra.run
    j = requests.map do |x|
      Oj.load(x.response.body)
    end
    m = MultiReply.new(j: j, pagination: :auto)
    m.cb = block
    return m
  end
  m = MultiReply.new(j: [Oj.load(request(path, o.merge(ep: ep)).run.body)])
  m.cb = block
  m
end

#order(*sort) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hcloud/abstract_resource.rb', line 31

def order(*sort)
  @order = sort.flat_map do |s|
    case s
    when Symbol, String then s.to_s
    when Hash then s.map { |k, v| "#{k}:#{v}" }
    else
      raise ArgumentError, 
        "Unable to resolve type for given #{s.inspect} from #{sort}"
    end
  end
  self
end

#page(page) ⇒ Object



16
17
18
19
# File 'lib/hcloud/abstract_resource.rb', line 16

def page(page)
  @page = page
  self
end

#per_page(per_page) ⇒ Object



21
22
23
24
# File 'lib/hcloud/abstract_resource.rb', line 21

def per_page(per_page)
  @per_page = per_page
  self
end