file_classify 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/file_classify.gemspec +26 -0
- data/lib/file_classify.rb +42 -0
- data/lib/file_classify/version.rb +3 -0
- data/spec/lib/file_classify_spec.rb +75 -0
- data/spec/resources/ascii.txt +22 -0
- data/spec/resources/binary.jpg +0 -0
- data/spec/spec_helper.rb +2 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2145249459482451c944d8510de554d92175533b
|
4
|
+
data.tar.gz: 1cad6de9ade4bcdca69d201063657e51c85d1db3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6bded4aabd3bdd4fd4f2c83f3209a7d304b88898b58e670a82993683e40146d9dd5760a0c8dca8745dac301469324d298d12c42be6f655e7ce46510ec40439e9
|
7
|
+
data.tar.gz: 19c94ceccd39958acbad53dc4a2b1a8327f1c5e09bfd5b73a456d14b7c26f46290177b85457acb9ee18fe965159dad3664698d7443317f084b4cbe767d070c57
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ryan Caught
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# FileClassify
|
2
|
+
|
3
|
+
Binary and ASCII classification for files
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'file_classify'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install file_classify
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'file_classify/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "file_classify"
|
8
|
+
spec.version = FileClassify::VERSION
|
9
|
+
spec.authors = ["Ryan Caught"]
|
10
|
+
spec.email = ["rcaught@gmail.com"]
|
11
|
+
spec.description = %q{Binary and ASCII classification for files}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = "https://github.com/rcaught/file_classify"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "coveralls"
|
25
|
+
spec.add_development_dependency "awesome_print"
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "file_classify/version"
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
class FileClassify
|
5
|
+
attr_accessor :contents, :path
|
6
|
+
|
7
|
+
def initialize(file_options = {})
|
8
|
+
raise ArgumentError if [:contents, :path].none? { |x| file_options.include?(x) }
|
9
|
+
|
10
|
+
@contents = file_options[:contents]
|
11
|
+
@path = file_options[:path]
|
12
|
+
end
|
13
|
+
|
14
|
+
# Based off the binary? method from ptools
|
15
|
+
# https://github.com/djberg96/ptools/blob/a43e133b4ee10750c0d4a7f68ab5be92269bbb56/lib/ptools.rb#L78
|
16
|
+
def binary?
|
17
|
+
file_path = self.path
|
18
|
+
|
19
|
+
if self.contents
|
20
|
+
temp = Tempfile.new('block_size_check')
|
21
|
+
temp.write(self.contents)
|
22
|
+
temp.close
|
23
|
+
|
24
|
+
file_path = temp.path
|
25
|
+
end
|
26
|
+
|
27
|
+
s = (File.read(file_path, File.stat(file_path).blksize) || "").split(//)
|
28
|
+
return ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
|
29
|
+
end
|
30
|
+
|
31
|
+
def ascii?
|
32
|
+
self.binary? ? false : true
|
33
|
+
end
|
34
|
+
|
35
|
+
def classify
|
36
|
+
if self.binary?
|
37
|
+
return 'binary'
|
38
|
+
else
|
39
|
+
return 'ascii'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'file_classify'
|
3
|
+
|
4
|
+
BINARY_FILE = 'spec/resources/binary.jpg'
|
5
|
+
ASCII_FILE = 'spec/resources/ascii.txt'
|
6
|
+
|
7
|
+
describe FileClassify do
|
8
|
+
let :binary_contents_subject do
|
9
|
+
FileClassify.new(contents: File.read(BINARY_FILE))
|
10
|
+
end
|
11
|
+
|
12
|
+
let :binary_path_subject do
|
13
|
+
FileClassify.new(path: BINARY_FILE)
|
14
|
+
end
|
15
|
+
|
16
|
+
let :ascii_contents_subject do
|
17
|
+
FileClassify.new(contents: File.read(ASCII_FILE))
|
18
|
+
end
|
19
|
+
|
20
|
+
let :ascii_path_subject do
|
21
|
+
FileClassify.new(path: ASCII_FILE)
|
22
|
+
end
|
23
|
+
|
24
|
+
context '#initialize' do
|
25
|
+
it 'path' do
|
26
|
+
fc = FileClassify.new(path: BINARY_FILE)
|
27
|
+
fc.path.should == BINARY_FILE
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'contents' do
|
31
|
+
fc = FileClassify.new(contents: File.read(BINARY_FILE))
|
32
|
+
fc.contents.should == File.read(BINARY_FILE)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'no args' do
|
36
|
+
expect { FileClassify.new }.to raise_error(ArgumentError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context '#classify' do
|
41
|
+
it 'binary' do
|
42
|
+
binary_contents_subject.classify.should == 'binary'
|
43
|
+
binary_path_subject.classify.should == 'binary'
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'ascii' do
|
47
|
+
ascii_contents_subject.classify.should == 'ascii'
|
48
|
+
ascii_path_subject.classify.should == 'ascii'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context '#binary?' do
|
53
|
+
it 'binary' do
|
54
|
+
binary_contents_subject.binary?.should be_true
|
55
|
+
binary_path_subject.binary?.should be_true
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'ascii' do
|
59
|
+
ascii_contents_subject.binary?.should be_false
|
60
|
+
ascii_path_subject.binary?.should be_false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context '#ascii?' do
|
65
|
+
it 'ascii' do
|
66
|
+
ascii_contents_subject.ascii?.should be_true
|
67
|
+
ascii_path_subject.ascii?.should be_true
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'binary' do
|
71
|
+
binary_contents_subject.ascii?.should be_false
|
72
|
+
binary_path_subject.ascii?.should be_false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Stopping by Woods on a Snowy Evening
|
2
|
+
Robert Frost
|
3
|
+
|
4
|
+
Whose woods these are I think I know.
|
5
|
+
His house is in the village, though;
|
6
|
+
He will not see me stopping here
|
7
|
+
To watch his woods fill up with snow.
|
8
|
+
|
9
|
+
My little horse must think it queer
|
10
|
+
To stop without a farmhouse near
|
11
|
+
Between the woods and frozen lake
|
12
|
+
The darkest evening of the year.
|
13
|
+
|
14
|
+
He gives his harness bells a shake
|
15
|
+
To ask if there is some mistake.
|
16
|
+
The only other sound's the sweep
|
17
|
+
Of easy wind and downy flake.
|
18
|
+
|
19
|
+
The woods are lovely, dark, and deep,
|
20
|
+
But I have promises to keep,
|
21
|
+
And miles to go before I sleep,
|
22
|
+
And miles to go before I sleep.
|
Binary file
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_classify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Caught
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: awesome_print
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Binary and ASCII classification for files
|
84
|
+
email:
|
85
|
+
- rcaught@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- file_classify.gemspec
|
97
|
+
- lib/file_classify.rb
|
98
|
+
- lib/file_classify/version.rb
|
99
|
+
- spec/lib/file_classify_spec.rb
|
100
|
+
- spec/resources/ascii.txt
|
101
|
+
- spec/resources/binary.jpg
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
homepage: https://github.com/rcaught/file_classify
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.0.3
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Binary and ASCII classification for files
|
127
|
+
test_files:
|
128
|
+
- spec/lib/file_classify_spec.rb
|
129
|
+
- spec/resources/ascii.txt
|
130
|
+
- spec/resources/binary.jpg
|
131
|
+
- spec/spec_helper.rb
|