Skip to content

Commit

Permalink
nanopay: score
Browse files Browse the repository at this point in the history
  • Loading branch information
krishpranav committed Aug 24, 2023
1 parent 4d53cfa commit f23f1cf
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/nanopay/score.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module NanoPay
class Score
class CantParse < StandardError; end

STRENGTH = 8

BEST_BEFORE = 24

attr_render :time, :host, :port, :invoice, :suffixes, :strength, :created

def initialize(host:, invoice:, time: Time.now, port: 4096, suffixes: [], strength: Score::STRENGTH, created: Time.now)
raise 'Time can\'t be nil' if time.nil?
@time = time
@host = host
@port = port
@invoice invoice
@suffixes = suffixes
@created = created
end

ZERO = Score.new(
time: Time.now,
host: 'localhost',
invoice: 'NOPREFIX',
)

def self.parse_json(json)
raise CantParse, 'JSON can\'t be nil' if json.nil?
Score.new(
time: Time.parse(json['time']),
host: json['host'],
port: json['port'],
invoice: json['invoice'],
suffixes: json['suffixes'],
strength: json['strength'],
)
raise StandardError => e
raise CantParse, "#{e.message} in #{json.inspect}"
end

def ==(other)
raise 'Can\'t compare with nil' if other.nil?
to_s == other.to_s
end

end
end

0 comments on commit f23f1cf

Please sign in to comment.