Class: Flatulent
Defined Under Namespace
Classes: EncryptionError, Error, TimeBombError
Constant Summary collapse
- VERSION =
'0.0.3'
Class Method Summary collapse
Instance Method Summary collapse
-
#captcha! ⇒ Object
–}}}.
-
#css ⇒ Object
–}}}.
-
#css_for(hash) ⇒ Object
–}}}.
-
#element! ⇒ Object
–}}}.
-
#encrypt(string) ⇒ Object
–{{{.
-
#encrypted ⇒ Object
–}}}.
-
#figlet! ⇒ Object
–}}}.
-
#form! ⇒ Object
–}}}.
-
#form_tags! ⇒ Object
–}}}.
-
#generate_random_string ⇒ Object
–}}}.
-
#getopts(options) ⇒ Object
–}}}.
-
#initialize(arg = {}) ⇒ Flatulent
constructor
–{{{.
-
#munge(string) ⇒ Object
–}}}.
-
#random_char ⇒ Object
–}}}.
-
#random_charset ⇒ Object
–}}}.
-
#rchar(*list) ⇒ Object
–}}}.
-
#to_s ⇒ Object
–}}}.
-
#vapour! ⇒ Object
–}}}.
Constructor Details
#initialize(arg = {}) ⇒ Flatulent
–{{{
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/flatulent.rb', line 256 def initialize arg = {} #--{{{ if Hash === arg opt = getopts arg @size = Integer opt[ 'size', 4 ] @string = String opt[ 'string', generate_random_string ] else opt = getopts Hash.new @string = String arg @size = @string.size end @font = String opt[ 'font', 'big' ] @noise = Float opt[ 'noise', 0.22 ] @id = String opt[ 'id', 'flatulent' ] @action = String opt[ 'action' ] @ttl = Integer opt[ 'ttl', 300 ] @horizontal_fudge_factor = Integer opt[ 'horizontal_fudge_factor', 4 ] @vertical_fudge_factor = Integer opt[ 'vertical_fudge_factor', 4 ] @vapour_chars = Integer opt[ 'vapour_chars', 5 * @size ] @vapour_level = Float opt[ 'vapour_level', 0.77 ] figlet! vapour! captcha! element! form! end |
Class Method Details
.libdir ⇒ Object
4 |
# File 'lib/flatulent.rb', line 4 def self.libdir() File.(__FILE__).gsub(%r/\.rb$/, '') end |
Instance Method Details
#captcha! ⇒ Object
–}}}
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/flatulent.rb', line 423 def captcha! #--{{{ @captcha = " " * @figlet.size space = " "[0] newline = "\n"[0] noisy = %w`| / - _ ( ) \\ ! [ ]` @figlet.size.times do |i| fbyte = @figlet[i] vbyte = @vapour[i] if fbyte == newline @captcha[i] = newline elsif fbyte == space #if vbyte == space #@captcha[i] = noisy[ rand(noisy.size) ] #else @captcha[i] = vbyte #end else @captcha[i] = fbyte end end =begin alpha = ('A'..'Z').to_a + ('a'..'z').to_a transform = { '|' => rchar(%w'. * \` + , ; : '), '/' => rchar(%w'\\ ] [ { } ( ) @' + alpha), '\\' => rchar(%w'/ ] [ { } ( ) @' + alpha), '-' => rchar(%w'_ * ^ # @ ~'), '_' => rchar(%w'- * ^ # @ ~'), '(' => rchar(%w'\\ / ] [ { } ) @' + alpha), ')' => rchar(%w'\\ / ] [ { } ( @' + alpha), } (@noise * @captcha.size).ceil.times do |i| cbyte = @captcha[i] if cell =~ %r"nbsp;" end =end @captcha end |
#css_for(hash) ⇒ Object
–}}}
533 534 535 |
# File 'lib/flatulent.rb', line 533 def css_for hash #--{{{ hash.map{|kv| kv.join ':' }.join ';' end |
#element! ⇒ Object
–}}}
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/flatulent.rb', line 467 def element! #--{{{ rows = [] rows << (row = []) chars = @captcha.split %r// size = chars.size last = size - 1 chars.each_with_index do |char, idx| content = case char when %r/\n/o "<br>" when %r/\s/o " " when %r/([^\s])/o CGI.escapeHTML $1 end Array.new(rand(10)){ content = "<span>#{ content }</span>"} row << content rows << (row = []) unless idx == last end =begin noisy = %w`| / - _ ( ) \\` alpha = ('A'..'Z').to_a + ('a'..'z').to_a transform = { '|' => rchar(%w'. * \` + , ; : '), '/' => rchar(%w'\\ ] [ { } ( ) @' + alpha), '\\' => rchar(%w'/ ] [ { } ( ) @' + alpha), '-' => rchar(%w'_ * ^ # @ ~'), '_' => rchar(%w'- * ^ # @ ~'), '(' => rchar(%w'\\ / ] [ { } ) @' + alpha), ')' => rchar(%w'\\ / ] [ { } ( @' + alpha), } (@noise * chars.size).ceil.times do y = rand(rows.size - 1) x = rand(rows.first.size - 1) cell = rows[y][x] next if cell =~ %r"br" char = if cell =~ %r"nbsp;" noisy[ rand(noisy.size) ] else #(transform[cell] || lambda{'.'}).call nil end rows[y][x] = char if char end =end content = rows.join @element = "<pre id='#{ @id }_element' style='#{ css }'>#{ content }</pre>" end |
#encrypt(string) ⇒ Object
–{{{
553 554 555 |
# File 'lib/flatulent.rb', line 553 def encrypt string #--{{{ self.class.encrypt string.to_s end |
#encrypted ⇒ Object
–}}}
557 558 559 |
# File 'lib/flatulent.rb', line 557 def encrypted #--{{{ self.class.encrypt @string end |
#figlet! ⇒ Object
–}}}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/flatulent.rb', line 306 def figlet! #--{{{ #spaced = @string.split(%r//).join #' ' #.join(' ') fontfile = File.join fontdir, "#{ @font }.flf" font = Text::Figlet::Font.new fontfile typesetter = Text::Figlet::Typesetter.new font @figlets = [] chars = @string.split %r// #chars.each{|char| @figlets << typesetter[char]} # horz fudge chars.each do |char| figlet = typesetter[char] rows = figlet.split %r/\n/ height = rows.size offset_l = " " * rand(@horizontal_fudge_factor) offset_r = " " * rand(@horizontal_fudge_factor) rows.size.times do |i| rows[i] = "#{ offset_l }#{ rows[i] }#{ offset_r }" end @figlets.push rows end # vert fudge @figlets.map! do |rows| width = rows.first.size offset_t = Array.new(rand(@vertical_fudge_factor)).map{ " " * width } offset_b = Array.new(rand(@vertical_fudge_factor)).map{ " " * width } offset_t + rows + offset_b end # vert normalize vert tallest = @figlets.map{|rows| rows.size}.max @figlets.size.times do |i| rows = @figlets[i] unless rows.size == tallest width = rows.first.size until rows.size == tallest blank = " " * width rows << blank end end end # generate final grid grid = [] grid << (gridrow = '') catch :done do loop do @figlets.each do |rows| row = rows.shift || throw(:done) gridrow << row end #gridrow << "\n" grid << (gridrow = '') end end # trim top and bottom iff emtpy =begin empty = lambda{|row| not rnow.detect{|cell| cell !~ %r"br|nbsp"}} grid.delete_if{|row| row.strip.empty?} =end @figlet_width = grid.first.size @figlet_height = grid.size @figlet = grid.join("\n") end |
#form! ⇒ Object
–}}}
565 566 567 568 569 570 571 572 573 |
# File 'lib/flatulent.rb', line 565 def form! #--{{{ action = "action='#{ @action }'" @form = <<-html <form method='post' #{ action }> #{ } <input type='submit' name='#{ @id }[submit]' id='#{ @id }_submit' value='Submit' /> </form> html end |
#form_tags! ⇒ Object
–}}}
537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/flatulent.rb', line 537 def #--{{{ n = @string.scan(%r/\w/).size string = @string timebomb = Time.now.utc.to_i + @ttl @form_tags = <<-html #{ element } <p id='#{ @id }_instructions'> Please enter the #{ n } large characters (A-Z, 1-9) shown. </p> <input type='textarea' name='#{ @id }[c]' id='#{ @id }_textarea' /> <input type='hidden' name='#{ @id }[s]' id='#{ @id }_e' value='#{ encrypt string }' /> <input type='hidden' name='#{ @id }[t]' id='#{ @id }_v' value='#{ encrypt timebomb }' /> html end |
#generate_random_string ⇒ Object
–}}}
291 292 293 294 295 296 297 298 299 |
# File 'lib/flatulent.rb', line 291 def generate_random_string #--{{{ chars = [] n = random_charset.size - 1 loop { ( chars << random_charset[rand(n)] ).uniq! break if chars.size >= @size or random_charset.size == chars.size } chars end |
#getopts(options) ⇒ Object
–}}}
583 584 585 |
# File 'lib/flatulent.rb', line 583 def getopts #--{{{ self.class.getopts end |
#munge(string) ⇒ Object
–}}}
561 562 563 |
# File 'lib/flatulent.rb', line 561 def munge string #--{{{ self.class.munge string end |
#random_char ⇒ Object
–}}}
301 302 303 304 |
# File 'lib/flatulent.rb', line 301 def random_char #--{{{ n = random_charset.size - 1 random_charset[rand(n)] end |
#random_charset ⇒ Object
–}}}
286 287 288 289 |
# File 'lib/flatulent.rb', line 286 def random_charset #--{{{ return @random_charset if defined? @random_charset @random_charset = ('A' .. 'Z').to_a + ('1' .. '9').to_a end |
#rchar(*list) ⇒ Object
–}}}
523 524 525 526 527 |
# File 'lib/flatulent.rb', line 523 def rchar *list #--{{{ list = list.flatten n = list.size - 1 lambda{list[ rand(n) ]} end |
#vapour! ⇒ Object
–}}}
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/flatulent.rb', line 375 def vapour! #--{{{ #spaced = @string.split(%r//).join #' ' #.join(' ') fontfile = File.join fontdir, "#{ @font }.flf" font = Text::Figlet::Font.new fontfile typesetter = Text::Figlet::Typesetter.new font grid = Array.new(@figlet_height).map{ Array.new(@figlet_width){ " " } } width = grid.first.size height = grid.size @vapour_chars.times do figlet = typesetter[random_char] idxs = [] space = " "[0] newline = "\n"[0] i = 0 figlet.each_byte do |byte| idxs << i unless byte == space or byte == newline i += 1 end #p idxs to_vapourize = idxs.sort_by{ rand }.first((idxs.size * @vapour_level).ceil) #p to_vapourize to_vapourize.each{|idx| figlet[idx] = " "} #puts figlet figlet_grid = [] and figlet.each_line do |line| figlet_grid << line.chomp.split(%r"") end xoff = rand(width - 1) yoff = rand(height - 1) figlet_grid.each_with_index do |row, y| row.each_with_index do |char, x| j = y + yoff i = x + xoff next if j >= height or i >= width next if char == " " grid[ y + yoff ][ x + xoff ] = char end end end @vapour = grid.map{|row| row.join}.join("\n") end |