Class: Zold::Score
- Inherits:
-
Object
- Object
- Zold::Score
- Defined in:
- lib/zold/score.rb
Overview
Score
Constant Summary collapse
- DEFAULT_STRENGTH =
8
- ZERO =
Score.new(Time.now, 'localhost', 80)
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(time, host, port, suffixes = [], strength: DEFAULT_STRENGTH) ⇒ Score
constructor
time: UTC ISO 8601 string.
- #next ⇒ Object
- #reduced(max = 4) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(time, host, port, suffixes = [], strength: DEFAULT_STRENGTH) ⇒ Score
time: UTC ISO 8601 string
34 35 36 37 38 39 40 41 42 |
# File 'lib/zold/score.rb', line 34 def initialize(time, host, port, suffixes = [], strength: DEFAULT_STRENGTH) raise 'Time must be of type Time' unless time.is_a?(Time) raise 'Port must be of type Integer' unless port.is_a?(Integer) @time = time @host = host @port = port @suffixes = suffixes @strength = strength end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
32 33 34 |
# File 'lib/zold/score.rb', line 32 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
32 33 34 |
# File 'lib/zold/score.rb', line 32 def port @port end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
32 33 34 |
# File 'lib/zold/score.rb', line 32 def time @time end |
Class Method Details
.parse(text, strength: DEFAULT_STRENGTH) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/zold/score.rb', line 46 def self.parse(text, strength: DEFAULT_STRENGTH) _, time, host, port, suffixes = text.split(' ', 5) Score.new( Time.parse(time), host, port.to_i, suffixes.split(' '), strength: strength ) end |
Instance Method Details
#next ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/zold/score.rb', line 73 def next raise 'This score is not valid' unless valid? idx = 0 loop do suffix = idx.to_s(16) score = Score.new( @time, @host, @port, @suffixes + [suffix], strength: @strength ) return score if score.valid? idx += 1 end end |
#reduced(max = 4) ⇒ Object
69 70 71 |
# File 'lib/zold/score.rb', line 69 def reduced(max = 4) Score.new(@time, @host, @port, @suffixes[0..max - 1], strength: @strength) end |
#to_h ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/zold/score.rb', line 58 def to_h { value: value, host: @host, port: @port, time: @time.utc.iso8601, suffixes: @suffixes, strength: @strength } end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/zold/score.rb', line 54 def to_s "#{value}: #{@time.utc.iso8601} #{@host} #{@port} #{@suffixes.join(' ')}" end |
#valid? ⇒ Boolean
87 88 89 90 91 92 93 94 95 |
# File 'lib/zold/score.rb', line 87 def valid? start = "#{@time.utc.iso8601} #{@host} #{@port}" @suffixes.reduce(start) do |prefix, suffix| hex = Digest::SHA256.hexdigest(prefix + ' ' + suffix) return false unless hex.end_with?('0' * @strength) hex[0, 19] end true end |
#value ⇒ Object
97 98 99 |
# File 'lib/zold/score.rb', line 97 def value @suffixes.length end |