Sha256: d5aa5baa474ea235a875ef317fb5936a499c9beb187d5879269d6789a7b1cf53

Contents?: true

Size: 1.15 KB

Versions: 14

Compression:

Stored size: 1.15 KB

Contents

module HaveAPI::CLI
  module ExampleFormatter
    def self.format_examples(cli, action, out = $>)
      action.examples.each do |example|
        out << ' > ' << example[:title] << ":\n" unless example[:title].empty?

        # request
        out << "$ #{$0} #{action.resource_path.join('.')} #{action.name}"

        params = example[:request]

        if params
          out << ' --' unless params.empty?

          params.each do |k, v|
            desc = action.param_description(:input, k)
            next unless desc

            out << ' ' << example_param(k, v, desc)
          end
        end

        out << "\n"

        # response
        cli.format_output(
          action,
          { action.namespace(:output).to_sym => example[:response] },
          out
        )
      end
    end

    def self.example_param(name, value, desc)
      option = name.to_s.dasherize

      case desc[:type]
      when 'Boolean'
        value ? "--#{option}" : "--no-#{option}"

      else
        "--#{option} #{example_value(value)}"
      end
    end

    def self.example_value(v)
      v.is_a?(String) && (v.empty? || v.index(' ')) ? "\"#{v}\"" : v
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
haveapi-client-0.25.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.24.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.23.7 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.23.6 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.23.5 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.23.4 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.23.3 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.23.2 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.23.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.23.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.22.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.22.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.21.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.21.0 lib/haveapi/cli/example_formatter.rb