Sha256: 69b20cbd235841335e36bbb4b1b596fb923520b776a68af6d3636d17d4d24fc4

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

# encoding: utf-8

module GithubCLI
  # This is the main entry point for formatting output.
  # It delegates to other objects like Formatter::Table
  # to perform actual rendering.
  class Formatter
    attr_reader :response, :format, :message, :quiet

    def initialize(response, options={})
      @response = response
      @message  = options[:message]
      @format   = options[:format]
      @quiet    = options[:quiet]
    end

    def render_output
      render_status
      return if quiet
      Terminal.paged_output
      determine_output_formatter
      render_message
    end

    def determine_output_formatter
      case format.to_s
      when 'table', /table:v.*/, /table:h.*/
        if response.body && !response.body.empty?
          formatter = Formatters::Table.new(response.body,
                        :transform => format.to_s.split(':').last)
          formatter.format
        end
      when 'csv'
        formatter = Formatters::CSV.new(response)
        formatter.format
      when 'json'
        'json output'
      else
        raise UnknownFormatError, format
      end
    end

    # Render status code
    def render_status
      if response.respond_to? :status
        Terminal.line "Response Status: #{response.status}\n"
        Terminal.newline
      end
    end

    def render_message
      if message
        Terminal.line message
      end
    end

  end # Formatter
end # GithubCLi

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
github_cli-0.6.2 lib/github_cli/formatter.rb
github_cli-0.6.1 lib/github_cli/formatter.rb
github_cli-0.6.0 lib/github_cli/formatter.rb
github_cli-0.5.9 lib/github_cli/formatter.rb