Class: Zold::Score
- Inherits:
-
Object
- Object
- Zold::Score
- Defined in:
- lib/zold/score.rb
Overview
Score
Constant Summary collapse
- STRENGTH =
Default strength for the entire system, in production mode.
6- ZERO =
The default no-value score.
Score.new(Time.now, 'localhost', 80, 'NOPREFIX@ffffffffffffffff')
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#invoice ⇒ Object
readonly
Returns the value of attribute invoice.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#strength ⇒ Object
readonly
Returns the value of attribute strength.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Class Method Summary collapse
- .parse(text, strength: STRENGTH) ⇒ Object
- .parse_json(json) ⇒ Object
- .parse_text(text, strength: STRENGTH) ⇒ Object
Instance Method Summary collapse
- #age_hours ⇒ Object
- #expired? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(time, host, port, invoice, suffixes = [], strength: STRENGTH) ⇒ Score
constructor
time: UTC ISO 8601 string.
- #next ⇒ Object
- #prefix ⇒ Object
- #reduced(max = 4) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #to_text ⇒ Object
- #valid? ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(time, host, port, invoice, suffixes = [], strength: STRENGTH) ⇒ Score
time: UTC ISO 8601 string
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/zold/score.rb', line 37 def initialize(time, host, port, invoice, suffixes = [], strength: STRENGTH) raise "Invalid host name: #{host}" unless host =~ /^[a-z0-9\.-]+$/ raise 'Time must be of type Time' unless time.is_a?(Time) raise 'Port must be of type Integer' unless port.is_a?(Integer) raise "Invalid TCP port: #{port}" if port <= 0 || port > 65_535 raise "Invoice '#{invoice}' has wrong format" unless invoice =~ /^[a-zA-Z0-9]{8,32}@[a-f0-9]{16}$/ @time = time @host = host @port = port @invoice = invoice @suffixes = suffixes @strength = strength end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
35 36 37 |
# File 'lib/zold/score.rb', line 35 def host @host end |
#invoice ⇒ Object (readonly)
Returns the value of attribute invoice.
35 36 37 |
# File 'lib/zold/score.rb', line 35 def invoice @invoice end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
35 36 37 |
# File 'lib/zold/score.rb', line 35 def port @port end |
#strength ⇒ Object (readonly)
Returns the value of attribute strength.
35 36 37 |
# File 'lib/zold/score.rb', line 35 def strength @strength end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
35 36 37 |
# File 'lib/zold/score.rb', line 35 def time @time end |
Class Method Details
.parse(text, strength: STRENGTH) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/zold/score.rb', line 67 def self.parse(text, strength: STRENGTH) m = Regexp.new( '^' + [ '([0-9]+:)', '(?<time>[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)', '(?<host>[0-9a-z\.\-]+)', '(?<port>[0-9]+)', '(?<invoice>[a-zA-Z0-9]{8,32}@[a-f0-9]{16})', '(?<suffixes>[a-zA-Z0-9 ]+)' ].join(' ') + '$' ).match(text) raise "Invalid score '#{text}'" if m.nil? Score.new( Time.parse(m[:time]), m[:host], m[:port].to_i, m[:invoice], m[:suffixes].split(' '), strength: strength ) end |
.parse_json(json) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/zold/score.rb', line 54 def self.parse_json(json) raise "Time in JSON is broken: #{json}" unless json['time'] =~ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/ raise "Host is wrong: #{json}" unless json['host'] =~ /^[0-9a-z\.\-]+$/ raise "Port is wrong: #{json}" unless json['port'].is_a?(Integer) raise "Invoice is wrong: #{json}" unless json['invoice'] =~ /^[a-zA-Z0-9]{8,32}@[a-f0-9]{16}$/ raise "Suffixes not array: #{json}" unless json['suffixes'].is_a?(Array) Score.new( Time.parse(json['time']), json['host'], json['port'], json['invoice'], json['suffixes'], strength: json['strength'] ) end |
.parse_text(text, strength: STRENGTH) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/zold/score.rb', line 87 def self.parse_text(text, strength: STRENGTH) parts = text.split(' ', 6) Score.new( Time.at(parts[0].hex), parts[1], parts[2].hex, "#{parts[3]}@#{parts[4]}", parts[5].split(' '), strength: strength ) end |
Instance Method Details
#age_hours ⇒ Object
164 165 166 |
# File 'lib/zold/score.rb', line 164 def age_hours (Time.now - @time) / 60 end |
#expired? ⇒ Boolean
168 169 170 |
# File 'lib/zold/score.rb', line 168 def expired? @time < Time.now - 24 * 60 * 60 end |
#hash ⇒ Object
99 100 101 102 103 104 |
# File 'lib/zold/score.rb', line 99 def hash raise 'Score has zero value, there is no hash' if @suffixes.empty? @suffixes.reduce(prefix) do |pfx, suffix| Digest::SHA256.hexdigest(pfx + ' ' + suffix)[0, 63] end end |
#next ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/zold/score.rb', line 150 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, @invoice, @suffixes + [suffix], strength: @strength ) return score if score.valid? idx += 1 end end |
#prefix ⇒ Object
172 173 174 |
# File 'lib/zold/score.rb', line 172 def prefix "#{@time.utc.iso8601} #{@host} #{@port} #{@invoice}" end |
#reduced(max = 4) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/zold/score.rb', line 142 def reduced(max = 4) raise "The length of the score is #{@suffixes.count}, can't reduce to #{max}" if max > @suffixes.count Score.new( @time, @host, @port, @invoice, @suffixes[0..max - 1], strength: @strength ) end |
#to_h ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/zold/score.rb', line 129 def to_h { value: value, host: @host, port: @port, invoice: @invoice, time: @time.utc.iso8601, suffixes: @suffixes, strength: @strength, hash: value.zero? ? nil : hash } end |
#to_s ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/zold/score.rb', line 118 def to_s [ "#{value}:", @time.utc.iso8601, @host, @port, @invoice, @suffixes.join(' ') ].join(' ') end |
#to_text ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/zold/score.rb', line 106 def to_text pfx, bnf = @invoice.split('@') [ @time.to_i.to_s(16), @host, @port.to_s(16), pfx, bnf, @suffixes.join(' ') ].join(' ') end |
#valid? ⇒ Boolean
176 177 178 |
# File 'lib/zold/score.rb', line 176 def valid? @suffixes.empty? || hash.end_with?('0' * @strength) end |
#value ⇒ Object
180 181 182 |
# File 'lib/zold/score.rb', line 180 def value @suffixes.length end |