class ThreeScaleToolbox::Commands::ActiveDocsCommand::Apply::ApplySubcommand

Public Class Methods

command() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 27
def self.command
  Cri::Command.define do
    name        'apply'
    usage       'apply <remote> <activedocs_id_or_system_name>'
    summary     'Update activedocs'
    description 'Create or update an ActiveDocs'

    option :i, :'service-id', "Specify the Service ID associated to the ActiveDocs", argument: :required
    option :p, :'publish', "Specify it to publish the ActiveDocs on the Developer Portal. Otherwise it will be hidden", argument: :forbidden
    option nil, :'hide', "Specify it to hide the ActiveDocs on the Developer Portal", argument: :forbidden
    option nil, :'skip-swagger-validations', "Skip validation of the Swagger specification. true or false", argument: :required, transform: ThreeScaleToolbox::Helper::BooleanTransformer.new
    option :d, :'description', "Specify the description of the ActiveDocs", argument: :required
    option :s, :'name', "Specify the name of the ActiveDocs", argument: :required
    option nil, :'openapi-spec', "Specify the swagger spec. Can be a file, an URL or '-' to read from stdin. This option is mandatory when applying the ActiveDoc for the first time", argument: :required

    param   :remote
    param   :activedocs_id_or_system_name

    ThreeScaleToolbox::CLI.output_flag(self)

    runner ApplySubcommand
  end
end

Public Instance Methods

run() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 51
def run
  validate_option_params
  res = activedocs
  if !res
    res = Entities::ActiveDocs.create(remote: remote, attrs: create_activedocs_attrs)
  else
    res.update(activedocs_attrs) unless activedocs_attrs.empty?
  end

  printer.print_record res.attrs
end

Private Instance Methods

activedocs() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 81
def activedocs
  @activedocs ||= find_activedocs
end
activedocs_attrs() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 115
def activedocs_attrs
  activedocs_basic_attrs.tap do |params|
    params["body"] = activedocs_json_spec if !option_openapi_spec.nil?
    params["published"] = true if option_publish
    params["published"] = false if option_hide
  end
end
activedocs_basic_attrs() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 123
def activedocs_basic_attrs
  {
    "service_id" => options[:'service-id'],
    "skip_swagger_validations" => options[:'skip-swagger-validations'],
    "description" => options[:'description'],
    "system_name" => options[:'system-name'],
    "name" => options[:name],
  }.compact
end
activedocs_json_spec() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 105
def activedocs_json_spec
  @json_spec ||= read_activedocs_json_spec
end
check_openapi_spec_defined() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 75
def check_openapi_spec_defined
  if !option_openapi_spec
    raise ThreeScaleToolbox::Error.new("--openapi-spec is mandatory when ActiveDocs is created")
  end
end
create_activedocs_attrs() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 133
def create_activedocs_attrs
  check_openapi_spec_defined
  activedocs_attrs.merge(
    "system_name" => ref,
    "name" => ref,
    "body" => activedocs_json_spec,
  ) { |_key, oldval, _newval| oldval } # receiver of the merge message has key priority
end
find_activedocs() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 89
def find_activedocs
  Entities::ActiveDocs.find(remote: remote, ref: ref)
end
option_hide() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 97
def option_hide
  options.fetch(:hide, false)
end
option_openapi_spec() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 101
def option_openapi_spec
  options[:'openapi-spec']
end
option_publish() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 93
def option_publish
  options.fetch(:publish, false)
end
printer() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 142
def printer
  # keep backwards compatibility
  options.fetch(:output, CustomPrinter.new(publish: option_publish, hide: option_hide))
end
read_activedocs_json_spec() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 109
def read_activedocs_json_spec
  activedoc_spec = option_openapi_spec
  activedoc_spec_content = load_resource(activedoc_spec)
  JSON.pretty_generate(activedoc_spec_content)
end
ref() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 85
def ref
  arguments[:activedocs_id_or_system_name]
end
remote() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 65
def remote
  @remote ||= threescale_client(arguments[:remote])
end
validate_option_params() click to toggle source
# File lib/3scale_toolbox/commands/activedocs_command/apply_command.rb, line 69
def validate_option_params
  if option_publish && option_hide
    raise ThreeScaleToolbox::Error.new("--publish and --hide are mutually exclusive")
  end
end