Skip to content

Commit

Permalink
refactor: extract base command
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 30, 2021
1 parent 20741be commit 0ec92d6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 78 deletions.
87 changes: 87 additions & 0 deletions lib/pact_broker/client/base_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'pact_broker/client/hal_client_methods'
require 'pact_broker/client/error'
require 'pact_broker/client/command_result'
require 'term/ansicolor'
require 'pact_broker/client/backports'

module PactBroker
module Client
class BaseCommand
include PactBroker::Client::HalClientMethods

def self.call(params, options, pact_broker_client_options)
new(params, options, pact_broker_client_options).call
end

def initialize(params, options, pact_broker_client_options)
@params = params
@options = options
@pact_broker_base_url = pact_broker_client_options.fetch(:pact_broker_base_url)
@pact_broker_client_options = pact_broker_client_options
end

def call
check_if_command_supported
do_call
rescue PactBroker::Client::Hal::ErrorResponseReturned => e
handle_http_error(e)
rescue PactBroker::Client::Error => e
handle_ruby_error(e)
rescue StandardError => e
handle_ruby_error(e, verbose?)
end

private

attr_reader :params, :options
attr_reader :pact_broker_base_url, :pact_broker_client_options

def handle_http_error(e)
message = if json_output?
body = e.entity.response.raw_body
(body.nil? || body == "") ? "{}" : body
else
::Term::ANSIColor.red(e.message)
end
PactBroker::Client::CommandResult.new(false, message)
end

def handle_ruby_error(e, include_backtrace = false)
PactBroker::Client::CommandResult.new(false, error_message(e, include_backtrace))
end

def error_message(e, include_backtrace)
if json_output?
json_error_message(e, include_backtrace)
else
text_error_message(e, include_backtrace)
end
end

def json_error_message(e, include_backtrace)
error_hash = { message: e.message }
error_hash[:class] = e.class.name unless e.is_a?(PactBroker::Client::Error)
error_hash[:backtrace] = e.backtrace if include_backtrace
{ error: error_hash }.to_json
end


def text_error_message(e, include_backtrace)
maybe_backtrace = (include_backtrace ? "\n" + e.backtrace.join("\n") : "")
exception_message = e.is_a?(PactBroker::Client::Error) ? e.message : "#{e.class} - #{e.message}"
::Term::ANSIColor.red(exception_message) + maybe_backtrace
end

def check_if_command_supported
end

def json_output?
options[:output] == "json"
end

def verbose?
options[:verbose]
end
end
end
end
80 changes: 2 additions & 78 deletions lib/pact_broker/client/environments/environment_command.rb
Original file line number Diff line number Diff line change
@@ -1,81 +1,13 @@
require 'pact_broker/client/hal_client_methods'
require 'pact_broker/client/error'
require 'pact_broker/client/command_result'
require 'term/ansicolor'
require 'pact_broker/client/backports'
require 'pact_broker/client/base_command'

module PactBroker
module Client
module Environments
class EnvironmentCommand
include PactBroker::Client::HalClientMethods

class EnvironmentCommand < PactBroker::Client::BaseCommand
NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support environments. Please upgrade to version 2.80.0 or later."

def self.call(params, options, pact_broker_client_options)
new(params, options, pact_broker_client_options).call
end

def initialize(params, options, pact_broker_client_options)
@params = params
@options = options
@pact_broker_base_url = pact_broker_client_options.fetch(:pact_broker_base_url)
@pact_broker_client_options = pact_broker_client_options
end

def call
check_if_command_supported
do_call
rescue PactBroker::Client::Hal::ErrorResponseReturned => e
handle_http_error(e)
rescue PactBroker::Client::Error => e
handle_ruby_error(e)
rescue StandardError => e
handle_ruby_error(e, verbose?)
end

private

attr_reader :params, :options
attr_reader :pact_broker_base_url, :pact_broker_client_options

def handle_http_error(e)
message = if json_output?
body = e.entity.response.raw_body
(body.nil? || body == "") ? "{}" : body
else
::Term::ANSIColor.red(e.message)
end
PactBroker::Client::CommandResult.new(false, message)
end

def handle_ruby_error(e, include_backtrace = false)
PactBroker::Client::CommandResult.new(false, error_message(e, include_backtrace))
end

def error_message(e, include_backtrace)
if json_output?
json_error_message(e, include_backtrace)
else
text_error_message(e, include_backtrace)
end
end

def json_error_message(e, include_backtrace)
error_hash = { message: e.message }
error_hash[:class] = e.class.name unless e.is_a?(PactBroker::Client::Error)
error_hash[:backtrace] = e.backtrace if include_backtrace
{ error: error_hash }.to_json
end


def text_error_message(e, include_backtrace)
maybe_backtrace = (include_backtrace ? "\n" + e.backtrace.join("\n") : "")
exception_message = e.is_a?(PactBroker::Client::Error) ? e.message : "#{e.class} - #{e.message}"
::Term::ANSIColor.red(exception_message) + maybe_backtrace
end


def new_environment_body
{
"name" => params[:name],
Expand Down Expand Up @@ -128,14 +60,6 @@ def check_if_command_supported
raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE)
end
end

def json_output?
options[:output] == "json"
end

def verbose?
options[:verbose]
end
end
end
end
Expand Down

0 comments on commit 0ec92d6

Please sign in to comment.