class Lurkmore

Main class for lurkmore.to

Constants

GREEN

Green code

Link to random page

RED

Red code

RESET_COLOR

Reset code

TERM_WIDTH

Current terminal width

Public Class Methods

all_p() click to toggle source

Finds all p tags on current session

@return [Array] array of p tags

# File lib/lurkmore.rb, line 70
def self.all_p
  @session.all(:css, '.mw-content-ltr p')
end
colorcode(string, code) click to toggle source

Colorcodes strings

@param string [String] string to colorcode @param code [String] color code @return [String] colorcoded string

# File lib/lurkmore.rb, line 95
def self.colorcode(string, code)
  code + string + RESET_COLOR
end
heading(head) click to toggle source

Creates a heading

@param head [String] text to make heading from @return [String] heading

# File lib/lurkmore.rb, line 85
def self.heading(head)
  half = ( TERM_WIDTH - Integer(head.size) ) / 2
  "-" * half + head + "-" * half
end
hr() click to toggle source

Makes a horizontal rule

@return [String] horizontal rule matching #TERM_WIDTH

# File lib/lurkmore.rb, line 77
def self.hr
  "-" * TERM_WIDTH
end
random() click to toggle source

Gets a random article from lurkmore.to @example

Lurkmore.random #=> ...

@note Sometimes throws a PhantomJS JavaScript error.

# File lib/lurkmore.rb, line 31
def self.random
  spinner = TTY::Spinner.new
  spinner.run do
    begin
      @session = Capybara::Session.new(:poltergeist)
      @session.visit(LINK_RANDOM)
    rescue Capybara::Poltergeist::JavascriptError
      $stderr.puts "A JS error occured. Skipping..."
    end
  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
    all_p.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) click to toggle source

@param s [String] string to wrap @return [String] wrapped string

# File lib/lurkmore.rb, line 63
def self.wrap(s)
  s.gsub(/(.{1,#{TERM_WIDTH}})(\s+|\Z)/, "\\1\n")
end