Class: Acter::Result
- Inherits:
-
Object
- Object
- Acter::Result
- Extended by:
- Forwardable
- Defined in:
- lib/acter/result.rb
Constant Summary collapse
- DEFAULT_RENDER_OPTIONS =
{ show_body: true, show_headers: false, show_status: true, color: :tty?, theme: "monokai", }
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#initialize(response) ⇒ Result
constructor
A new instance of Result.
- #render(options = nil) ⇒ Object
Constructor Details
#initialize(response) ⇒ Result
Returns a new instance of Result.
18 19 20 |
# File 'lib/acter/result.rb', line 18 def initialize(response) @response = response end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
22 23 24 |
# File 'lib/acter/result.rb', line 22 def response @response end |
Instance Method Details
#render(options = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/acter/result.rb', line 25 def render( = nil) = DEFAULT_RENDER_OPTIONS.merge(Hash()) if block_given? = yield response .merge!(Hash()) end colorize = [:color] && ([:color] != :tty? || $>.tty?) StringIO.open do |s| if [:show_status] if colorize s.puts Term::ANSIColor.bold(response.status) else s.puts response.status end end if [:show_headers] response.headers.each(&s.method(:puts)) end if [: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: [:theme]) else s.puts response.body end end s.string end end |