class Insights::API::Common::RBAC::Service
Public Class Methods
call(klass, extra_headers = {}) { |init(klass, extra_headers)| ... }
click to toggle source
# 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)
click to toggle source
# 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
Private Class Methods
dev_credentials(config)
click to toggle source
# File lib/insights/api/common/rbac/service.rb, line 65 def self.dev_credentials(config) # Set up user/pass for basic auth if we're in dev and they exist. if Rails.env.development? config.username = ENV.fetch('DEV_USERNAME') config.password = ENV.fetch('DEV_PASSWORD') end end
init(klass, extra_headers)
click to toggle source
# File lib/insights/api/common/rbac/service.rb, line 58 def self.init(klass, extra_headers) headers = Insights::API::Common::Request.current_forwardable.merge(extra_headers) klass.new.tap do |api| api.api_client.default_headers = api.api_client.default_headers.merge(headers) end end
setup()
click to toggle source
# File lib/insights/api/common/rbac/service.rb, line 50 def self.setup RBACApiClient.configure do |config| config.host = ENV['RBAC_URL'] || 'localhost' config.scheme = URI.parse(ENV['RBAC_URL']).try(:scheme) || 'http' dev_credentials(config) end end