Class: Flatulent
Defined Under Namespace
Classes: EncryptionError, Error, TimeBombError
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
–}}}.
-
#hammer(chars) ⇒ Object
–}}}.
- #image(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 285 |
# 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.05 ] @transform = Float opt[ 'transform', 0.03 ] @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
3 |
# File 'lib/flatulent.rb', line 3 def self.libdir() File.(__FILE__).gsub(%r/\.rb$/, '') end |
.version ⇒ Object
2 |
# File 'lib/flatulent.rb', line 2 def self.version() '0.0.4' end |
Instance Method Details
#captcha! ⇒ Object
–}}}
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/flatulent.rb', line 424 def captcha! #--{{{ figlet = @figlet.dup chars = figlet.split %r"" hammer chars figlet = chars.join space = " "[0] newline = "\n"[0] @captcha = " " * figlet.size # merge vapour with figlet figlet.size.times do |i| fbyte = figlet[i] vbyte = @vapour[i] if fbyte == newline @captcha[i] = newline elsif fbyte == space @captcha[i] = vbyte else @captcha[i] = fbyte end end @captcha end |
#css_for(hash) ⇒ Object
–}}}
542 543 544 |
# File 'lib/flatulent.rb', line 542 def css_for hash #--{{{ hash.map{|kv| kv.join ':' }.join ';' end |
#element! ⇒ Object
–}}}
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 |
# File 'lib/flatulent.rb', line 483 def element! #--{{{ rows = [] rows << (row = []) chars = @captcha.split %r// #chars = @figlet.split %r// size = chars.size last = size - 1 chars.each_with_index do |char, idx| content = case char when %r/\n/o #"<br>" "\n" 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 content = rows.join #<table width='50%' border=1 bgcolor='#{ style["background"] }'><tr><td> #</td></tr></table> @element = <<-html <div id='#{ @id }_element' style='#{ css }'>#{ content }</div> html end |
#encrypt(string) ⇒ Object
–{{{
562 563 564 |
# File 'lib/flatulent.rb', line 562 def encrypt string #--{{{ self.class.encrypt string.to_s end |
#encrypted ⇒ Object
–}}}
566 567 568 |
# File 'lib/flatulent.rb', line 566 def encrypted #--{{{ self.class.encrypt @string end |
#figlet! ⇒ Object
–}}}
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 374 |
# File 'lib/flatulent.rb', line 307 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
–}}}
574 575 576 577 578 579 580 581 582 |
# File 'lib/flatulent.rb', line 574 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
–}}}
546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/flatulent.rb', line 546 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
–}}}
292 293 294 295 296 297 298 299 300 |
# File 'lib/flatulent.rb', line 292 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
–}}}
592 593 594 |
# File 'lib/flatulent.rb', line 592 def getopts #--{{{ self.class.getopts end |
#hammer(chars) ⇒ Object
–}}}
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/flatulent.rb', line 449 def hammer chars #--{{{ # transform character chars newline = "\n"[0] space = " "[0] alphas = ('A'..'Z').to_a + ('a'..'z').to_a transform = { '|' => rchar(%w'. * + , ; : ' + alphas), '/' => rchar(%w'] [ { } ( ) @' + alphas), '\\' => rchar(%w'/ ] [ { } ( ) @' + alphas), '-' => rchar(%w'_ * ^ # @ ~'), '_' => rchar(%w'- * ^ # @ ~'), '(' => rchar(%w'/ ] [ { } ) @' + alphas), ')' => rchar(%w'/ ] [ { } ( @' + alphas), } to_transform = (0 ... chars.size).to_a.select{|i| chars[i] !~ %r"\s"}.sort_by{ rand } to_transform = to_transform.first((to_transform.size * @transform).ceil) to_transform.each do |i| char = chars[i] chars[i] = (transform[char] || lambda{char}).call end # background noise to_noise = (0 ... chars.size).to_a.select{|i| chars[i] =~ %r" "}.sort_by{ rand } to_noise = to_noise.first((to_noise.size * @noise).ceil) noisy = %w`| / ( ) \\ ! [ ] < > ^ v V` noise = lambda{|*a| noisy[ rand(noisy.size) ]} to_noise.each do |i| char = chars[i] chars[i] = noise[char] end end |
#image(options = {}) ⇒ Object
518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/flatulent.rb', line 518 def image = {} require 'RMagick' grid = captcha.split(%r"\n").map{|row| row.split %r""} width = grid.first.size height = grid.size img = Magick::Image.new width, height img end |
#munge(string) ⇒ Object
–}}}
570 571 572 |
# File 'lib/flatulent.rb', line 570 def munge string #--{{{ self.class.munge string end |
#random_char ⇒ Object
–}}}
302 303 304 305 |
# File 'lib/flatulent.rb', line 302 def random_char #--{{{ n = random_charset.size - 1 random_charset[rand(n)] end |
#random_charset ⇒ Object
–}}}
287 288 289 290 |
# File 'lib/flatulent.rb', line 287 def random_charset #--{{{ return @random_charset if defined? @random_charset @random_charset = ('A' .. 'Z').to_a + ('1' .. '9').to_a end |
#rchar(*list) ⇒ Object
–{{{
532 533 534 535 536 |
# File 'lib/flatulent.rb', line 532 def rchar *list #--{{{ list = list.flatten.sort_by{ rand } n = list.size - 1 lambda{list[ rand(n) ]} end |
#vapour! ⇒ Object
–}}}
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 422 |
# File 'lib/flatulent.rb', line 376 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 |