Class: IGMarkets::RequestPrinter
- Inherits:
-
Object
- Object
- IGMarkets::RequestPrinter
- 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
-
.enabled ⇒ Boolean
Whether the request printer is enabled.
Class Method Summary collapse
-
.print_options(options) ⇒ void
Prints out an options hash that is ready to be passed to
RestClient::Request.execute. -
.print_response_body(body) ⇒ void
Formats and prints a JSON response body.
Class Attribute Details
.enabled ⇒ Boolean
Returns 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
.print_options(options) ⇒ void
This method returns an undefined value.
Prints out an options hash that is ready to be passed to RestClient::Request.execute.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ig_markets/request_printer.rb', line 14 def () return unless enabled puts "#{[:method].to_s.upcase} #{[:url]}" puts ' Headers:' [:headers].each do |name, value| print_request_header name, value end print_request_body [:payload] end |
.print_response_body(body) ⇒ void
This method returns an undefined value.
Formats and prints a JSON 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 |