class Resty::PrettyPrinter

Attributes

options[RW]
params[RW]

Public Class Methods

new(options, params) click to toggle source
# File lib/resty/pretty_printer.rb, line 6
def initialize(options, params)
  @options = options
  @params = params
end

Public Instance Methods

generate() click to toggle source
# File lib/resty/pretty_printer.rb, line 11
def generate 
  if params.is_a? Hashie::Mash
    output = ""
    if verbose?
      output += build_line("#{request.method.upcase} #{request.url}")
      request.processed_headers.each { |key, value| output += build_line("#{key}: #{value}") }
      output += "\n"

      output += build_line("Response Code: #{response.code}")
      response.headers.each { |key, value| output += build_line("#{key}: #{value}") }
    else
      output += build_line("Response Code: #{response.code}")
    end

    output += pretty_print_response(response)
  else
    params
  end
end

Private Instance Methods

build_line(line) click to toggle source
# File lib/resty/pretty_printer.rb, line 33
def build_line(line)
  "> #{line}\n"
end
pretty_print_response(response) click to toggle source
# File lib/resty/pretty_printer.rb, line 37
def pretty_print_response(response)
  return response if response == ""
  parsed_response = JSON.parse(response)
  MultiJson.dump(parsed_response, { pretty: true }) || ""
rescue => e
  response
end
request() click to toggle source
# File lib/resty/pretty_printer.rb, line 51
def request
  params[:request]
end
response() click to toggle source
# File lib/resty/pretty_printer.rb, line 55
def response
  params[:response]
end
verbose?() click to toggle source
# File lib/resty/pretty_printer.rb, line 47
def verbose?
  options.verbose?
end