linux_fortune 0.0.1

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 ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-03-23
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ lib/linux_fortune.rb
6
+ test/test_helper.rb
7
+ test/test_linux_fortune.rb
data/README.rdoc ADDED
@@ -0,0 +1,67 @@
1
+ = LinuxFortune
2
+
3
+ * http://github.com/srejbi/linux_fortune_gem
4
+
5
+ == DESCRIPTION:
6
+
7
+ Provides a wrapper to the 'fortune' program on linux (and unix /parameters
8
+ might differ/) platforms. As most (good) web servers are running on these
9
+ platforms, a 'local' fortune gem seemed like a good idea to play around and
10
+ discover ruby gem development through a practical example.
11
+
12
+ If you need a network-based fortune service, though, you might want to check out
13
+ lacco's ruby_fortunes at http://github.com/lacco/ruby-fortunes
14
+
15
+ == FEATURES:
16
+
17
+ * allows generating messages from all or specific fortune database modules
18
+ * possibility to generate short (up to LinuxFortune.short_length characters) messages
19
+
20
+ == ISSUES:
21
+
22
+ * LinuxFortune.long = true seem to have no effect due to issue with fortune (?)
23
+
24
+ == SYNOPSIS:
25
+
26
+ To use it:
27
+
28
+ require 'rubygems'
29
+ require 'linux_fortune'
30
+ f = LinuxFortune.generate("chucknorris")
31
+ puts f.body
32
+
33
+ == REQUIREMENTS:
34
+
35
+ * fortune must be installed, with some fortune db modules
36
+
37
+ == INSTALL:
38
+
39
+ * gentoo:
40
+ sudo emerge fortune
41
+ * ubuntu, debian:
42
+ apt-get install fortune
43
+
44
+ == LICENSE:
45
+
46
+ (The MIT License)
47
+
48
+ Copyright (c) 2010 György Schreiber
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining
51
+ a copy of this software and associated documentation files (the
52
+ 'Software'), to deal in the Software without restriction, including
53
+ without limitation the rights to use, copy, modify, merge, publish,
54
+ distribute, sublicense, and/or sell copies of the Software, and to
55
+ permit persons to whom the Software is furnished to do so, subject to
56
+ the following conditions:
57
+
58
+ The above copyright notice and this permission notice shall be
59
+ included in all copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
62
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
64
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
65
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
66
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
67
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('linux_fortune', '0.0.1') do |p|
6
+ p.description = "A gem that provides a wrapper for the linux fortune program"
7
+ p.url = "http://github.com/srejbi/linux_fortune"
8
+ p.author = "George Schreiber"
9
+ p.email = "gy.schreiber @nospam@ mobility.hu"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
@@ -0,0 +1,221 @@
1
+ module LinuxFortune
2
+ # <tt>@@binary_path</tt> - path to the 'fortune' binary
3
+ #mattr_accessor :binary_path
4
+ @@binary_path = "/usr/bin/fortune"
5
+
6
+ # sets the path of the linux fortune program ("/usr/bin/fortune" by default)
7
+ def self.binary_path=(path)
8
+ @@binary_path = path unless @@binary_path == path
9
+ end
10
+
11
+ # gets the current path to the linux fortune program
12
+ def self.binary_path
13
+ @@binary_path
14
+ end
15
+
16
+ @@offensive = false
17
+
18
+ # <tt>off</tt> - Choose only from potentially offensive aphorisms. This option is ignored if a fortune directory is specified.
19
+ def self.offensive=(off = true)
20
+ @@offensive = off
21
+ end
22
+
23
+ def self.offensive
24
+ @@offensive
25
+ end
26
+
27
+ @@equalsize = false
28
+
29
+ # <tt>eq</tt> - Consider all fortune files to be of equal size (see discussion below on multiple files).
30
+ def self.equalsize=(eq = true)
31
+ @@equalsize = eq
32
+ end
33
+
34
+ # returns the current state of <tt>equalsize</tt>
35
+ def self.equalsize
36
+ @@equalsize
37
+ end
38
+
39
+ @@short_length = 160
40
+
41
+ # Set the longest fortune length (in characters) considered to be ``short'' (the default is 160). All
42
+ # fortunes longer than this are considered ``long''. Be careful! If you set the length too short and
43
+ # ask for short fortunes, or too long and ask for long ones, fortune goes into a never-ending thrash
44
+ # loop.
45
+ def self.short_length=(shortlength = 160)
46
+ @@short_length = shortlength
47
+ end
48
+
49
+ # gets the current maximum length considered short
50
+ def self.short_length
51
+ @@short_length
52
+ end
53
+
54
+ @@short = false
55
+
56
+ # turns on/off short message generation
57
+ def self.short=(shortfortunes = true)
58
+ @@long = false if shortfortunes
59
+ @@short = shortfortunes
60
+ end
61
+
62
+ # get the state of short message generation
63
+ def self.short
64
+ @@short
65
+ end
66
+
67
+ @@long = false
68
+
69
+ # turns on/off long message generation
70
+ def self.long=(longfortunes = true)
71
+ @@short = false if longfortunes
72
+ @@long = longfortunes
73
+ end
74
+
75
+ # gets the state of long message generation
76
+ def self.long
77
+ @@long
78
+ end
79
+
80
+ @@ignore_case = true
81
+
82
+ # turns on/off case-insensitivity for search
83
+ def self.ignore_case=(ignore = true)
84
+ @@ignore_case = ignore
85
+ end
86
+
87
+ # gets the state of case-insensitivity
88
+ def self.ignore_case
89
+ @@ignore_case
90
+ end
91
+
92
+ # fortune source
93
+ class FortuneSource
94
+ @path = "/usr/share/fortune"
95
+ @source = ""
96
+ @weight = 0
97
+
98
+ def init(source = nil, path = "/usr/share/fortune", weight = nil )
99
+ @source = source
100
+ @path = path
101
+ @weight = weight
102
+ end
103
+
104
+ # sets the fortune source path
105
+ def path=(srcpath)
106
+ @path = srcpath
107
+ end
108
+
109
+ # gets the source path (directory with source)
110
+ def path
111
+ @path
112
+ end
113
+
114
+ # sets the source file name (file in FortuneSource.path)
115
+ def source=(src)
116
+ @source = src
117
+ end
118
+
119
+ # gets source file name
120
+ def source
121
+ @source
122
+ end
123
+
124
+ def weight
125
+ @weight
126
+ end
127
+
128
+ # gets full path to the source
129
+ def fullpath
130
+ File.join(@path, @source)
131
+ end
132
+ end
133
+
134
+
135
+ # The Fortune class is basicly 2 strings, source and body
136
+ class Fortune
137
+ @source = ""
138
+ @body = ""
139
+
140
+ # attribute accessors
141
+
142
+ # gets the fortune text
143
+ def body
144
+ @body
145
+ end
146
+ # gets the fortune text (alias for body)
147
+ alias_method(:to_s, :body)
148
+
149
+ # gets the fortune source
150
+ def source
151
+ @source
152
+ end
153
+
154
+ def body=(text = "")
155
+ @body = text
156
+ end
157
+
158
+ def source=(src = "")
159
+ @source = src
160
+ end
161
+
162
+ end
163
+
164
+ # list available sources
165
+ def self.get_sources
166
+ sources = []
167
+ path = nil
168
+ srclist = `#{self.binary_path} -f 2>&1`
169
+ srclist.each do |source|
170
+ weight,src = source.strip.split
171
+ if src.match(/(\/.*)*/)
172
+ puts "#{src} -> #{weight}"
173
+ path = src
174
+ else
175
+ sources << FortuneSource.new( :path => path, :source => src, :weight => weight )
176
+ end
177
+ end
178
+ sources
179
+ end
180
+
181
+ # executes the fortune program
182
+ def self.fortune(sources = nil)
183
+ puts "executing #{self.binary_path} -c #{fortune_options} #{sources.each { |s| s; } unless sources.nil?} 2>&1"
184
+ `#{self.binary_path} -c #{fortune_options} #{sources.each { |s| s; } unless sources.nil?} 2>&1`
185
+ end
186
+
187
+ # generates a fortune message
188
+ # <tt>sources</tt> - array of sources
189
+ 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
201
+ end
202
+
203
+ # searches fortune sources and returns hits
204
+ def self.search(pattern = nil)
205
+ # TODO
206
+ end
207
+
208
+ protected
209
+
210
+ # construct command line options for fortune
211
+ def self.fortune_options
212
+ opts = ""
213
+ opts << "-o " if @@offensive
214
+ opts << "-e " if @@equalsize
215
+ opts << "-l " if @@long
216
+ opts << "-s " if @@short
217
+ opts << "-n #{@@short_length}" unless @@short_length == 160 or (!@@long and !@@short)
218
+ opts
219
+ end
220
+ end
221
+
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{linux_fortune}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["George Schreiber"]
9
+ s.cert_chain = ["/home/george/.ssh/gem/gem-public_cert.pem"]
10
+ s.date = %q{2010-03-23}
11
+ s.description = %q{A gem that provides a wrapper for the linux fortune program}
12
+ s.email = %q{gy.schreiber @nospam@ mobility.hu}
13
+ s.extra_rdoc_files = ["README.rdoc", "lib/linux_fortune.rb"]
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}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Linux_fortune", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{linux_fortune}
19
+ s.rubygems_version = %q{1.3.5}
20
+ s.signing_key = %q{/home/george/.ssh/gem/gem-private_key.pem}
21
+ s.summary = %q{A gem that provides a wrapper for the linux fortune program}
22
+ s.test_files = ["test/test_helper.rb", "test/test_linux_fortune.rb"]
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
+ else
30
+ end
31
+ else
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/linux_fortune'
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestLinuxFortune < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_binary_path
9
+ LinuxFortune.binary_path=`which fortune`.strip
10
+ assert !LinuxFortune.binary_path.nil?
11
+ assert File.exist?(LinuxFortune.binary_path)
12
+ end
13
+
14
+ # test if fortune is generated
15
+ def test_basic_generation
16
+ lf = LinuxFortune.generate
17
+ assert !lf.nil? # fortune should be present
18
+ assert !lf.body.nil? && !lf.body.empty? # body should be present
19
+ assert !lf.source.nil? && !lf.source.empty? # source should be present
20
+ end
21
+
22
+ # verify that long gets set to false if configuring for short messages
23
+ def test_short
24
+ LinuxFortune.short = true
25
+ assert LinuxFortune.short # short should be set to true
26
+ assert LinuxFortune.long == false # long to false
27
+ assert LinuxFortune.fortune_options.include?("-s")
28
+ assert LinuxFortune.fortune_options.include?("-n")
29
+ 10.times do # check multiple times if the generated length is ok
30
+ lf = LinuxFortune.generate
31
+ assert lf.body.size < LinuxFortune.short_length # check if actual size is less than the max. short length
32
+ end
33
+ end
34
+
35
+ # verify that short gets set to false if configuring for long messages
36
+ def test_long
37
+ LinuxFortune.long = true
38
+ assert LinuxFortune.long # long should be set to short
39
+ assert LinuxFortune.short == false # short to false
40
+ assert LinuxFortune.fortune_options.include?("-l")
41
+ assert LinuxFortune.fortune_options.include?("-n")
42
+ 5.times do # check multiple times if the generated length is ok
43
+ lf = LinuxFortune.generate
44
+ #puts "#{lf.body.size} characters"
45
+ # TODO apparently there is an issue with 'fortune -l'; check fortune docs & bugs (manual mentions a different problem
46
+ assert lf.body.size+10 >= LinuxFortune.short_length # check if actual size is greater than the max. short length
47
+ end
48
+ end
49
+
50
+ # Fortune.body should be the same as Fortune.to_s
51
+ def test_to_s
52
+ lf = LinuxFortune.generate
53
+ assert lf.body == lf.to_s
54
+ end
55
+
56
+ # test that there are sources
57
+ # TODO refine test case
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?
62
+ end
63
+
64
+ # test if fortune messages are from the sources requested
65
+ # TODO pick random sources from sources
66
+ def test_fortune_from_sources
67
+ 5.times do
68
+ lf = LinuxFortune.generate(["chucknorris"])
69
+ #puts lf.source
70
+ #puts lf.source.split("/").last
71
+ assert ["chucknorris"].include?(lf.source.split("/").last.strip)
72
+ end
73
+ end
74
+ end
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linux_fortune
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - George Schreiber
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain:
16
+ - |
17
+ -----BEGIN CERTIFICATE-----
18
+ MIIDPjCCAiagAwIBAgIBADANBgkqhkiG9w0BAQUFADBFMRUwEwYDVQQDDAxneS5z
19
+ Y2hyZWliZXIxGDAWBgoJkiaJk/IsZAEZFghtb2JpbGl0eTESMBAGCgmSJomT8ixk
20
+ ARkWAmh1MB4XDTEwMDMyMjIyMjcyN1oXDTExMDMyMjIyMjcyN1owRTEVMBMGA1UE
21
+ AwwMZ3kuc2NocmVpYmVyMRgwFgYKCZImiZPyLGQBGRYIbW9iaWxpdHkxEjAQBgoJ
22
+ kiaJk/IsZAEZFgJodTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMJJ
23
+ s2WBsiNww5bbNAiHZEoEQgvmZA2G3+wiojnk7pYzecLbO5EIUy/SAGbOknjo5oZV
24
+ ltBcHezhvrC1rz1yGZj99rHRqIdVZeMDhWBqy0UlQ4sUFo/XMyTLCrjgR/uAC30R
25
+ 8VH862M5X437OOWzQtRA4HhO//7Wgnyqk5XDf9/4yPA7ZR9D9ByqsFMSB/lUbn1a
26
+ rvUqEYVjYG79n3Cr8xZ6N1Tq97Boyomnr1kfokpuvkSDXdjkOIvxrxcWADbpIDW6
27
+ hNl+aM43lvILA2XfoxnvNSl0kKWfMFJqEZZBpQUjYKCIf035qFEvM8bSYwsgJjBB
28
+ IZGcOiAN0cM1WdoYFXMCAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAw
29
+ HQYDVR0OBBYEFAqua0hB8DclDhesGYAgmSOOYXLcMA0GCSqGSIb3DQEBBQUAA4IB
30
+ AQAV+IGpY3FuM3hwGQzEHIvRbZiHl2xRMWjsyspgKAXi65ecAZo8LIsgdGznrOB7
31
+ Dd6QV4mzDVOnlCea92b3hZ3Cs0aKFDgXFbX8GChJ00vU32vOxFloMGX+TY8oApmR
32
+ sHX07jF6alv1gyAy1vk4K2aaOCQP8R87hjKJVosg/LCVDjYnpGgctiDIrWOQw6pm
33
+ ZYkcwKLRgPyxXGaiHG7b9JC9c7Zh9pHHGzOixPR6Hk8OFbtB4h2l4I5wHdv5Aey/
34
+ aIXWbEzN9SxPazhq7J72gIvE7VMxbKRVdZM4VY6Gk1Ucs9uimgYoaF2Pzzs8I9v4
35
+ 0ps4N+22wwDW/tu5hg8+hebo
36
+ -----END CERTIFICATE-----
37
+
38
+ date: 2010-03-23 00:00:00 +01:00
39
+ default_executable:
40
+ dependencies: []
41
+
42
+ description: A gem that provides a wrapper for the linux fortune program
43
+ email: gy.schreiber @nospam@ mobility.hu
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - README.rdoc
50
+ - lib/linux_fortune.rb
51
+ files:
52
+ - History.txt
53
+ - Manifest
54
+ - README.rdoc
55
+ - Rakefile
56
+ - lib/linux_fortune.rb
57
+ - test/test_helper.rb
58
+ - test/test_linux_fortune.rb
59
+ - linux_fortune.gemspec
60
+ has_rdoc: true
61
+ homepage: http://github.com/srejbi/linux_fortune
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --line-numbers
67
+ - --inline-source
68
+ - --title
69
+ - Linux_fortune
70
+ - --main
71
+ - README.rdoc
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 1
87
+ - 2
88
+ version: "1.2"
89
+ requirements: []
90
+
91
+ rubyforge_project: linux_fortune
92
+ rubygems_version: 1.3.6
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: A gem that provides a wrapper for the linux fortune program
96
+ test_files:
97
+ - test/test_helper.rb
98
+ - test/test_linux_fortune.rb
metadata.gz.sig ADDED
Binary file