Sha256: 914e079ed9ea4e6e852d44eb027bd57d16acc6de3d7ae37432e55bc7c7fea79d

Contents?: true

Size: 1.16 KB

Versions: 31

Compression:

Stored size: 1.16 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

31 entries across 31 versions & 1 rubygems

Version Path
haveapi-client-0.20.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.19.3 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.19.2 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.19.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.19.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.18.2 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.18.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.18.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.17.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.16.3 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.16.2 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.16.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.16.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.15.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.15.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.14.2 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.14.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.14.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.13.3 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.13.2 lib/haveapi/cli/example_formatter.rb