Class: Debug

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_grabber/debug.rb

Overview

Save debug log information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDebug

Initialize a debug object



12
13
14
15
16
# File 'lib/tiny_grabber/debug.rb', line 12

def initialize
  @active = false
  @destination = :print
  @save_html = false
end

Instance Attribute Details

#activeObject

Get debug active flag



4
5
6
# File 'lib/tiny_grabber/debug.rb', line 4

def active
  @active
end

#destinationObject

Get debug destination



6
7
8
# File 'lib/tiny_grabber/debug.rb', line 6

def destination
  @destination
end

#save_htmlObject

Get debug flag to save response HTML to file



8
9
10
# File 'lib/tiny_grabber/debug.rb', line 8

def save_html
  @save_html
end

Instance Method Details

#save(message) ⇒ Object

Save log information

Parameters:

  • message

    Message body



69
70
71
72
73
74
75
76
77
# File 'lib/tiny_grabber/debug.rb', line 69

def save message
  message = "TG | #{Time.now.strftime('%Y%m%d-%H%M%S')} | #{message}"
  case @destination
    when :file
      save_to_file message
    when :print
      p message
  end
end

#save_to_file(message) ⇒ Object

Save log information to file

Parameters:

  • message

    Message body



84
85
86
87
88
89
# File 'lib/tiny_grabber/debug.rb', line 84

def save_to_file message
  debug_path = "#{Dir.pwd}/log"
  Dir.mkdir(debug_path, 0775) unless File.exists? debug_path
  filename = "#{Time.now.strftime('%Y%m%d')}.log"
  File.open("#{debug_path}/#{filename}", 'a+') { |f| f << "#{message}\r\n" }
end