Class: Tor::Counter

Inherits:
BasicObject
Defined in:
lib/rest_tor/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tor, success: 0, fail: 0, success_at: nil, fail_at: nil, errors: {}) ⇒ Counter

Returns a new instance of Counter.



102
103
104
105
106
107
108
109
# File 'lib/rest_tor/instance.rb', line 102

def initialize(tor, success: 0, fail: 0, success_at: nil, fail_at: nil, errors: {})
  @tor        = tor
  @success    = success
  @fail       = fail
  @success_at = success_at
  @fail_at    = fail_at
  @errors     = errors.is_a?(::Hash) ? errors : {}
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



101
102
103
# File 'lib/rest_tor/instance.rb', line 101

def errors
  @errors
end

#failObject

Returns the value of attribute fail.



101
102
103
# File 'lib/rest_tor/instance.rb', line 101

def fail
  @fail
end

#fail_atObject

Returns the value of attribute fail_at.



101
102
103
# File 'lib/rest_tor/instance.rb', line 101

def fail_at
  @fail_at
end

#successObject

Returns the value of attribute success.



101
102
103
# File 'lib/rest_tor/instance.rb', line 101

def success
  @success
end

#success_atObject

Returns the value of attribute success_at.



101
102
103
# File 'lib/rest_tor/instance.rb', line 101

def success_at
  @success_at
end

Instance Method Details

#fail!(e) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rest_tor/instance.rb', line 126

def fail!(e)
  if e.is_a?(::Exception)
    errors[e.class] ||= 0
    errors[e.class] += 1
  else
    erros[e] ||= 0
    erros[e] += 1
  end

  @tor.apply do
    @fail += 1
    @fail_at = ::Time.now
  end
end

#inspectObject



111
112
113
# File 'lib/rest_tor/instance.rb', line 111

def inspect
  "#<Counter success: #{@success}, fail: #{@fail}, succss_at: #{@success_at}, fail_at:#{@fail_at}>"
end

#success!Object



119
120
121
122
123
124
# File 'lib/rest_tor/instance.rb', line 119

def success!
  @tor.apply do
    @success += 1
    @success_at = ::Time.now
  end
end

#to_hObject



115
116
117
# File 'lib/rest_tor/instance.rb', line 115

def to_h
  { success: @success, fail: @fail, success_at: @success_at, fail_at: @fail_at, errors: @errors }
end