class ThreeScaleToolbox::Commands::MethodsCommand::Apply::ApplySubcommand

Public Class Methods

command() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 26
def self.command
  Cri::Command.define do
    name        'apply'
    usage       'apply [opts] <remote> <service> <method>'
    summary     'Update method'
    description 'Update (create if it does not exist) method'

    option      :n, :name, 'Method name', argument: :required
    flag        nil, :disabled, 'Disables this method in all application plans'
    flag        nil, :enabled, 'Enables this method in all application plans'
    option      nil, :description, 'Method description', argument: :required
    ThreeScaleToolbox::CLI.output_flag(self)

    param       :remote
    param       :service_ref
    param       :method_ref

    runner ApplySubcommand
  end
end

Public Instance Methods

run() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 47
def run
  validate_option_params
  hits = service.hits
  method = Entities::Method.find(service: service, ref: method_ref)
  if method.nil?
    method = Entities::Method.create(service: service, attrs: create_method_attrs)
  else
    method.update(method_attrs) unless method_attrs.empty?
  end

  method.disable if option_disabled
  method.enable if option_enabled

  printer.print_record method.attrs
end

Private Instance Methods

create_method_attrs() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 70
def create_method_attrs
  method_attrs.merge('system_name' => method_ref,
                     'friendly_name' => method_ref) { |_key, oldval, _newval| oldval }
end
find_service() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 94
def find_service
  Entities::Service.find(remote: remote,
                         ref: service_ref).tap do |svc|
    raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
  end
end
method_attrs() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 75
def method_attrs
  {
    'friendly_name' => options[:name],
    'description' => options[:description]
  }.compact
end
method_ref() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 109
def method_ref
  arguments[:method_ref]
end
option_disabled() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 86
def option_disabled
  options.fetch(:disabled, false)
end
option_enabled() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 82
def option_enabled
  options.fetch(:enabled, false)
end
printer() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 113
def printer
  # keep backwards compatibility
  options.fetch(:output, CustomPrinter.new(disabled: option_disabled, enabled: option_enabled))
end
remote() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 101
def remote
  @remote ||= threescale_client(arguments[:remote])
end
service() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 90
def service
  @service ||= find_service
end
service_ref() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 105
def service_ref
  arguments[:service_ref]
end
validate_option_params() click to toggle source
# File lib/3scale_toolbox/commands/methods_command/apply_command.rb, line 65
def validate_option_params
  raise ThreeScaleToolbox::Error, '--disabled and --enabled are mutually exclusive' \
    if option_enabled && option_disabled
end