Class: Hcloud::AbstractResource

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, base_path: '') ⇒ AbstractResource

Returns a new instance of AbstractResource.



57
58
59
60
61
62
63
# File 'lib/hcloud/abstract_resource.rb', line 57

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



55
56
57
# File 'lib/hcloud/abstract_resource.rb', line 55

def client
  @client
end

Class Method Details

.bind_to(klass) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/hcloud/abstract_resource.rb', line 12

def bind_to(klass)
  resource = self
  %w[find find_by where all [] page limit per_page order
     to_a count pagnation pagination each].each do |method|
    klass.define_singleton_method(method) do |*args, &block|
      resource.new(client: Client.connection).public_send(method, *args, &block)
    end
  end
end

.filter_attributes(*keys) ⇒ Object



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

def filter_attributes(*keys)
  return @filter_attributes if keys.to_a.empty?

  @filter_attributes = keys
end

.resource(res = nil) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/hcloud/abstract_resource.rb', line 44

def resource(res = nil)
  return (@resource = res) if res
  return @resource if @resource

  auto_const = resource_class.name.demodulize.gsub('Resource', '').to_sym
  return Hcloud.const_get(auto_const) if Hcloud.constants.include?(auto_const)

  raise Error, "unable to lookup resource class for #{name}"
end

.resource_classObject



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

def resource_class
  ancestors[ancestors.index(Hcloud::AbstractResource) - 1]
end

.resource_path(path = nil) ⇒ Object



38
39
40
41
42
# File 'lib/hcloud/abstract_resource.rb', line 38

def resource_path(path = nil)
  return (@resource_path = path) if path

  @resource_path || resource_url
end

.resource_url(url = nil) ⇒ Object



32
33
34
35
36
# File 'lib/hcloud/abstract_resource.rb', line 32

def resource_url(url = nil)
  return (@resource_url = url) if url

  @resource_url || resource_class.name.demodulize.gsub('Resource', '').tableize
end

Instance Method Details

#[](arg) ⇒ Object



88
89
90
# File 'lib/hcloud/abstract_resource.rb', line 88

def [](arg)
  find_by(id: arg)
end

#allObject



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

def all
  where
end

#each(&block) ⇒ Object



141
142
143
# File 'lib/hcloud/abstract_resource.rb', line 141

def each(&block)
  run.each(&block)
end

#find(id) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/hcloud/abstract_resource.rb', line 80

def find(id)
  prepare_request(
    [self.class.resource_url, id].join('/'),
    resource_path: resource_path.to_s.singularize,
    resource_class: self.class.resource
  )
end

#find_by(**kwargs) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/hcloud/abstract_resource.rb', line 92

def find_by(**kwargs)
  if id = kwargs.delete(:id)
    return find(id)
  end

  per_page(1).where(**kwargs).first
rescue Error::NotFound
end

#limit(limit) ⇒ Object



113
114
115
# File 'lib/hcloud/abstract_resource.rb', line 113

def limit(limit)
  _dup :@limit, limit
end

#order(*sort) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hcloud/abstract_resource.rb', line 117

def order(*sort)
  _dup :@order,
       begin
         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
       end
end

#page(page) ⇒ Object

def count

per_page(1).first&.response&.pagination&.total_entries.to_i

end



105
106
107
# File 'lib/hcloud/abstract_resource.rb', line 105

def page(page)
  _dup :@page, page
end

#paginationObject

this is just to keep the actual bevahior



146
147
148
149
150
# File 'lib/hcloud/abstract_resource.rb', line 146

def pagination
  return :auto if client.auto_pagination

  run.response.pagination
end

#per_page(per_page) ⇒ Object



109
110
111
# File 'lib/hcloud/abstract_resource.rb', line 109

def per_page(per_page)
  _dup :@per_page, per_page
end

#runObject



132
133
134
135
136
137
138
139
# File 'lib/hcloud/abstract_resource.rb', line 132

def run
  @run ||= multi_query(
    resource_url,
    q: @query,
    resource_path: resource_path,
    resource_class: self.class.resource
  )
end

#where(kwargs = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/hcloud/abstract_resource.rb', line 69

def where(kwargs = {})
  kwargs.each_key do |key|
    keys = self.class.filter_attributes.map(&:to_s)
    next if keys.include?(key.to_s)

    raise ArgumentError, "unknown filter #{key}, allowed keys are #{keys}"
  end

  _dup :@query, @query.to_h.merge(kwargs)
end