Sha256: 0843db6555d0d639bc72992f612b69229305453a08fc716b609348e817bc6a04

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8

module GithubCLI
  module Formatters
    class CSV

      attr_reader :response

      def initialize(response)
        @response = response
      end

      def format
        if response.respond_to?(:to_ary)
          render_headers(response.first)
          response.each_with_index do |item, indx|
            render_line(indx, item)
            Terminal.newline
          end
        elsif response.respond_to?(:keys)
          render_headers(response)
          render_line(1, response)
        else
          Terminal.line "#{response}\n"
        end
      end

      def render_headers(header)
        output = header
        if header.respond_to?(:to_hash)
          output = GithubCLI::Util.flatten_hash(header.to_hash)
          output = "Index,#{output.keys.join(',')}\n"
        elsif header.respond_to?(:to_ary)
          output = "Index,#{header.join(',')}\n"
        end

        Terminal.line output
      end

      def render_line(index, item)
        output = item
        if item.respond_to?(:to_hash)
          output = GithubCLI::Util.flatten_hash(item.to_hash)
          output = output.values.join(',')
        elsif item.respond_to?(:to_ary)
          output = item.join(',')
        end

        $stdout.printf "%d,%s", index, output
      end

    end # CSV
  end # Formatters
end # GithubCLI

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
github_cli-0.6.2 lib/github_cli/formatters/csv.rb
github_cli-0.6.1 lib/github_cli/formatters/csv.rb
github_cli-0.6.0 lib/github_cli/formatters/csv.rb
github_cli-0.5.9 lib/github_cli/formatters/csv.rb
github_cli-0.5.8 lib/github_cli/formatters/csv.rb
github_cli-0.5.7 lib/github_cli/formatters/csv.rb
github_cli-0.5.6 lib/github_cli/formatters/csv.rb
github_cli-0.5.5 lib/github_cli/formatters/csv.rb
github_cli-0.5.4 lib/github_cli/formatters/csv.rb