Class: BazaRb::Fake
- Inherits:
-
Object
- Object
- BazaRb::Fake
- Defined in:
- lib/baza-rb/fake.rb
Overview
Fake implementation of the Zerocracy API client for testing.
This class implements the same public interface as BazaRb but doesn't make any network connections. Instead, it returns predefined fake values and validates inputs to help catch errors during testing.
- Author
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
Copyright (c) 2024-2026 Yegor Bugayenko
- License
MIT
Instance Method Summary collapse
-
#balance ⇒ Float
Get current balance of the authenticated user.
-
#csrf ⇒ String
Get CSRF token from the server for authenticated requests.
-
#durable_find(pname, file) ⇒ Integer
Find a single durable.
-
#durable_load(id, file) ⇒ Object
Load a single durable from server to local file.
-
#durable_lock(id, owner) ⇒ Object
Lock a single durable.
-
#durable_place(pname, file) ⇒ Integer
Place a single durable file on the server.
-
#durable_save(id, file, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) ⇒ Object
Save a single durable from local file to server.
-
#durable_unlock(id, owner) ⇒ Object
Unlock a single durable.
-
#enter(name, badge, why, job) { ... } ⇒ String
Enter a valve to cache or retrieve a computation result.
-
#exit_code(id) ⇒ Integer
Read and return the exit code of the job.
-
#fee(tab, amount, summary, job) ⇒ Integer
Pay a fee associated with a job.
-
#finished?(id) ⇒ Boolean
Check if the job with this ID is finished already.
-
#lock(name, owner) ⇒ Object
Lock the name.
-
#name_exists?(name) ⇒ Boolean
Check whether the name of the job exists on the server.
-
#pull(id) ⇒ String
Pull factbase from the server.
-
#push(name, data, meta, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) ⇒ Integer
Push factbase to the server.
-
#recent(name) ⇒ Integer
Get the ID of the job by the name.
-
#stdout(id) ⇒ String
Read and return the stdout of the job.
-
#transfer(recipient, amount, summary, job: nil, badge: nil) ⇒ Integer
Transfer funds to another user.
-
#unlock(name, owner) ⇒ Object
Unlock the name.
-
#verified(id) ⇒ String
Read and return the verification verdict of the job.
-
#whoami ⇒ String
Get GitHub login name of the logged in user.
Instance Method Details
#balance ⇒ Float
Get current balance of the authenticated user.
191 192 193 |
# File 'lib/baza-rb/fake.rb', line 191 def balance 3.14 end |
#csrf ⇒ String
Get CSRF token from the server for authenticated requests.
258 259 260 |
# File 'lib/baza-rb/fake.rb', line 258 def csrf 'fake-csrf-token' end |
#durable_find(pname, file) ⇒ Integer
Find a single durable.
133 134 135 136 137 138 |
# File 'lib/baza-rb/fake.rb', line 133 def durable_find(pname, file) checkname(pname) raise(RuntimeError, 'The "file" is nil') if file.nil? raise(RuntimeError, 'The "file" may not be empty') if file.empty? 42 end |
#durable_load(id, file) ⇒ Object
Load a single durable from server to local file.
165 166 167 168 |
# File 'lib/baza-rb/fake.rb', line 165 def durable_load(id, file) checkid(id) raise(RuntimeError, 'The "file" of the durable is nil') if file.nil? end |
#durable_lock(id, owner) ⇒ Object
Lock a single durable.
174 175 176 177 |
# File 'lib/baza-rb/fake.rb', line 174 def durable_lock(id, owner) checkid(id) checkowner(owner) end |
#durable_place(pname, file) ⇒ Integer
Place a single durable file on the server.
145 146 147 148 149 |
# File 'lib/baza-rb/fake.rb', line 145 def durable_place(pname, file) checkname(pname) checkfile(file) 42 end |
#durable_save(id, file, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) ⇒ Object
Save a single durable from local file to server.
156 157 158 159 |
# File 'lib/baza-rb/fake.rb', line 156 def durable_save(id, file, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) # rubocop:disable Lint/UnusedMethodArgument checkid(id) checkfile(file) end |
#durable_unlock(id, owner) ⇒ Object
Unlock a single durable.
183 184 185 186 |
# File 'lib/baza-rb/fake.rb', line 183 def durable_unlock(id, owner) checkid(id) checkowner(owner) end |
#enter(name, badge, why, job) { ... } ⇒ String
Enter a valve to cache or retrieve a computation result.
247 248 249 250 251 252 253 |
# File 'lib/baza-rb/fake.rb', line 247 def enter(name, badge, why, job) checkname(name) raise(RuntimeError, "The badge '#{badge}' is not valid") unless badge.match?(/\A[a-zA-Z0-9_-]+\z/) raise(RuntimeError, 'The reason cannot be empty') if why.empty? checkid(job) unless job.nil? yield end |
#exit_code(id) ⇒ Integer
Read and return the exit code of the job.
78 79 80 81 |
# File 'lib/baza-rb/fake.rb', line 78 def exit_code(id) checkid(id) 0 end |
#fee(tab, amount, summary, job) ⇒ Integer
Pay a fee associated with a job.
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/baza-rb/fake.rb', line 225 def fee(tab, amount, summary, job) raise(RuntimeError, 'The "tab" is nil') if tab.nil? raise(RuntimeError, 'The "amount" is nil') if amount.nil? unless amount.is_a?(Float) || amount.is_a?(BigDecimal) raise(RuntimeError, 'The "amount" must be Float or BigDecimal') end raise(RuntimeError, 'The "amount" must be positive') unless amount.positive? raise(RuntimeError, 'The "job" is nil') if job.nil? raise(RuntimeError, 'The "job" must be Integer') unless job.is_a?(Integer) raise(RuntimeError, 'The "job" must be positive') unless job.positive? raise(RuntimeError, 'The "summary" is nil') if summary.nil? 42 end |
#finished?(id) ⇒ Boolean
Check if the job with this ID is finished already.
60 61 62 63 |
# File 'lib/baza-rb/fake.rb', line 60 def finished?(id) checkid(id) true end |
#lock(name, owner) ⇒ Object
Lock the name.
96 97 98 99 |
# File 'lib/baza-rb/fake.rb', line 96 def lock(name, owner) checkname(name) checkowner(owner) end |
#name_exists?(name) ⇒ Boolean
Check whether the name of the job exists on the server.
123 124 125 126 |
# File 'lib/baza-rb/fake.rb', line 123 def name_exists?(name) checkname(name) true end |
#pull(id) ⇒ String
Pull factbase from the server.
51 52 53 54 |
# File 'lib/baza-rb/fake.rb', line 51 def pull(id) checkid(id) Factbase.new.export end |
#push(name, data, meta, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) ⇒ Integer
Push factbase to the server.
39 40 41 42 43 44 45 |
# File 'lib/baza-rb/fake.rb', line 39 def push(name, data, , chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) # rubocop:disable Lint/UnusedMethodArgument checkname(name) raise(RuntimeError, 'The "data" of the job is nil') if data.nil? raise(RuntimeError, 'The data must be non-empty') if data.empty? raise(RuntimeError, 'The meta must be an array') unless .is_a?(Array) 42 end |
#recent(name) ⇒ Integer
Get the ID of the job by the name.
114 115 116 117 |
# File 'lib/baza-rb/fake.rb', line 114 def recent(name) checkname(name) 42 end |
#stdout(id) ⇒ String
Read and return the stdout of the job.
69 70 71 72 |
# File 'lib/baza-rb/fake.rb', line 69 def stdout(id) checkid(id) 'Fake stdout output' end |
#transfer(recipient, amount, summary, job: nil, badge: nil) ⇒ Integer
Transfer funds to another user.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/baza-rb/fake.rb', line 203 def transfer(recipient, amount, summary, job: nil, badge: nil) raise(RuntimeError, 'The "recipient" is nil') if recipient.nil? raise(RuntimeError, "The recipient #{recipient.inspect} is not valid") unless recipient.match?(/\A[a-zA-Z0-9-]+\z/) raise(RuntimeError, 'The "amount" is nil') if amount.nil? unless amount.is_a?(Float) || amount.is_a?(BigDecimal) raise(RuntimeError, 'The "amount" must be Float or BigDecimal') end raise(RuntimeError, 'The "amount" must be positive') unless amount.positive? raise(RuntimeError, 'The "summary" is nil') if summary.nil? raise(RuntimeError, "The summary #{summary.inspect} is empty") if summary.empty? checkid(job) unless job.nil? raise(RuntimeError, 'The "badge" must be a String') if !badge.nil? && !badge.is_a?(String) 42 end |
#unlock(name, owner) ⇒ Object
Unlock the name.
105 106 107 108 |
# File 'lib/baza-rb/fake.rb', line 105 def unlock(name, owner) checkname(name) checkowner(owner) end |
#verified(id) ⇒ String
Read and return the verification verdict of the job.
87 88 89 90 |
# File 'lib/baza-rb/fake.rb', line 87 def verified(id) checkid(id) 'fake-verdict' end |
#whoami ⇒ String
Get GitHub login name of the logged in user.
28 29 30 |
# File 'lib/baza-rb/fake.rb', line 28 def whoami 'torvalds' end |