class Acter::Result

Constants

DEFAULT_RENDER_OPTIONS

Attributes

response[R]

Public Class Methods

new(response) click to toggle source
# File lib/acter/result.rb, line 18
def initialize(response)
  @response = response
end

Public Instance Methods

render(options = nil) { |response| ... } click to toggle source
# File lib/acter/result.rb, line 25
def render(options = nil)
  options = DEFAULT_RENDER_OPTIONS.merge(Hash(options))
  if block_given?
    more_options = yield response
    options.merge!(Hash(more_options))
  end

  colorize = options[:color] && (options[:color] != :tty? || $>.tty?)

  StringIO.open do |s|
    if options[:show_status]
      if colorize
        s.puts Term::ANSIColor.bold(response.status)
      else
        s.puts response.status
      end
    end
    if options[:show_headers]
      response.headers.each(&s.method(:puts))
    end
    if options[:show_body]
      s.puts
      if colorize
        lexer = response.body_is_json? ? Rouge::Lexers::JSON : Rouge::Lexers::HTML
        s.puts Rouge::Formatters::Terminal256.format(lexer.new.lex(response.body), theme: options[:theme])
      else
        s.puts response.body
      end
    end
    s.string
  end
end