Class: Lurkmore

Inherits:
Object
  • Object
show all
Defined in:
lib/lurkmore.rb

Constant Summary collapse

'http://lurkmore.to/%D0%A1%D0%BB%D1%83%D0%B6%D0%B5%D0%B1%D0%BD%D0%B0%D1%8F:Random'
TERM_WIDTH =
Integer(`tput cols`)
RED =
"\e[31m"
GREEN =
"\e[32m"
RESET_COLOR =
"\e[0m"

Class Method Summary collapse

Class Method Details

.all_pObject



43
44
45
# File 'lib/lurkmore.rb', line 43

def self.all_p
  @session.all(:css, '.mw-content-ltr p')
end

.colorcode(string, code) ⇒ Object



56
57
58
# File 'lib/lurkmore.rb', line 56

def self.colorcode(string, code)
  code + string + RESET_COLOR
end

.heading(head_text) ⇒ Object



51
52
53
54
# File 'lib/lurkmore.rb', line 51

def self.heading(head_text)
  half = ( TERM_WIDTH - Integer(head_text.size) ) / 2
  "-" * half + head_text + "-" * half
end

.hrObject



47
48
49
# File 'lib/lurkmore.rb', line 47

def self.hr
  "-" * TERM_WIDTH
end

.randomObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lurkmore.rb', line 14

def self.random
  spinner = TTY::Spinner.new
  spinner.run do
    @session = Capybara::Session.new(:poltergeist)
    @session.visit(LINK_RANDOM)
  end
  begin
    if $stdout.tty?
      puts heading(colorcode(@session.first('.firstHeading').text.strip, GREEN))
    else
      puts heading(@session.first('.firstHeading').text.strip)
    end
    puts
    puts hr
    paragraphs = all_p
    paragraphs.each do |p|
      text = p.text.strip
      next if text.empty? || (text.split(' ').size < 10)
      puts wrap(text), hr
    end
  rescue Errno::EPIPE
    exit(74)
  end
end

.wrap(s, width = TERM_WIDTH) ⇒ Object



39
40
41
# File 'lib/lurkmore.rb', line 39

def self.wrap(s, width=TERM_WIDTH)
  s.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
end