Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Remove remaining Activesupport methods #118

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ jobs:
- '2.7'
- '3.0'
- '3.1'
rails-version:
- '6.0'
- '6.1'
- '7.0'
env:
TEST_RAILS_VERSION: ${{ matrix.rails-version }}
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
steps:
- uses: actions/checkout@v4
Expand All @@ -33,6 +28,6 @@ jobs:
- name: Run tests
run: bundle exec rake
- name: Report code coverage
if: ${{ github.ref == 'refs/heads/master' && matrix.ruby-version == '3.0' && matrix.rails-version == '6.1' }}
if: ${{ github.ref == 'refs/heads/master' && matrix.ruby-version == '3.0' }}
continue-on-error: true
uses: paambaati/codeclimate-action@v5
12 changes: 0 additions & 12 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,3 @@ gemspec
# Load developer specific Gemfile
dev_gemfile = File.expand_path("Gemfile.dev.rb", __dir__)
eval_gemfile(dev_gemfile) if File.exist?(dev_gemfile)

minimum_version =
case ENV['TEST_RAILS_VERSION']
when "6.0"
"~>6.0.4"
when "7.0"
"~>7.0.8"
else
"~>6.1.4"
end

gem "activesupport", minimum_version
2 changes: 0 additions & 2 deletions lib/manageiq/api/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "active_support"
require "active_support/core_ext"
require "faraday"
require "faraday_middleware"
require 'forwardable'
Expand Down
6 changes: 3 additions & 3 deletions lib/manageiq/api/client/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ def initialize(options = {})
@token, @miqtoken, @bearer_token, @group = options.values_at(:token, :miqtoken, :bearer_token, :group)

unless token || miqtoken || bearer_token
raise "Must specify both a user and a password" if user.blank? || password.blank?
raise "Must specify both a user and a password" if user.nil? || user.empty? || password.nil? || password.empty?
end
end

def self.auth_options_specified?(options)
options.slice(:user, :password, :token, :miqtoken, :bearer_token, :group).present?
!options.slice(:user, :password, :token, :miqtoken, :bearer_token, :group).empty?
end

private

def fetch_credentials(options)
if options.slice(:user, :password, :token, :miqtoken, :bearer_token).blank?
if options.slice(:user, :password, :token, :miqtoken, :bearer_token).empty?
[DEFAULTS[:user], DEFAULTS[:password]]
else
[options[:user], options[:password]]
Expand Down
14 changes: 7 additions & 7 deletions lib/manageiq/api/client/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def find(*args)
raise "Couldn't find resource without an 'id'"
when 1
res = limit(1).where(:id => args[0]).to_a
raise ManageIQ::API::Client::ResourceNotFound, "Couldn't find resource with 'id' #{args}" if res.blank?
raise ManageIQ::API::Client::ResourceNotFound, "Couldn't find resource with 'id' #{args}" if res.empty?
request_array ? res : res.first
else
raise "Multiple resource find is not supported" unless respond_to?(:query)
Expand All @@ -57,7 +57,7 @@ def pluck(*attrs)
end

def self.subclass(name)
name = name.camelize
name = name.split('_').map(&:capitalize).join #.camelize

if const_defined?(name, false)
const_get(name, false)
Expand Down Expand Up @@ -111,7 +111,7 @@ def respond_to_missing?(sym, *_)
def parameters_from_query_relation(options)
api_params = {}
[:offset, :limit].each { |opt| api_params[opt] = options[opt] if options[opt] }
api_params[:attributes] = options[:select].join(",") if options[:select].present?
api_params[:attributes] = options[:select].join(",") if options[:select] && !options[:select].empty?
if options[:where]
api_params[:filter] ||= []
api_params[:filter] += filters_from_query_relation("=", options[:where])
Expand Down Expand Up @@ -178,18 +178,18 @@ def exec_action(name, *args, &block)
def action_body(action_name, *args, &block)
args = args.flatten
args = args.first if args.size == 1 && args.first.kind_of?(Hash)
args = {} if args.blank?
args = {} if args.empty?
block_data = block ? block.call : {}
body = { "action" => action_name }
if block_data.present?
if block_data && !block_data.empty?
if block_data.kind_of?(Array)
body["resources"] = block_data.collect { |resource| resource.merge(args) }
elsif args.present? && args.kind_of?(Array)
elsif !args.empty? && args.kind_of?(Array)
body["resources"] = args.collect { |resource| resource.merge(block_data) }
else
body["resource"] = args.dup.merge!(block_data)
end
elsif args.present?
elsif !args.empty?
body[args.kind_of?(Array) ? "resources" : "resource"] = args
end
body
Expand Down
14 changes: 7 additions & 7 deletions lib/manageiq/api/client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def options(path = "", params = {})

def json_response
resp = response.body.strip
resp.blank? ? {} : JSON.parse(resp)
resp.empty? ? {} : JSON.parse(resp)
rescue
raise JSON::ParserError, "Response received from #{url} is not of type #{CONTENT_TYPE}"
end

def api_path(path)
if path.to_s.start_with?(url.to_s)
path.to_s
elsif path.to_s.blank?
elsif path.to_s.empty?
URI.join(url, API_PREFIX).to_s
else
URI.join(url, path.to_s.start_with?(API_PREFIX) ? path.to_s : "#{API_PREFIX}/#{path}").to_s
Expand All @@ -78,7 +78,7 @@ def handle
faraday.response(:logger, client.logger)
faraday.use FaradayMiddleware::FollowRedirects, :limit => 3, :standards_compliant => true
faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP
if authentication.token.blank? && authentication.miqtoken.blank? && authentication.bearer_token.blank?
if authentication.token.nil? && authentication.miqtoken.nil? && authentication.bearer_token.nil?
faraday.basic_auth(authentication.user, authentication.password)
end
end
Expand All @@ -92,10 +92,10 @@ def send_request(method, path, params, &block)
@response = handle.run_request(method.to_sym, api_path(path), nil, nil) do |request|
request.headers[:content_type] = CONTENT_TYPE
request.headers[:accept] = CONTENT_TYPE
request.headers[:authorization] = "Bearer #{authentication.bearer_token}" unless authentication.bearer_token.blank?
request.headers['X-MIQ-Group'] = authentication.group unless authentication.group.blank?
request.headers['X-Auth-Token'] = authentication.token unless authentication.token.blank?
request.headers['X-MIQ-Token'] = authentication.miqtoken unless authentication.miqtoken.blank?
request.headers[:authorization] = "Bearer #{authentication.bearer_token}" unless authentication.bearer_token.nil?
request.headers['X-MIQ-Group'] = authentication.group unless authentication.group.nil?
request.headers['X-Auth-Token'] = authentication.token unless authentication.token.nil?
request.headers['X-MIQ-Token'] = authentication.miqtoken unless authentication.miqtoken.nil?
request.params.merge!(params)
request.body = yield(block).to_json if block
end
Expand Down
2 changes: 1 addition & 1 deletion lib/manageiq/api/client/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def update(status, json_response = {})
@status = status
@kind, @message, @klass = nil
error = json_response["error"]
if status >= 400 && error.present?
if status >= 400 && (!error.nil? && !error.empty?)
if error.kind_of?(Hash)
@kind, @message, @klass = error.values_at("kind", "message", "klass")
else
Expand Down
6 changes: 2 additions & 4 deletions lib/manageiq/api/client/mixins/action_mixin.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module ActionMixin
extend ActiveSupport::Concern

private

def clear_actions
@actions = []
end

def actions_present?
@actions.present?
!@actions.empty?
end

def fetch_actions(resource_hash)
Expand All @@ -25,7 +23,7 @@ def action_defined?(action)
end

def actions=(action_array)
@actions = action_array.blank? ? [] : action_array
@actions = (action_array.nil? || action_array.empty?) ? [] : action_array
end

def add_action(action)
Expand Down
2 changes: 0 additions & 2 deletions lib/manageiq/api/client/mixins/custom_inspect_mixin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module CustomInspectMixin
extend ActiveSupport::Concern

def inspect
pretty_print_inspect
end
Expand Down
4 changes: 2 additions & 2 deletions lib/manageiq/api/client/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Resource
include CustomInspectMixin

def self.subclass(name)
name = name.classify
name = name.split('_').map(&:capitalize).join.gsub(/e?s$/, "") #.classify

if const_defined?(name, false)
const_get(name, false)
Expand Down Expand Up @@ -62,7 +62,7 @@ def exec_action(name, args = nil, &block)
body = { "action" => action.name }
resource = args.dup
resource.merge!(block.call) if block
resource.present? ? body.merge("resource" => resource) : body
!resource.empty? ? body.merge("resource" => resource) : body
end
action_result(res)
end
Expand Down
1 change: 0 additions & 1 deletion manageiq-api-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "activesupport", ">= 6.0", "<7.1"
spec.add_dependency "faraday", "~> 1.0"
spec.add_dependency "faraday_middleware", "~> 1.0"
spec.add_dependency "json", "~> 2.3"
Expand Down
4 changes: 0 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@
def api_file_fixture(path)
File.read(File.join("spec/fixtures/api/", path))
end

require "active_support"
puts
puts "\e[93mUsing ActiveSupport #{ActiveSupport.version}\e[0m"