Class: Insights::API::Common::RBAC::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/insights/api/common/rbac/service.rb

Class Method Summary collapse

Class Method Details

.call(klass, extra_headers = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/insights/api/common/rbac/service.rb', line 11

def self.call(klass, extra_headers = {})
  setup
  yield init(klass, extra_headers)
rescue RBACApiClient::ApiError => err
  raise TimedOutError.new('Connection timed out') if err.code.nil?
  raise NetworkError.new(err.message) if err.code.zero?

  Rails.logger.error("#{err.class}: #{err.message} ")
  raise
end

.paginate(obj, method, pagination_options, *method_args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/insights/api/common/rbac/service.rb', line 22

def self.paginate(obj, method, pagination_options, *method_args)
  Enumerator.new do |enum|
    opts = { :limit => 10, :offset => 0 }.merge(pagination_options)
    count = nil
    fetched = 0
    begin
      loop do
        args = [method_args, opts].flatten.compact
        result = obj.send(method, *args)
        count ||= result.meta.count
        opts[:offset] = opts[:offset] + result.data.count
        result.data.each do |element|
          enum.yield element
        end
        fetched += result.data.count
        break if count == fetched || result.data.empty?
      end
    rescue RBACApiClient::ApiError => err
      raise TimedOutError.new('Connection timed out') if err.code.nil?
      raise NetworkError.new(err.message) if err.code.zero?
      raise
    rescue StandardError => e
      Rails.logger.error("Exception when calling pagination on #{method} #{e}")
      raise
    end
  end
end