Module: LinuxFortune

Defined in:
lib/linux_fortune.rb

Defined Under Namespace

Classes: Fortune, FortuneSource

Constant Summary collapse

@@binary_path =

@@binary_path - path to the ‘fortune’ binary mattr_accessor :binary_path

"/usr/bin/fortune"
@@offensive =
false
@@equalsize =
false
@@short_length =
160
@@short =
false
@@long =
false
@@ignore_case =
true

Class Method Summary collapse

Class Method Details

.binary_pathObject

gets the current path to the linux fortune program



12
13
14
# File 'lib/linux_fortune.rb', line 12

def self.binary_path
  @@binary_path
end

.binary_path=(path) ⇒ Object

sets the path of the linux fortune program (“/usr/bin/fortune” by default)



7
8
9
# File 'lib/linux_fortune.rb', line 7

def self.binary_path=(path)
  @@binary_path = path unless @@binary_path == path
end

.equalsizeObject

returns the current state of equalsize



35
36
37
# File 'lib/linux_fortune.rb', line 35

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).



30
31
32
# File 'lib/linux_fortune.rb', line 30

def self.equalsize=(eq = true)
  @@equalsize = eq
end

.fortune(sources = nil) ⇒ Object

executes the fortune program



182
183
184
185
# File 'lib/linux_fortune.rb', line 182

def self.fortune(sources = nil)
  puts "executing #{self.binary_path} -c #{fortune_options} #{sources.each { |s| s; } unless sources.nil?} 2>&1"
  `#{self.binary_path} -c #{fortune_options} #{sources.each { |s| s; } unless sources.nil?} 2>&1`
end

.generate(sources = nil) ⇒ Object

generates a fortune message sources - array of sources



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/linux_fortune.rb', line 189

def self.generate(sources = nil)
  lf = LinuxFortune::Fortune.new
  lf.body = ""
  ftn = self.fortune(sources)
  ftn.each do |s|
    if s.match(/\(.*\)/)
      lf.source = s.gsub(/(^\()|(\)$)/, "")
    else
      lf.body << s unless s.match(/^%\n/)
    end
  end
  lf
end

.get_sourcesObject

list available sources



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/linux_fortune.rb', line 165

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(/(\/.*)*/)
      puts "#{src} -> #{weight}"
      path = src
    else
      sources << FortuneSource.new( :path => path, :source => src, :weight => weight )
    end
  end
  sources
end

.ignore_caseObject

gets the state of case-insensitivity



88
89
90
# File 'lib/linux_fortune.rb', line 88

def self.ignore_case
  @@ignore_case
end

.ignore_case=(ignore = true) ⇒ Object

turns on/off case-insensitivity for search



83
84
85
# File 'lib/linux_fortune.rb', line 83

def self.ignore_case=(ignore = true)
  @@ignore_case = ignore
end

.longObject

gets the state of long message generation



76
77
78
# File 'lib/linux_fortune.rb', line 76

def self.long
  @@long
end

.long=(longfortunes = true) ⇒ Object

turns on/off long message generation



70
71
72
73
# File 'lib/linux_fortune.rb', line 70

def self.long=(longfortunes = true)
  @@short = false if longfortunes
  @@long = longfortunes
end

.offensiveObject



23
24
25
# File 'lib/linux_fortune.rb', line 23

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.



19
20
21
# File 'lib/linux_fortune.rb', line 19

def self.offensive=(off = true)
  @@offensive = off
end

.search(pattern = nil) ⇒ Object

searches fortune sources and returns hits



204
205
206
# File 'lib/linux_fortune.rb', line 204

def self.search(pattern = nil)
  # TODO
end

.shortObject

get the state of short message generation



63
64
65
# File 'lib/linux_fortune.rb', line 63

def self.short
  @@short
end

.short=(shortfortunes = true) ⇒ Object

turns on/off short message generation



57
58
59
60
# File 'lib/linux_fortune.rb', line 57

def self.short=(shortfortunes = true)
  @@long = false if shortfortunes
  @@short = shortfortunes
end

.short_lengthObject

gets the current maximum length considered short



50
51
52
# File 'lib/linux_fortune.rb', line 50

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.



45
46
47
# File 'lib/linux_fortune.rb', line 45

def self.short_length=(shortlength = 160)
  @@short_length = shortlength
end