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.



106
107
108
109
110
111
112
113
# File 'lib/rest_tor/instance.rb', line 106

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.



105
106
107
# File 'lib/rest_tor/instance.rb', line 105

def errors
  @errors
end

#failObject

Returns the value of attribute fail.



105
106
107
# File 'lib/rest_tor/instance.rb', line 105

def fail
  @fail
end

#fail_atObject

Returns the value of attribute fail_at.



105
106
107
# File 'lib/rest_tor/instance.rb', line 105

def fail_at
  @fail_at
end

#successObject

Returns the value of attribute success.



105
106
107
# File 'lib/rest_tor/instance.rb', line 105

def success
  @success
end

#success_atObject

Returns the value of attribute success_at.



105
106
107
# File 'lib/rest_tor/instance.rb', line 105

def success_at
  @success_at
end

Instance Method Details

#fail!(e) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rest_tor/instance.rb', line 130

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



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

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

#success!Object



123
124
125
126
127
128
# File 'lib/rest_tor/instance.rb', line 123

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

#to_hObject



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

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