magic_bytes 1.0.1 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e4ba56d5cd2228b01a35c54c6c2e2192d47ed17d
4
- data.tar.gz: ab1ec8aaedbe75604fdeef68be0d09fa27e2c702
3
+ metadata.gz: f329a7e5d53256b5f4889346b2f95fee7159aa11
4
+ data.tar.gz: 0380ac028a591eb9210691d22a74e716ac11e9f5
5
5
  SHA512:
6
- metadata.gz: 3e2839aca9bd215da68122361b97ad6c488a95ebf1c2067d71f5d5dfda7114e7b90686b6eb28e7f256b3914a3d5550237af7f0336abfe3b138087c1554dd2536
7
- data.tar.gz: 14724a04c4e03aa52c9a50e1586f9e00aa79befbcc24880b2bbf112b5c88a15f3d64a5a2365f07feec068fc023d1bdd5106f07b03dbf38e200151fc2350e12f9
6
+ metadata.gz: 41a12b10b5136b481dfd3df78b108daee7ed3e2d965780e9aab96194aecd54be42cb0863094732cec98fe6cc71150c423f986c10b011b405f1749564bb1e8407
7
+ data.tar.gz: b70c2fd86ea65e9594c6363607da6444d77eb60f3a9438c8d79af02b92d56b5144ba19d2f69f02e1ade4173b80f2c41db33e36212570482af3f95f6c7e1e32b1
@@ -1,8 +1,15 @@
1
1
  module MagicBytes
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
 
4
- class FileType < Struct.new(:ext, :mime)
5
- end
4
+ # The maximum length supported (needed for .tar archives)
5
+ HEADER_SIZE = 262
6
+
7
+ # Gets raised when the file being read from is at EOF or empty
8
+ # (when read() from the file returns `nil`)
9
+ ReadError = Class.new(StandardError)
10
+
11
+ # Describes a file type with its file extension and MIME type
12
+ FileType = Struct.new(:ext, :mime)
6
13
 
7
14
  extend self
8
15
 
@@ -11,8 +18,9 @@ module MagicBytes
11
18
  # @param io[#read] a readable object
12
19
  # @return [Hash, nil] the hash of ext: and mime: or nil if the type could not be deduced
13
20
  def read_and_detect(io)
14
- n_bytes = 262 # The maximum length supported (needed for .tar archives)
15
- detect(io.read(n_bytes))
21
+ first_n_bytes = io.read(HEADER_SIZE)
22
+ raise ReadError unless first_n_bytes
23
+ detect(first_n_bytes)
16
24
  end
17
25
 
18
26
  # This is a line-for-line port of https://github.com/sindresorhus/file-type
@@ -21,6 +29,7 @@ module MagicBytes
21
29
  # @param header_bytes[String] the header bytes of the file
22
30
  # @return [Hash, nil] the hash of ext: and mime: or nil if the type could not be deduced
23
31
  def detect(header_bytes)
32
+ raise ReadError unless header_bytes
24
33
  d = _detect(header_bytes)
25
34
  FileType.new(d.fetch(:ext), d.fetch(:mime))
26
35
  end
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: magic_bytes 1.0.1 ruby lib
5
+ # stub: magic_bytes 1.0.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "magic_bytes"
9
- s.version = "1.0.1"
9
+ s.version = "1.0.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Julik Tarkhanov"]
14
- s.date = "2016-05-28"
14
+ s.date = "2016-06-13"
15
15
  s.description = "Basic file type checks based on a few header bytes"
16
16
  s.email = "me@julik.nl"
17
17
  s.extra_rdoc_files = [
@@ -5,7 +5,24 @@ def get_extension(ext, name='fixture')
5
5
  file_type = File.open(path, 'r'){|f| MagicBytes.read_and_detect(f) }
6
6
  end
7
7
 
8
+ describe 'MagicBytes.detect' do
9
+ it 'raises a meaningful exception with nil as argument' do
10
+ expect {
11
+ MagicBytes.detect(nil)
12
+ }.to raise_error(MagicBytes::ReadError)
13
+ end
14
+ end
15
+
8
16
  describe 'MagicBytes.read_and_detect' do
17
+ describe 'with an empty file' do
18
+ it 'raises a meaningful exception' do
19
+ tf = Tempfile.new('x')
20
+ expect {
21
+ MagicBytes.read_and_detect(tf)
22
+ }.to raise_error(MagicBytes::ReadError)
23
+ end
24
+ end
25
+
9
26
  describe 'with the original tests from the JS version' do
10
27
  extensions = [
11
28
  'jpg',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic_bytes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julik Tarkhanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-28 00:00:00.000000000 Z
11
+ date: 2016-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec