Class: IGMarkets::RequestPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/request_printer.rb

Overview

This class contains methods for printing a REST request and its JSON response for inspection and debugging. Request printing is enabled by setting RequestPrinter.enabled to true.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledBoolean

Returns Whether the request printer is enabled.

Returns:

  • (Boolean)

    Whether the request printer is enabled.



7
8
9
# File 'lib/ig_markets/request_printer.rb', line 7

def enabled
  @enabled
end

Class Method Details

This method returns an undefined value.

Prints out an options hash that is ready to be passed to RestClient::Request.execute.

Parameters:

  • options (Hash)

    The options hash.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ig_markets/request_printer.rb', line 14

def print_options(options)
  return unless enabled

  puts "#{options[:method].to_s.upcase} #{options[:url]}"

  puts '  Headers:'
  options[:headers].each do |name, value|
    print_request_header name, value
  end

  print_request_body options[:payload]
end

This method returns an undefined value.

Formats and prints a JSON response body.

Parameters:

  • body (String)

    The response body.



32
33
34
35
36
37
38
39
40
# File 'lib/ig_markets/request_printer.rb', line 32

def print_response_body(body)
  return unless enabled

  print '  Response: '

  puts JSON.pretty_generate(JSON.parse(body)).gsub "\n", "\n            "
rescue JSON::ParserError
  puts body
end