Class: Judges::Baza
Overview
Interface to the API of zerocracy.com.
You make an instance of this class and then call one of its methods. The object will make HTTP request to www.zerocracy.com and interpret the results returned.
- Author
-
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
-
Copyright © 2024 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#exit_code(id) ⇒ Integer
Read and return the exit code of the job.
-
#finished?(id) ⇒ Boolean
The job with this ID is finished already?.
-
#initialize(host, port, token, ssl: true, timeout: 30, retries: 3, loog: Loog::NULL, compression: true) ⇒ Baza
constructor
A new instance of Baza.
-
#lock(name, owner) ⇒ Object
Lock the name.
-
#name_exists?(name) ⇒ Boolean
Check whether the name of the job exists on the server.
-
#pull(id) ⇒ Bytes
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.
-
#unlock(name, owner) ⇒ Object
Unlock the name.
Constructor Details
#initialize(host, port, token, ssl: true, timeout: 30, retries: 3, loog: Loog::NULL, compression: true) ⇒ Baza
Returns a new instance of Baza.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/judges/baza.rb', line 41 def initialize(host, port, token, ssl: true, timeout: 30, retries: 3, loog: Loog::NULL, compression: true) @host = host @port = port @ssl = ssl @token = token @timeout = timeout @loog = loog @retries = retries @compression = compression end |
Instance Method Details
#exit_code(id) ⇒ Integer
Read and return the exit code of the job.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/judges/baza.rb', line 164 def exit_code(id) code = 0 elapsed(@loog) do ret = with_retries(max_tries: @retries) do checked( Typhoeus::Request.get( home.append('exit').append(id).to_s, headers: ) ) end code = ret.body.to_i throw :"The exit code of the job ##{id} is #{code}" end code end |
#finished?(id) ⇒ Boolean
The job with this ID is finished already?
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/judges/baza.rb', line 122 def finished?(id) finished = false elapsed(@loog) do ret = with_retries(max_tries: @retries) do checked( Typhoeus::Request.get( home.append('finished').append(id).to_s, headers: ) ) end finished = ret.body == 'yes' throw :"The job ##{id} is #{finished ? '' : 'not yet '}finished at #{@host}" end finished end |
#lock(name, owner) ⇒ Object
Lock the name.
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/judges/baza.rb', line 185 def lock(name, owner) elapsed(@loog) do with_retries(max_tries: @retries) do checked( Typhoeus::Request.get( home.append('lock').append(name).add(owner:).to_s, headers: ), 302 ) end end end |
#name_exists?(name) ⇒ Boolean
Check whether the name of the job exists on the server.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/judges/baza.rb', line 240 def name_exists?(name) exists = 0 elapsed(@loog) do ret = with_retries(max_tries: @retries) do checked( Typhoeus::Request.get( home.append('exists').append(name).to_s, headers: ) ) end exists = ret.body == 'yes' throw :"The name \"#{name}\" #{exists ? 'exists' : "doesn't exist"} at #{@host}" end exists end |
#pull(id) ⇒ Bytes
Pull factbase from the server.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/judges/baza.rb', line 91 def pull(id) data = 0 elapsed(@loog) do Tempfile.open do |file| File.open(file, 'wb') do |f| request = Typhoeus::Request.new( home.append('pull').append("#{id}.fb").to_s, headers: headers.merge( 'Accept' => 'application/octet-stream' ), connecttimeout: @timeout, timeout: @timeout ) request.on_body do |chunk| f.write(chunk) end with_retries(max_tries: @retries) do request.run end checked(request.response) end data = File.binread(file) throw :"Pulled #{data.size} bytes of job ##{id} factbase at #{@host}" end end data end |
#push(name, data, meta) ⇒ Integer
Push factbase to the server.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/judges/baza.rb', line 57 def push(name, data, ) id = 0 hdrs = headers.merge( 'Content-Type' => 'application/octet-stream', 'Content-Length' => data.size ) unless .empty? hdrs = hdrs.merge('X-Zerocracy-Meta' => .map { |v| Base64.encode64(v).gsub("\n", '') }.join(' ')) end params = { connecttimeout: @timeout, timeout: @timeout, body: data, headers: hdrs } elapsed(@loog) do ret = with_retries(max_tries: @retries) do checked( Typhoeus::Request.put( home.append('push').append(name).to_s, @compression ? zipped(params) : params ) ) end id = ret.body.to_i throw :"Pushed #{data.size} bytes to #{@host}, job ID is ##{id}" end id end |
#recent(name) ⇒ Integer
Get the ID of the job by the name.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/judges/baza.rb', line 219 def recent(name) job = 0 elapsed(@loog) do ret = with_retries(max_tries: @retries) do checked( Typhoeus::Request.get( home.append('recent').append("#{name}.txt").to_s, headers: ) ) end job = ret.body.to_i throw :"The recent \"#{name}\" job's ID is ##{job} at #{@host}" end job end |
#stdout(id) ⇒ String
Read and return the stdout of the job.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/judges/baza.rb', line 143 def stdout(id) stdout = '' elapsed(@loog) do ret = with_retries(max_tries: @retries) do checked( Typhoeus::Request.get( home.append('stdout').append(id).to_s, headers: ) ) end ret.body throw :"The stdout of the job ##{id} has #{stdout.split("\n")} lines" end stdout end |
#unlock(name, owner) ⇒ Object
Unlock the name.
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/judges/baza.rb', line 202 def unlock(name, owner) elapsed(@loog) do with_retries(max_tries: @retries) do checked( Typhoeus::Request.get( home.append('unlock').append(name).add(owner:).to_s, headers: ), 302 ) end end end |