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 ([email protected])
- Copyright
-
Copyright © 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(jname, 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(jname, file) ⇒ Integer
Place a single durable file on the server.
-
#durable_save(id, file) ⇒ 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.
-
#finish(id, zip) ⇒ Object
Submit a ZIP archive to finish a previously popped 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.
-
#pop(owner, zip) ⇒ Boolean
Pop the next available job from the server’s queue.
-
#pull(id) ⇒ String
Pull factbase from the server.
-
#push(name, data, meta) ⇒ 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) ⇒ 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.
187 188 189 |
# File 'lib/baza-rb/fake.rb', line 187 def balance 3.14 end |
#csrf ⇒ String
Get CSRF token from the server for authenticated requests.
264 265 266 |
# File 'lib/baza-rb/fake.rb', line 264 def csrf 'fake-csrf-token' end |
#durable_find(jname, file) ⇒ Integer
Find a single durable.
131 132 133 134 135 |
# File 'lib/baza-rb/fake.rb', line 131 def durable_find(jname, file) assert_name(jname) assert_file(file) 42 end |
#durable_load(id, file) ⇒ Object
Load a single durable from server to local file.
161 162 163 164 |
# File 'lib/baza-rb/fake.rb', line 161 def durable_load(id, file) assert_id(id) assert_file(file) end |
#durable_lock(id, owner) ⇒ Object
Lock a single durable.
170 171 172 173 |
# File 'lib/baza-rb/fake.rb', line 170 def durable_lock(id, owner) assert_id(id) assert_owner(owner) end |
#durable_place(jname, file) ⇒ Integer
Place a single durable file on the server.
142 143 144 145 146 |
# File 'lib/baza-rb/fake.rb', line 142 def durable_place(jname, file) assert_name(jname) assert_file(file) 42 end |
#durable_save(id, file) ⇒ Object
Save a single durable from local file to server.
152 153 154 155 |
# File 'lib/baza-rb/fake.rb', line 152 def durable_save(id, file) assert_id(id) assert_file(file) end |
#durable_unlock(id, owner) ⇒ Object
Unlock a single durable.
179 180 181 182 |
# File 'lib/baza-rb/fake.rb', line 179 def durable_unlock(id, owner) assert_id(id) assert_owner(owner) end |
#enter(name, badge, why, job) { ... } ⇒ String
Enter a valve to cache or retrieve a computation result.
253 254 255 256 257 258 259 |
# File 'lib/baza-rb/fake.rb', line 253 def enter(name, badge, why, job) assert_name(name) raise "The badge '#{badge}' is not valid" unless badge.match?(/^[a-zA-Z0-9_-]+$/) raise 'The reason cannot be empty' if why.empty? assert_id(job) unless job.nil? yield end |
#exit_code(id) ⇒ Integer
Read and return the exit code of the job.
76 77 78 79 |
# File 'lib/baza-rb/fake.rb', line 76 def exit_code(id) assert_id(id) 0 end |
#fee(tab, amount, summary, job) ⇒ Integer
Pay a fee associated with a job.
212 213 214 215 216 217 218 219 220 |
# File 'lib/baza-rb/fake.rb', line 212 def fee(tab, amount, summary, job) raise 'The "tab" is nil' if tab.nil? raise "The amount #{amount} must be a Float" unless amount.is_a?(Float) raise "The amount #{amount} must be positive" unless amount.positive? raise 'The "job" is nil' if job.nil? raise 'The "job" must be Integer' unless job.is_a?(Integer) raise 'The "summary" is nil' if summary.nil? 42 end |
#finish(id, zip) ⇒ Object
Submit a ZIP archive to finish a previously popped job.
240 241 242 243 |
# File 'lib/baza-rb/fake.rb', line 240 def finish(id, zip) assert_id(id) assert_file(zip) end |
#finished?(id) ⇒ Boolean
Check if the job with this ID is finished already.
58 59 60 61 |
# File 'lib/baza-rb/fake.rb', line 58 def finished?(id) assert_id(id) true end |
#lock(name, owner) ⇒ Object
Lock the name.
94 95 96 97 |
# File 'lib/baza-rb/fake.rb', line 94 def lock(name, owner) assert_name(name) assert_owner(owner) end |
#name_exists?(name) ⇒ Boolean
Check whether the name of the job exists on the server.
121 122 123 124 |
# File 'lib/baza-rb/fake.rb', line 121 def name_exists?(name) assert_name(name) true end |
#pop(owner, zip) ⇒ Boolean
Pop the next available job from the server’s queue.
rubocop:disable Naming/PredicateMethod
228 229 230 231 232 233 |
# File 'lib/baza-rb/fake.rb', line 228 def pop(owner, zip) assert_owner(owner) FileUtils.mkdir_p(File.dirname(zip)) FileUtils.touch(zip) true end |
#pull(id) ⇒ String
Pull factbase from the server.
49 50 51 52 |
# File 'lib/baza-rb/fake.rb', line 49 def pull(id) assert_id(id) Factbase.new.export end |
#push(name, data, meta) ⇒ Integer
Push factbase to the server.
38 39 40 41 42 43 |
# File 'lib/baza-rb/fake.rb', line 38 def push(name, data, ) assert_name(name) raise 'The data must be non-empty' if data.empty? raise 'The meta must be an array' unless .is_a?(Array) 42 end |
#recent(name) ⇒ Integer
Get the ID of the job by the name.
112 113 114 115 |
# File 'lib/baza-rb/fake.rb', line 112 def recent(name) assert_name(name) 42 end |
#stdout(id) ⇒ String
Read and return the stdout of the job.
67 68 69 70 |
# File 'lib/baza-rb/fake.rb', line 67 def stdout(id) assert_id(id) 'Fake stdout output' end |
#transfer(recipient, amount, summary) ⇒ Integer
Transfer funds to another user.
197 198 199 200 201 202 203 |
# File 'lib/baza-rb/fake.rb', line 197 def transfer(recipient, amount, summary, *) raise "The recipient #{recipient.inspect} is not valid" unless recipient.match?(/^[a-zA-Z0-9-]+$/) raise "The amount #{amount} must be a Float" unless amount.is_a?(Float) raise "The amount #{amount} must be positive" unless amount.positive? raise "The summary #{summary.inspect} is empty" if summary.empty? 42 end |
#unlock(name, owner) ⇒ Object
Unlock the name.
103 104 105 106 |
# File 'lib/baza-rb/fake.rb', line 103 def unlock(name, owner) assert_name(name) assert_owner(owner) end |
#verified(id) ⇒ String
Read and return the verification verdict of the job.
85 86 87 88 |
# File 'lib/baza-rb/fake.rb', line 85 def verified(id) assert_id(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 |