Class: RightScraper::Loggers::Base

Inherits:
Logger
  • Object
show all
Defined in:
lib/right_scraper/loggers/base.rb

Direct Known Subclasses

Default

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



35
36
37
38
39
40
41
# File 'lib/right_scraper/loggers/base.rb', line 35

def initialize(*args)
  super(*args)
  @abort_seen = false
  @callback = nil
  @errors = []
  @warnings = []
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



33
34
35
# File 'lib/right_scraper/loggers/base.rb', line 33

def callback
  @callback
end

#errorsObject

Returns the value of attribute errors.



33
34
35
# File 'lib/right_scraper/loggers/base.rb', line 33

def errors
  @errors
end

#warningsObject

Returns the value of attribute warnings.



33
34
35
# File 'lib/right_scraper/loggers/base.rb', line 33

def warnings
  @warnings
end

Instance Method Details

#note_error(exception, type, explanation = nil) ⇒ TrueClass

Note an error, with the given exception and explanation of what was going on.

Parameters:

  • exception (Exception)

    or nil

  • type (Symbol)

    of operation

  • explanation (String) (defaults to: nil)

    of operation or nil

Returns:

  • (TrueClass)

    always true

Raises:

  • (NotImplementedError)


96
97
98
# File 'lib/right_scraper/loggers/base.rb', line 96

def note_error(exception, type, explanation = nil)
  raise NotImplementedError
end

#note_phase(phase, type, explanation, exception = nil) ⇒ TrueClass

Note a phase change within an operation.

Parameters:

  • phase (Symbol)

    of operation; one of :begin, :commit, :abort

  • type (Symbol)

    of operation

  • explanation (String)

    of operation or nil

  • exception (Exception) (defaults to: nil)

    or nil

Returns:

  • (TrueClass)

    always true



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/right_scraper/loggers/base.rb', line 71

def note_phase(phase, type, explanation, exception = nil)
  @callback.call(phase, type, explanation, exception) if @callback
  case phase
  when :begin
    @abort_seen = false
  when :commit
    # do nothing
  when :abort
    unless @abort_seen
      note_error(exception, type, explanation)
      @abort_seen = true
    end
  else
    fail 'Unknown phase'
  end
end

#note_warning(message) ⇒ TrueClass

Note a warning for current operation.

Parameters:

  • exception (Exception)

    or nil

  • type (Symbol)

    of operation

  • explanation (String)

    of operation or nil

Returns:

  • (TrueClass)

    always true

Raises:

  • (NotImplementedError)


107
108
109
# File 'lib/right_scraper/loggers/base.rb', line 107

def note_warning(message)
  raise NotImplementedError
end

#operation(type, explanation = '') { ... } ⇒ Object

Encapsulates an operation that merits logging.

Parameters:

  • type (Symbol)

    of operation

  • explanation (String) (defaults to: '')

    of operation or empty

Yields:

  • operational callback (required)

Returns:

  • (Object)

    result of callback



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/right_scraper/loggers/base.rb', line 51

def operation(type, explanation = '')
  begin
    note_phase(:begin, type, explanation)
    result = yield
    note_phase(:commit, type, explanation)
    result
  rescue Exception => e
    note_phase(:abort, type, explanation, e)
    raise
  end
end