Sha256: 4cb3a0bf0a434cd6e18848b7fbbe7aa6cb53e4131bcaf64058a85e324677a39e

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 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][action.namespace(:input).to_sym]

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

          params.each do |k, v|
            out << ' ' << example_param(k, v, action.param_description(:input, k))
          end
        end

        out << "\n"

        # response
        cli.format_output(action, 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

10 entries across 10 versions & 1 rubygems

Version Path
haveapi-client-0.6.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.5.4 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.5.3 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.5.2 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.5.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.5.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.4.2 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.4.1 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.4.0 lib/haveapi/cli/example_formatter.rb
haveapi-client-0.3.0 lib/haveapi/cli/example_formatter.rb