linux_fortune 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -3,8 +3,13 @@
3
3
  * 1 major enhancement:
4
4
  * Initial release
5
5
 
6
- === 0.0.1 2010-03-24
6
+ === 0.0.2 2010-03-24
7
7
 
8
8
  * 2 fixes:
9
9
  * debug puts messages commented out
10
- * test failures / errors fixed
10
+ * test failures / errors fixed
11
+
12
+ === 0.0.3 2010-03-24
13
+
14
+ * 1 major enhancement:
15
+ * fortune sources listing / selection
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('linux_fortune', '0.0.2') do |p|
5
+ Echoe.new('linux_fortune', '0.0.3') do |p|
6
6
  p.description = "A gem that provides a wrapper for the linux fortune program"
7
7
  p.url = "http://github.com/srejbi/linux_fortune"
8
8
  p.author = "George Schreiber"
data/lib/linux_fortune.rb CHANGED
@@ -89,13 +89,16 @@ module LinuxFortune
89
89
  @@ignore_case
90
90
  end
91
91
 
92
- # fortune source
92
+ # fortune source class
93
+ # basically a couple of strings to construct the db file path
94
+ # and the weight (in percentage) of the file (compared to 100%)
93
95
  class FortuneSource
94
96
  @path = "/usr/share/fortune"
95
97
  @source = ""
96
- @weight = 0
98
+ @weight = ""
97
99
 
98
- def init(source = nil, path = "/usr/share/fortune", weight = nil )
100
+ # create a new source reference with source, path and weight
101
+ def initialize(source = nil, path = "/usr/share/fortune", weight = nil )
99
102
  @source = source
100
103
  @path = path
101
104
  @weight = weight
@@ -129,6 +132,11 @@ module LinuxFortune
129
132
  def fullpath
130
133
  File.join(@path, @source)
131
134
  end
135
+
136
+ # gets a fortune message from this source
137
+ def fortune
138
+ LinuxFortune.generate([self.fullpath])
139
+ end
132
140
  end
133
141
 
134
142
 
@@ -137,6 +145,21 @@ module LinuxFortune
137
145
  @source = ""
138
146
  @body = ""
139
147
 
148
+ # pass the string from the fortune program
149
+ def initialize(fortunestring)
150
+ # check lines of the string, extract source and separate from the rest of the body
151
+ src = ""
152
+ bod = ""
153
+ fortunestring.each do |s|
154
+ if s.match(/\(.*\)/) and src.empty?
155
+ src = s.gsub(/(^\()|(\)$)/, "").strip
156
+ else
157
+ bod += s unless s.match(/^%\n/)
158
+ end
159
+ end
160
+ @source = src
161
+ @body = bod
162
+ end
140
163
  # attribute accessors
141
164
 
142
165
  # gets the fortune text
@@ -168,11 +191,10 @@ module LinuxFortune
168
191
  srclist = `#{self.binary_path} -f 2>&1`
169
192
  srclist.each do |source|
170
193
  weight,src = source.strip.split
171
- if src.match(/(\/.*)*/)
172
- #puts "#{src} -> #{weight}"
194
+ if src.match(/\/.*/)
173
195
  path = src
174
196
  else
175
- sources << FortuneSource.new( :path => path, :source => src, :weight => weight )
197
+ sources << LinuxFortune::FortuneSource.new( src, path, weight )
176
198
  end
177
199
  end
178
200
  sources
@@ -187,17 +209,7 @@ module LinuxFortune
187
209
  # generates a fortune message
188
210
  # <tt>sources</tt> - array of sources
189
211
  def self.generate(sources = nil)
190
- lf = LinuxFortune::Fortune.new
191
- lf.body = ""
192
- ftn = self.fortune(sources)
193
- ftn.each do |s|
194
- if s.match(/\(.*\)/)
195
- lf.source = s.gsub(/(^\()|(\)$)/, "")
196
- else
197
- lf.body << s unless s.match(/^%\n/)
198
- end
199
- end
200
- lf
212
+ LinuxFortune::Fortune.new( self.fortune(sources) )
201
213
  end
202
214
 
203
215
  # searches fortune sources and returns hits
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{linux_fortune}
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["George Schreiber"]
@@ -48,7 +48,7 @@ class TestLinuxFortune < Test::Unit::TestCase
48
48
  end
49
49
 
50
50
  # Fortune.body should be the same as Fortune.to_s
51
- def test_to_s
51
+ def test_fortune_to_s
52
52
  lf = LinuxFortune.generate
53
53
  assert lf.body == lf.to_s
54
54
  end
@@ -56,17 +56,29 @@ class TestLinuxFortune < Test::Unit::TestCase
56
56
  # test that there are sources
57
57
  # TODO refine test case
58
58
  def test_sources
59
- src = LinuxFortune.get_sources
60
- #src.each { |s| s.strip!; weight,name = s.split(" "); puts "#{name} -> #{weight}" }
61
- assert !src.nil?
59
+ # just get whatever length fortunes
60
+ LinuxFortune.short=false
61
+ LinuxFortune.long=false
62
+
63
+ sources = LinuxFortune.get_sources
64
+ assert !sources.nil? and sources.size > 0
65
+ end
66
+
67
+ # test fortune from source
68
+ def test_fortune_from_each_source
69
+ sources = LinuxFortune.get_sources
70
+ fortunes = []
71
+ sources.each do |src|
72
+ fortunes << src.fortune
73
+ assert fortunes.size > 0
74
+ #puts "#{fortunes.last.source} --- #{src.fullpath}"
75
+ assert fortunes.last.source == src.fullpath
76
+ end
62
77
  end
63
78
 
64
79
  # test if fortune messages are from the sources requested
65
- # TODO pick random sources from sources
80
+ # TODO pick random sources from sources once sources code is functional
66
81
  def test_fortune_from_sources
67
- # just get whatever length fortunes
68
- LinuxFortune.short=false
69
- LinuxFortune.long=false
70
82
  # check multiple times
71
83
  5.times do
72
84
  lf = LinuxFortune.generate(["chucknorris","linux"])
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - George Schreiber
metadata.gz.sig CHANGED
Binary file