rfortune 0.1.2

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.
Files changed (6) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +30 -0
  3. data/VERSION +1 -0
  4. data/lib/rfortune.rb +117 -0
  5. data/test/test_rfortune.rb +64 -0
  6. metadata +59 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Helge Rausch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = rfortune
2
+
3
+ A Library for handling cookie jar files as known from 'fortune'.
4
+
5
+ == INSTALLATION
6
+
7
+ The latest development version can be found at GitHub:
8
+
9
+ http://github.com/tsujigiri/rfortune
10
+
11
+ The latest stable version can be installed from Gemcutter:
12
+
13
+ gem install rfortune
14
+
15
+ == SYNOPSIS:
16
+
17
+ To instantiate a RFortune you have to give the relative path to a
18
+ cookie jar file as an argument. If there is no such file relative to the
19
+ current directory it will be searched beneath /usr/share/games/fortunes.
20
+
21
+ cookies = RFortune.new 'cookie_jar_file'
22
+
23
+ To get a random cookie from the jar simply do:
24
+
25
+ puts cookies.random
26
+
27
+ == Copyright
28
+
29
+ Copyright (c) 2009 Helge Rausch. See LICENSE for details.
30
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/rfortune.rb ADDED
@@ -0,0 +1,117 @@
1
+ # This library provides handling of cookie jar files as used by
2
+ # 'fortune' to read its fortunes from.
3
+ #
4
+ # Author:: Helge Rausch (mailto:helge@rauschenimweltnetz.de)
5
+ # Copyright:: Copyright (c) 2009 Helge Rausch
6
+ # License:: Distributes under the same terms as Ruby
7
+
8
+ # The RFortune class reads the contents from a cookie jar file and
9
+ # stores its content in an array. It takes the path of a cookie jar file
10
+ # as an argument and searches for it first locally (relative or
11
+ # absolute), than beneath /usr/share/games/fortunes.
12
+
13
+ class RFortune
14
+
15
+ FortunesPath = '/usr/share/games/fortunes/'
16
+
17
+ # Looks for the specified cookie jar file first localy, than under
18
+ # /usr/share/games/fortunes, opens it and reads its content to the
19
+ # @cookies array. Without an argument or if the file specified does
20
+ # not exist it creates a new cookie jar.
21
+ def initialize file_path = nil
22
+
23
+ @file_path = file_path
24
+
25
+ unless @file_path.nil?
26
+
27
+ if File.exist?( @file_path )
28
+
29
+ if File.writable?( @file_path )
30
+ mode = 'r+'
31
+ else
32
+ mode = 'r'
33
+ end
34
+
35
+ cookie_jar_file = File.open( @file_path, mode )
36
+
37
+ elsif File.exist?( FortunesPath + @file_path )
38
+
39
+ cookie_jar_file = File.open( FortunesPath + @file_path, 'r' )
40
+
41
+ elsif File.exist?( FortunesPath + 'off/' + @file_path )
42
+
43
+ cookie_jar_file = File.open( FortunesPath + 'off/' + @file_path, 'r' )
44
+
45
+ end
46
+
47
+ end
48
+
49
+ unless cookie_jar_file.nil?
50
+
51
+ @cookies = cookie_jar_file.read.split( '%' )
52
+
53
+ cookie_jar_file.close
54
+
55
+ # delete empty cookies
56
+ @cookies.delete_if { |cookie| cookie.match( /^[\r\n\s]+$/ ) }
57
+
58
+ else
59
+
60
+ @cookies = []
61
+
62
+ end
63
+
64
+ end
65
+
66
+
67
+ # Returns a random cookie
68
+ def random
69
+
70
+ @cookies[ rand( @cookies.size ) ]
71
+
72
+ end
73
+
74
+
75
+ # Returns the number of cookies in the jar
76
+ def count
77
+
78
+ @cookies.size
79
+
80
+ end
81
+
82
+ # Adds a cookie to the jar
83
+ def add cookie
84
+
85
+ @cookies += [ cookie ] unless cookie.empty?
86
+
87
+ end
88
+
89
+ # Returns all cookies as an array
90
+ def all
91
+
92
+ @cookies
93
+
94
+ end
95
+
96
+ # Returns all cookies in cookie jar file format
97
+ def all_formated
98
+
99
+ all_cookies = @cookies.collect { |c| c = "\n%\n" + c }
100
+
101
+ all_cookies += [ "\n%\n" ]
102
+
103
+ all_cookies.to_s.sub( /^\n+/, '' )
104
+
105
+ end
106
+
107
+ # Saves the content of the jar to the file specified by argument given
108
+ # to itself or to the new method
109
+ def save new_file_path = nil
110
+
111
+ new_file_path or new_file_path = @file_path or raise 'No file name given'
112
+
113
+ File.open( new_file_path, 'w') { |jar| jar.puts all_formated }
114
+
115
+ end
116
+
117
+ end
@@ -0,0 +1,64 @@
1
+ require 'test/unit'
2
+ require 'lib/rfortune'
3
+
4
+ Cookie_jar_file = 'food'
5
+
6
+ class RFortuneTest < Test::Unit::TestCase
7
+
8
+ def test_init
9
+ assert_not_nil RFortune.new( Cookie_jar_file ), 'Instantiated a new RFortune'
10
+ end
11
+
12
+ def test_random
13
+ assert_instance_of String, RFortune.new( Cookie_jar_file ).random, 'Got a random cookie from the jar'
14
+ end
15
+
16
+ def test_count
17
+ assert_instance_of Fixnum, RFortune.new( Cookie_jar_file ).count
18
+ end
19
+
20
+ def test_add_cookie_and_save
21
+
22
+ cookies = RFortune.new( 'tmp_test_new_cookie_jar' )
23
+
24
+ cookies.add( 'Simsalabimbambasaladusaladim' )
25
+
26
+ cookies.save
27
+
28
+ jar_file_content = ''
29
+
30
+ if File.exist?( 'tmp_test_new_cookie_jar' )
31
+
32
+ jar_file_content = File.open( 'tmp_test_new_cookie_jar', 'r' ).read
33
+
34
+ File.delete( 'tmp_test_new_cookie_jar' )
35
+
36
+ end
37
+
38
+ assert_equal "%\nSimsalabimbambasaladusaladim\n%\n", jar_file_content, 'Check if the cookie jar file has been written'
39
+ end
40
+
41
+ def test_init_without_argument_and_save
42
+
43
+ cookies = RFortune.new
44
+
45
+ assert_not_nil cookies, 'Instantiate RFortune without specifying a file'
46
+
47
+ assert_raise RuntimeError do
48
+
49
+ cookies.save
50
+
51
+ end
52
+
53
+ assert_nothing_raised do
54
+
55
+ cookies.save 'tmp_test_cookie_jar'
56
+
57
+ end
58
+
59
+ File.exist?( 'tmp_test_cookie_jar' ) and File.delete( 'tmp_test_cookie_jar' )
60
+
61
+ end
62
+
63
+ end
64
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rfortune
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Helge Rausch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-30 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: RFortune provides handling of cookie jar files as known from 'fortune'
17
+ email: helge@rauschenimweltnetz.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/rfortune.rb
26
+ - test/test_rfortune.rb
27
+ - LICENSE
28
+ - README.rdoc
29
+ - VERSION
30
+ has_rdoc: true
31
+ homepage: http://github.com/tsujigiri/rfortune
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: RFortune provides handling of cookie jar files as known from 'fortune'
58
+ test_files: []
59
+