Sha256: 265f7a5fcdd8a2bbd9ecaa158ca72f33a400f8d0a46435dd1b489285c205cf26

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require "forwardable"
require "rouge"
require "stringio"
require "term/ansicolor"

module Acter
  class Result
    extend Forwardable

    DEFAULT_RENDER_OPTIONS = {
      show_body: true,
      show_headers: false,
      color: :tty?,
      theme: "monokai",
    }

    def initialize(response)
      @response = response
    end

    attr_reader :response
    def_delegator :@response, :success?

    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 colorize
          s.puts Term::ANSIColor.bold(response.status)
        else
          s.puts response.status
        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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acter-0.1.0 lib/acter/result.rb