Module: FirstDirect::Helpers

Extended by:
Helpers
Included in:
FirstDirect, Account, Helpers
Defined in:
lib/firstdirect.rb

Overview

Some useful methods to have lying around.

Constant Summary collapse

@@colours =
{ :red => 31, :green => 32 }

Instance Method Summary collapse

Instance Method Details

#colour(colour, format, *args) ⇒ Object



93
94
95
# File 'lib/firstdirect.rb', line 93

def colour(colour, format, *args)
  "%s*%s %s" % [ "\033[#{@@colours[colour]}m", "\033[0m", (format % args) ]
end

#strip_js(link) ⇒ Object

Turn a javascript @href into a normal URL.



83
84
85
86
87
88
89
# File 'lib/firstdirect.rb', line 83

def strip_js(link)
  if link['href'].match(/reDoGet\('(.+)'\)$/)
    return $1
  else
    raise Error, "link doesn't look like a js link: #{link}"
  end
end

#trace(*args) ⇒ Object

Print a message with a red star, then print it again with a green star, once we’ve yielded.

result = trace('doing something'){ ... }


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/firstdirect.rb', line 103

def trace(*args)
  $stderr.sync = true
  $stderr.print colour(:red, *args)
  begin
    t = Thread.new do
      while true
        $stderr.print '.'
        sleep 0.2
      end
    end
    result = yield
    $stderr.print "\r" + colour(:green, *args)
  ensure
    t.kill
    $stderr.print "\n"
  end
  return result
end