ptools 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,6 @@
1
+ == 1.1.5 - 19-Jul-2007
2
+ * Added the File.image? method. Inspired by ruby-talk: 260487.
3
+
1
4
  == 1.1.4 - 21-May-2007
2
5
  * Removed the install.rb file. Installation is now handled exclusively by the
3
6
  rake install task.
data/MANIFEST CHANGED
@@ -7,6 +7,7 @@
7
7
  * test/ts_all.rb
8
8
  * test/tc_constants.rb
9
9
  * test/tc_head.rb
10
+ * test/tc_image.rb
10
11
  * test/tc_middle.rb
11
12
  * test/tc_nlconvert.rb
12
13
  * test/tc_null.rb
@@ -1,7 +1,7 @@
1
1
  require 'win32/file' if RUBY_PLATFORM.match('mswin')
2
2
 
3
3
  class File
4
- PTOOLS_VERSION = '1.1.3'
4
+ PTOOLS_VERSION = '1.1.5'
5
5
 
6
6
  # :stopdoc:
7
7
 
@@ -16,8 +16,27 @@ class File
16
16
  IS_WINDOWS = false
17
17
  end
18
18
 
19
+ IMAGE_EXT = %w/.bmp .gif .jpg .jpeg .png/
20
+
19
21
  # :startdoc:
20
22
 
23
+ # Returns whether or not the file is an image. Only JPEG, PNG, BMP and
24
+ # GIF are checked against.
25
+ #
26
+ # This method does some simple read and extension checks. For a version
27
+ # that is more robust, but which depends on a 3rd party C library (and is
28
+ # difficult to build on MS Windows), see the 'filemagic' library, available
29
+ # on the RAA.
30
+ #--
31
+ # Approach used here is based on information found at
32
+ # http://en.wikipedia.org/wiki/Magic_number_(programming)
33
+ #
34
+ def self.image?(file)
35
+ bool = IMAGE_EXT.include?(File.extname(file).downcase) # Match ext
36
+ bool = bmp?(file) || jpg?(file) || png?(file) || gif?(file) # Check data
37
+ bool
38
+ end
39
+
21
40
  # Returns the null device (aka bitbucket) on your platform. On most
22
41
  # Unix-like systems this is '/dev/null', on Windows it's 'NUL', etc.
23
42
  #--
@@ -281,4 +300,22 @@ class File
281
300
  return [bytes,chars,words,lines]
282
301
  end
283
302
  end
303
+
304
+ private
305
+
306
+ def self.bmp?(file)
307
+ IO.read(file, 3) == "BM6"
308
+ end
309
+
310
+ def self.jpg?(file)
311
+ IO.read(file, 10) == "\377\330\377\340\000\020JFIF"
312
+ end
313
+
314
+ def self.png?(file)
315
+ IO.read(file, 4) == "\211PNG"
316
+ end
317
+
318
+ def self.gif?(file)
319
+ ['GIF89a', 'GIF97a'].include?(IO.read(file, 6))
320
+ end
284
321
  end
@@ -9,7 +9,11 @@ require 'ptools'
9
9
 
10
10
  class TC_Constants < Test::Unit::TestCase
11
11
  def test_version
12
- assert_equal('1.1.4', File::PTOOLS_VERSION)
12
+ assert_equal('1.1.5', File::PTOOLS_VERSION)
13
+ end
14
+
15
+ def test_image_ext
16
+ assert_equal(%w/.bmp .gif .jpeg .jpg .png/, File::IMAGE_EXT.sort)
13
17
  end
14
18
 
15
19
  if RUBY_PLATFORM.match('mswin')
@@ -0,0 +1,32 @@
1
+ #####################################################################
2
+ # tc_image.rb
3
+ #
4
+ # Test case for the File.image? method. You should run this test
5
+ # via the 'rake test_image' task.
6
+ #####################################################################
7
+ require 'test/unit'
8
+ require 'ptools'
9
+
10
+ class TC_Ptools_Image < Test::Unit::TestCase
11
+ def setup
12
+ Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
13
+ @text_file = 'test_file1.txt'
14
+ end
15
+
16
+ def test_image_basic
17
+ assert_respond_to(File, :image?)
18
+ assert_nothing_raised{ File.image?(@text_file) }
19
+ end
20
+
21
+ def test_image_expected_results
22
+ assert_equal(false, File.image?(@text_file))
23
+ end
24
+
25
+ def test_image_expected_errors
26
+ assert_raises(Errno::ENOENT, ArgumentError){ File.image?('bogus') }
27
+ end
28
+
29
+ def teardown
30
+ @text_file = nil
31
+ end
32
+ end
@@ -9,6 +9,7 @@ $LOAD_PATH.unshift(Dir.pwd + "/test")
9
9
  require 'tc_binary'
10
10
  require 'tc_constants'
11
11
  require 'tc_head'
12
+ require 'tc_image'
12
13
  require 'tc_middle'
13
14
  require 'tc_nlconvert'
14
15
  require 'tc_null'
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: ptools
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.4
7
- date: 2007-05-21 00:00:00 -06:00
6
+ version: 1.1.5
7
+ date: 2007-07-19 00:00:00 -06:00
8
8
  summary: Extra methods for the File class
9
9
  require_paths:
10
10
  - lib
@@ -37,6 +37,7 @@ files:
37
37
  - test/tc_binary.rb
38
38
  - test/tc_constants.rb
39
39
  - test/tc_head.rb
40
+ - test/tc_image.rb
40
41
  - test/tc_middle.rb
41
42
  - test/tc_nlconvert.rb
42
43
  - test/tc_null.rb