Module: LinuxFortune
- Defined in:
- lib/linux_fortune.rb
Constant Summary collapse
- FortuneSource =
fortune source class basically a couple of strings to construct the db file path and the weight (in percentage) of the file (compared to 100%)
Class.new do # create a new source reference with source, path and weight def initialize(source = nil, path = "/usr/share/fortune", weight = nil ) @source = source @path = path @weight = weight end attr_reader :source, :path, :weight # gets full path to the source def fullpath File.join(@path, @source) end # gets the full path to the message alias_method(:to_s, :fullpath) # gets a fortune message from this source def fortune LinuxFortune.generate([self.fullpath]) end end
- Fortune =
The Fortune class is basicly 2 strings, source and body
Class.new do # attribute accessors attr_reader :body, :source # pass the string from the fortune program def initialize(fortunestring) # check lines of the string, extract source and separate from the rest of the body temp_source = "" temp_body = "" fortunestring.each do |fortune_line| # in fortune strings, source is a path included in brackets if fortune_line.match(/\(.*\)/) and temp_source.empty? temp_source = fortune_line.gsub(/(^\()|(\)$)/, "").strip else # if not a source # it is a body line, unless it is a 'field separator' - a line with a single percent sign temp_body += fortune_line unless fortune_line.match(/^%\n/) end end @source = temp_source @body = temp_body end # gets the fortune text (alias for body) alias_method(:to_s, :body) # processes a fortune search output string and returns an array of fortunes def self.fortunes(fortunesstring) ret = [] # check lines of the string, extract source and separate from the rest of the body temp_source = "" temp_body = "" # parse results string fortunesstring.each do |fortunes_line| if fortunes_line.strip.match(/^%$/) # field separator? if temp_source != "" and temp_body != "" fortunesource = "#{temp_source}\n%\n#{temp_body}" ret << Fortune.new(fortunesource) end temp_body = "" elsif fortunes_line.match(/^\(.*\)$/) # source field? temp_source = fortunes_line.strip else # if not a source or a separator temp_body += fortunes_line end end ret end end
- @@binary_path =
@@binary_path
- path to the ‘fortune’ binary "/usr/bin/fortune"
- @@offensive =
false
- @@equalsize =
false
- @@short_length =
160
- @@short =
false
- @@long =
false
- @@ignore_case =
true
Class Method Summary collapse
-
.binary_path ⇒ Object
gets the current path to the linux fortune program.
-
.binary_path=(path) ⇒ Object
sets the path of the linux fortune program (“/usr/bin/fortune” by default).
-
.equalsize ⇒ Object
returns the current state of
equalsize
. -
.equalsize=(eq = true) ⇒ Object
eq
- Consider all fortune files to be of equal size (see discussion below on multiple files). -
.fortune(sources = nil) ⇒ Object
executes the fortune program.
-
.generate(sources = nil) ⇒ Object
generates a fortune message
sources
- array of sources. -
.get_sources ⇒ Object
list available sources.
-
.ignore_case ⇒ Object
gets the state of case-insensitivity.
-
.ignore_case=(ignore = true) ⇒ Object
turns on/off case-insensitivity for search.
-
.long ⇒ Object
gets the state of long message generation.
-
.long=(longfortunes = true) ⇒ Object
turns on/off long message generation.
- .offensive ⇒ Object
-
.offensive=(off = true) ⇒ Object
off
- Choose only from potentially offensive aphorisms. -
.search(pattern = nil, sources = nil) ⇒ Object
searches fortune sources and returns hits
pattern
- search pattern (grep-like)sources
- array of sources to be searched (all if not specified). -
.short ⇒ Object
get the state of short message generation.
-
.short=(shortfortunes = true) ⇒ Object
turns on/off short message generation.
-
.short_length ⇒ Object
gets the current maximum length considered short.
-
.short_length=(shortlength = 160) ⇒ Object
Set the longest fortune length (in characters) considered to be “short” (the default is 160).
Class Method Details
.binary_path ⇒ Object
gets the current path to the linux fortune program
11 12 13 |
# File 'lib/linux_fortune.rb', line 11 def self.binary_path @@binary_path end |
.binary_path=(path) ⇒ Object
sets the path of the linux fortune program (“/usr/bin/fortune” by default)
6 7 8 |
# File 'lib/linux_fortune.rb', line 6 def self.binary_path=(path) @@binary_path = path unless @@binary_path == path end |
.equalsize ⇒ Object
returns the current state of equalsize
34 35 36 |
# File 'lib/linux_fortune.rb', line 34 def self.equalsize @@equalsize end |
.equalsize=(eq = true) ⇒ Object
eq
- Consider all fortune files to be of equal size (see discussion below on multiple files).
29 30 31 |
# File 'lib/linux_fortune.rb', line 29 def self.equalsize=(eq = true) @@equalsize = eq end |
.fortune(sources = nil) ⇒ Object
executes the fortune program
185 186 187 188 |
# File 'lib/linux_fortune.rb', line 185 def self.fortune(sources = nil) #puts "executing #{self.binary_path} -c #{fortune_options} #{sources.each { |s| s.strip }.join(" ") unless sources.nil?} 2>&1" `#{self.binary_path} -c #{} #{sources.each { |source| source.to_s }.join(" ") unless sources.nil?} 2>&1` end |
.generate(sources = nil) ⇒ Object
generates a fortune message sources
- array of sources
192 193 194 |
# File 'lib/linux_fortune.rb', line 192 def self.generate(sources = nil) LinuxFortune::Fortune.new( self.fortune(sources) ) end |
.get_sources ⇒ Object
list available sources
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/linux_fortune.rb', line 169 def self.get_sources sources = [] path = nil srclist = `#{self.binary_path} -f 2>&1` srclist.each do |source| weight,src = source.strip.split if src.match(/\/.*/) path = src else sources << LinuxFortune::FortuneSource.new( src, path, weight ) end end sources end |
.ignore_case ⇒ Object
gets the state of case-insensitivity
87 88 89 |
# File 'lib/linux_fortune.rb', line 87 def self.ignore_case @@ignore_case end |
.ignore_case=(ignore = true) ⇒ Object
turns on/off case-insensitivity for search
82 83 84 |
# File 'lib/linux_fortune.rb', line 82 def self.ignore_case=(ignore = true) @@ignore_case = ignore end |
.long ⇒ Object
gets the state of long message generation
75 76 77 |
# File 'lib/linux_fortune.rb', line 75 def self.long @@long end |
.long=(longfortunes = true) ⇒ Object
turns on/off long message generation
69 70 71 72 |
# File 'lib/linux_fortune.rb', line 69 def self.long=(longfortunes = true) @@short = false if longfortunes @@long = longfortunes end |
.offensive ⇒ Object
22 23 24 |
# File 'lib/linux_fortune.rb', line 22 def self.offensive @@offensive end |
.offensive=(off = true) ⇒ Object
off
- Choose only from potentially offensive aphorisms. This option is ignored if a fortune directory is specified.
18 19 20 |
# File 'lib/linux_fortune.rb', line 18 def self.offensive=(off = true) @@offensive = off end |
.search(pattern = nil, sources = nil) ⇒ Object
searches fortune sources and returns hits pattern
- search pattern (grep-like) sources
- array of sources to be searched (all if not specified)
199 200 201 202 203 204 205 206 207 |
# File 'lib/linux_fortune.rb', line 199 def self.search(pattern = nil, sources = nil) # reset long / short filters LinuxFortune.long = false LinuxFortune.short = false # run fortune results = `#{self.binary_path} -c -m "#{pattern.gsub(/"/, '\\"')}" #{} #{sources.each { |source| source.to_s }.join(" ") unless sources.nil?} 2>&1` # process results LinuxFortune::Fortune.fortunes(results) end |
.short ⇒ Object
get the state of short message generation
62 63 64 |
# File 'lib/linux_fortune.rb', line 62 def self.short @@short end |
.short=(shortfortunes = true) ⇒ Object
turns on/off short message generation
56 57 58 59 |
# File 'lib/linux_fortune.rb', line 56 def self.short=(shortfortunes = true) @@long = false if shortfortunes @@short = shortfortunes end |
.short_length ⇒ Object
gets the current maximum length considered short
49 50 51 |
# File 'lib/linux_fortune.rb', line 49 def self.short_length @@short_length end |
.short_length=(shortlength = 160) ⇒ Object
Set the longest fortune length (in characters) considered to be “short” (the default is 160). All fortunes longer than this are considered “long”. Be careful! If you set the length too short and ask for short fortunes, or too long and ask for long ones, fortune goes into a never-ending thrash loop.
44 45 46 |
# File 'lib/linux_fortune.rb', line 44 def self.short_length=(shortlength = 160) @@short_length = shortlength end |