Class: GithubCLI::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/github_cli/formatter.rb

Overview

This is the main entry point for formatting output. It delegates to other objects like Formatter::Table to perform actual rendering.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, options = {}) ⇒ Formatter

Returns a new instance of Formatter.



10
11
12
13
# File 'lib/github_cli/formatter.rb', line 10

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

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/github_cli/formatter.rb', line 8

def format
  @format
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/github_cli/formatter.rb', line 8

def response
  @response
end

Instance Method Details

#determine_output_formatterObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/github_cli/formatter.rb', line 21

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

#render_outputObject



15
16
17
18
19
# File 'lib/github_cli/formatter.rb', line 15

def render_output
  render_status
  Terminal.paged_output
  determine_output_formatter
end

#render_statusObject

Render status code



39
40
41
42
43
44
# File 'lib/github_cli/formatter.rb', line 39

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