zold-score 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f144b35f41f572b478b56aa45dfb771f6df83f9ffbfbe725287a39dd51eb7329
4
- data.tar.gz: 8de2e35edc58105204a30479352af5283178b69b006e849b581c7492ac65197c
3
+ metadata.gz: fea20008b46e776779c40cc80438e7a95446cd3348ed819a447c2dfe9896b2f8
4
+ data.tar.gz: e077d65ccd21ffc3a331c04d45eed5e18f4af4b11cdec0c26b4ed9b27c6141bf
5
5
  SHA512:
6
- metadata.gz: 157659238d2e181462a65694ae60801ac49fae88e6656e709b521a6f4df172a33afbdbc759e1a81ac81ee77928cf3d552331c0fced3481d9e9d3d06fb1407f7a
7
- data.tar.gz: 202d378faeba0c50d0d357436e3bf844c0770c12173d47a32b331369f835298d79cc759ca5c9f20938ab7929a96678ee42d0e7f570656e80268069740be5c079
6
+ metadata.gz: beebaa5ce22becb6404f983d4b1214c64dc227da9a6868715c46e5f36c81fc8334c334c5996b5ecec1dc89d63ff257b6300ef1835b8ae5e0fd2f1ce6b2ad4a9e
7
+ data.tar.gz: a1ee92aee0494c7c6dd74f423ac83d3bc9d5a220340d4340e45bb09c142ef1d948c658a037c81df79eebbd0a156d901276478480e7a2f85a80fe9f183f7cc23c
@@ -7,14 +7,18 @@ Layout/EmptyLineAfterGuardClause:
7
7
  Layout/EndOfLine:
8
8
  EnforcedStyle: lf
9
9
  Metrics/AbcSize:
10
- Max: 25
10
+ Max: 45
11
11
  Metrics/BlockLength:
12
12
  Max: 30
13
13
  Metrics/MethodLength:
14
- Max: 25
14
+ Max: 40
15
15
  Metrics/ClassLength:
16
16
  Max: 180
17
17
  Metrics/ParameterLists:
18
18
  Max: 7
19
19
  Layout/AlignParameters:
20
20
  Enabled: false
21
+ Metrics/CyclomaticComplexity:
22
+ Max: 20
23
+ Metrics/PerceivedComplexity:
24
+ Max: 20
@@ -48,17 +48,53 @@ module Zold
48
48
  # increase. The number is set empirically.
49
49
  STRENGTH = 8
50
50
 
51
+ # The maximum amount of hours a score can stay "fresh." After that it
52
+ # will be considered expired.
53
+ BEST_BEFORE = 24
54
+
51
55
  attr_reader :time, :host, :port, :invoice, :suffixes, :strength, :created
52
56
 
53
57
  # Makes a new object of the class.
54
58
  def initialize(time: Time.now, host:, port: 4096, invoice:, suffixes: [],
55
59
  strength: Score::STRENGTH, created: Time.now)
60
+ raise 'Time can\'t be nil' if time.nil?
61
+ unless time.is_a?(Time)
62
+ raise "Time must be Time, while #{time.class.name} is provided"
63
+ end
56
64
  @time = time
65
+ raise 'Host can\'t be nil' if host.nil?
66
+ unless host =~ /^[0-9a-z\.\-]+$/
67
+ raise "Host \"#{host}\" is in a wrong format"
68
+ end
57
69
  @host = host
70
+ raise 'Port can\'t be nil' if port.nil?
71
+ unless port.is_a?(Integer)
72
+ raise "Port must be Integer, while #{port.class.name} is provided"
73
+ end
74
+ if port > 65_535
75
+ raise "Port must be less than 65535, while #{port} is provided"
76
+ end
77
+ unless port.positive?
78
+ raise "Port must be positive integer, while #{port} is provided"
79
+ end
58
80
  @port = port
81
+ raise 'Invoice can\'t be nil' if invoice.nil?
82
+ unless invoice =~ /^[a-zA-Z0-9]{8,32}@[a-f0-9]{16}$/
83
+ raise "Invoice \"#{invoice}\" is wrong"
84
+ end
59
85
  @invoice = invoice
86
+ raise 'Suffixes can\'t be nil' if suffixes.nil?
87
+ raise 'Suffixes are not an array' unless suffixes.is_a?(Array)
60
88
  @suffixes = suffixes
89
+ raise 'Strength can\'t be nil' if strength.nil?
90
+ unless strength.positive?
91
+ raise "Strength must be positive integer, while #{strength} is provided"
92
+ end
61
93
  @strength = strength
94
+ raise 'Created can\'t be nil' if created.nil?
95
+ unless created.is_a?(Time)
96
+ raise "Created must be Time, while #{created.class.name} is provided"
97
+ end
62
98
  @created = created
63
99
  end
64
100
 
@@ -70,23 +106,41 @@ module Zold
70
106
 
71
107
  # Parses it back from the JSON.
72
108
  def self.parse_json(json)
73
- unless json['time'] =~ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
74
- raise "Time in JSON is broken: #{json}"
75
- end
76
- raise "Host is wrong: #{json}" unless json['host'] =~ /^[0-9a-z\.\-]+$/
77
- raise "Port is wrong: #{json}" unless json['port'].is_a?(Integer)
78
- unless json['invoice'] =~ /^[a-zA-Z0-9]{8,32}@[a-f0-9]{16}$/
79
- raise "Invoice is wrong: #{json}"
80
- end
81
- raise "Suffixes not array: #{json}" unless json['suffixes'].is_a?(Array)
109
+ raise 'JSON can\'t be nil' if json.nil?
82
110
  Score.new(
83
- time: Time.parse(json['time']), host: json['host'],
84
- port: json['port'], invoice: json['invoice'],
111
+ time: Time.parse(json['time']),
112
+ host: json['host'],
113
+ port: json['port'],
114
+ invoice: json['invoice'],
85
115
  suffixes: json['suffixes'],
86
116
  strength: json['strength']
87
117
  )
88
118
  end
89
119
 
120
+ # Compare with another Score, by text.
121
+ def ==(other)
122
+ raise 'Can\'t compare with nil' if other.nil?
123
+ to_s == other.to_s
124
+ end
125
+
126
+ # Compare with another Score, by value.
127
+ def <(other)
128
+ raise 'Can\'t compare with nil' if other.nil?
129
+ value < other.value
130
+ end
131
+
132
+ # Compare with another Score, by value.
133
+ def >(other)
134
+ raise 'Can\'t compare with nil' if other.nil?
135
+ value > other.value
136
+ end
137
+
138
+ # Compare with another Score, by value.
139
+ def <=>(other)
140
+ raise 'Can\'t compare with nil' if other.nil?
141
+ value <=> other.value
142
+ end
143
+
90
144
  # Converts it to a string. You can parse it back
91
145
  # using <tt>parse()</tt>.
92
146
  def to_s
@@ -104,6 +158,7 @@ module Zold
104
158
 
105
159
  # Parses it back from the text generated by <tt>to_s</tt>.
106
160
  def self.parse(text)
161
+ raise 'Can\'t parse nil' if text.nil?
107
162
  parts = text.split(' ', 7)
108
163
  raise "Invalid score, not enough parts in \"#{text}\"" if parts.length < 6
109
164
  Score.new(
@@ -151,6 +206,7 @@ module Zold
151
206
  # Returns a new score, which is a copy of the current one, but the amount
152
207
  # of hash suffixes is reduced to the <tt>max</tt> provided.
153
208
  def reduced(max = 4)
209
+ raise 'Max can\'t be nil' if max.nil?
154
210
  raise "Max can't be negative: #{max}" if max.negative?
155
211
  Score.new(
156
212
  time: @time, host: @host, port: @port, invoice: @invoice,
@@ -183,7 +239,8 @@ module Zold
183
239
  end
184
240
 
185
241
  # Returns TRUE if the age of the score is over 24 hours.
186
- def expired?(hours = 24)
242
+ def expired?(hours = BEST_BEFORE)
243
+ raise 'Hours can\'t be nil' if hours.nil?
187
244
  age > hours * 60 * 60
188
245
  end
189
246
 
@@ -54,9 +54,18 @@ class TestScore < Minitest::Test
54
54
  assert_equal(64, score.hash.length)
55
55
  end
56
56
 
57
+ def test_compares
58
+ score = Zold::Score.new(
59
+ time: Time.parse('2017-07-19T21:24:51Z'),
60
+ host: 'localhost', port: 443, invoice: 'NOPREFIX@ffffffffffffffff',
61
+ suffixes: %w[A B C D E F G]
62
+ )
63
+ assert(score == score.reduced(10))
64
+ end
65
+
57
66
  def test_drops_to_zero_when_expired
58
67
  score = Zold::Score.new(
59
- time: Time.now - 24 * 60 * 60,
68
+ time: Time.now - Zold::Score::BEST_BEFORE * 60 * 60,
60
69
  host: 'some-host', port: 9999, invoice: 'NOPREFIX@ffffffffffffffff',
61
70
  strength: 50
62
71
  ).next
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
29
29
  s.rubygems_version = '2.2'
30
30
  s.required_ruby_version = '>=2.3'
31
31
  s.name = 'zold-score'
32
- s.version = '0.4.3'
32
+ s.version = '0.4.4'
33
33
  s.license = 'MIT'
34
34
  s.summary = 'Zold score'
35
35
  s.description = 'Score calculating Ruby Gem for Zold'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zold-score
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-11 00:00:00.000000000 Z
11
+ date: 2018-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov