linux_fortune 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -18,4 +18,11 @@
18
18
 
19
19
  * 2 minor enhancements:
20
20
  * some of "gregory's anonymous class hacks":http://blog.rubybestpractices.com/posts/gregory/anonymous_class_hacks.html rbp's implemented
21
- * updated testing
21
+ * updated testing
22
+
23
+ === 0.0.5 2010-03-30
24
+
25
+ * 1 major enhancement:
26
+ * fortunes can be searched
27
+ * minor enhancements:
28
+ * code comments & cleanups
data/README.rdoc CHANGED
@@ -30,6 +30,17 @@ To use it:
30
30
  f = LinuxFortune.generate("chucknorris")
31
31
  puts f.body
32
32
 
33
+ To search fortunes:
34
+
35
+ require 'rubygems'
36
+ require 'linux_fortune'
37
+ fs = LinuxFortune.search("develop")
38
+ puts "#{fs.size} hits:"
39
+ fs.each do |f|
40
+ puts f.source
41
+ puts f.body
42
+ end
43
+
33
44
  == REQUIREMENTS:
34
45
 
35
46
  * fortune must be installed, with some fortune db modules
data/Rakefile CHANGED
@@ -2,9 +2,9 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('linux_fortune', '0.0.4') do |p|
5
+ Echoe.new('linux_fortune', '0.0.5') do |p|
6
6
  p.description = "A gem that provides a wrapper for the linux fortune program"
7
- p.url = "http://github.com/srejbi/linux_fortune"
7
+ p.url = "http://srejbi.info/posts/5_linux_fortune"
8
8
  p.author = "George Schreiber"
9
9
  p.email = "gy.schreiber @nospam@ mobility.hu"
10
10
  p.ignore_pattern = ["tmp/*", "script/*"]
data/lib/linux_fortune.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module LinuxFortune
2
2
  # <tt>@@binary_path</tt> - path to the 'fortune' binary
3
- #mattr_accessor :binary_path
4
3
  @@binary_path = "/usr/bin/fortune"
5
4
 
6
5
  # sets the path of the linux fortune program ("/usr/bin/fortune" by default)
@@ -117,27 +116,52 @@ module LinuxFortune
117
116
 
118
117
  # The Fortune class is basicly 2 strings, source and body
119
118
  Fortune = Class.new do
119
+ # attribute accessors
120
+ attr_reader :body, :source
121
+
120
122
  # pass the string from the fortune program
121
123
  def initialize(fortunestring)
122
124
  # check lines of the string, extract source and separate from the rest of the body
123
- src = ""
124
- bod = ""
125
- fortunestring.each do |s|
126
- if s.match(/\(.*\)/) and src.empty?
127
- src = s.gsub(/(^\()|(\)$)/, "").strip
128
- else
129
- bod += s unless s.match(/^%\n/)
125
+ temp_source = ""
126
+ temp_body = ""
127
+ fortunestring.each do |fortune_line|
128
+ # in fortune strings, source is a path included in brackets
129
+ if fortune_line.match(/\(.*\)/) and temp_source.empty?
130
+ temp_source = fortune_line.gsub(/(^\()|(\)$)/, "").strip
131
+ else # if not a source
132
+ # it is a body line, unless it is a 'field separator' - a line with a single percent sign
133
+ temp_body += fortune_line unless fortune_line.match(/^%\n/)
130
134
  end
131
135
  end
132
- @source = src
133
- @body = bod
136
+ @source = temp_source
137
+ @body = temp_body
134
138
  end
135
- # attribute accessors
136
-
137
- attr_reader :body, :source
138
139
 
139
140
  # gets the fortune text (alias for body)
140
141
  alias_method(:to_s, :body)
142
+
143
+ # processes a fortune search output string and returns an array of fortunes
144
+ def self.fortunes(fortunesstring)
145
+ ret = []
146
+ # check lines of the string, extract source and separate from the rest of the body
147
+ temp_source = ""
148
+ temp_body = ""
149
+ # parse results string
150
+ fortunesstring.each do |fortunes_line|
151
+ if fortunes_line.strip.match(/^%$/) # field separator?
152
+ if temp_source != "" and temp_body != ""
153
+ fortunesource = "#{temp_source}\n%\n#{temp_body}"
154
+ ret << Fortune.new(fortunesource)
155
+ end
156
+ temp_body = ""
157
+ elsif fortunes_line.match(/^\(.*\)$/) # source field?
158
+ temp_source = fortunes_line.strip
159
+ else # if not a source or a separator
160
+ temp_body += fortunes_line
161
+ end
162
+ end
163
+ ret
164
+ end
141
165
  end
142
166
 
143
167
 
@@ -160,7 +184,7 @@ module LinuxFortune
160
184
  # executes the fortune program
161
185
  def self.fortune(sources = nil)
162
186
  #puts "executing #{self.binary_path} -c #{fortune_options} #{sources.each { |s| s.strip }.join(" ") unless sources.nil?} 2>&1"
163
- `#{self.binary_path} -c #{fortune_options} #{sources.each { |s| s.to_s }.join(" ") unless sources.nil?} 2>&1`
187
+ `#{self.binary_path} -c #{fortune_options} #{sources.each { |source| source.to_s }.join(" ") unless sources.nil?} 2>&1`
164
188
  end
165
189
 
166
190
  # generates a fortune message
@@ -170,8 +194,16 @@ module LinuxFortune
170
194
  end
171
195
 
172
196
  # searches fortune sources and returns hits
173
- def self.search(pattern = nil)
174
- # TODO
197
+ # <tt>pattern</tt> - search pattern (grep-like)
198
+ # <tt>sources</tt> - array of sources to be searched (all if not specified)
199
+ def self.search(pattern = nil, sources = nil)
200
+ # reset long / short filters
201
+ LinuxFortune.long = false
202
+ LinuxFortune.short = false
203
+ # run fortune
204
+ results = `#{self.binary_path} -c -m "#{pattern.gsub(/"/, '\\"')}" #{fortune_options} #{sources.each { |source| source.to_s }.join(" ") unless sources.nil?} 2>&1`
205
+ # process results
206
+ LinuxFortune::Fortune.fortunes(results)
175
207
  end
176
208
 
177
209
  protected
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{linux_fortune}
5
- s.version = "0.0.4"
5
+ s.version = "0.0.5"
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"]
9
9
  s.cert_chain = ["/home/george/.ssh/gem/gem-public_cert.pem"]
10
- s.date = %q{2010-03-25}
10
+ s.date = %q{2010-03-30}
11
11
  s.description = %q{A gem that provides a wrapper for the linux fortune program}
12
12
  s.email = %q{gy.schreiber @nospam@ mobility.hu}
13
13
  s.extra_rdoc_files = ["README.rdoc", "lib/linux_fortune.rb"]
14
14
  s.files = ["History.txt", "Manifest", "README.rdoc", "Rakefile", "lib/linux_fortune.rb", "test/test_helper.rb", "test/test_linux_fortune.rb", "linux_fortune.gemspec"]
15
- s.homepage = %q{http://github.com/srejbi/linux_fortune}
15
+ s.homepage = %q{http://srejbi.info/posts/5_linux_fortune}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Linux_fortune", "--main", "README.rdoc"]
17
17
  s.require_paths = ["lib"]
18
18
  s.rubyforge_project = %q{linux_fortune}
@@ -84,4 +84,15 @@ class TestLinuxFortune < Test::Unit::TestCase
84
84
  fortunes << LinuxFortune.generate( sources )
85
85
  assert !fortunes.empty?
86
86
  end
87
+
88
+ # test that search executes, array of fortunes is returned
89
+ def test_search
90
+ fortunes = LinuxFortune.search('develop', ['chucknorris'])
91
+ assert fortunes.is_a?(Array)
92
+ assert fortunes.size > 0
93
+ fortunes.each do |fortune|
94
+ assert fortune.is_a?(LinuxFortune::Fortune)
95
+ assert fortune.source.include?('chucknorris')
96
+ end
97
+ end
87
98
  end
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
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - George Schreiber
@@ -35,7 +35,7 @@ cert_chain:
35
35
  0ps4N+22wwDW/tu5hg8+hebo
36
36
  -----END CERTIFICATE-----
37
37
 
38
- date: 2010-03-25 00:00:00 +01:00
38
+ date: 2010-03-30 00:00:00 +02:00
39
39
  default_executable:
40
40
  dependencies: []
41
41
 
@@ -58,7 +58,7 @@ files:
58
58
  - test/test_linux_fortune.rb
59
59
  - linux_fortune.gemspec
60
60
  has_rdoc: true
61
- homepage: http://github.com/srejbi/linux_fortune
61
+ homepage: http://srejbi.info/posts/5_linux_fortune
62
62
  licenses: []
63
63
 
64
64
  post_install_message:
metadata.gz.sig CHANGED
Binary file