ruby-magic 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEIDCCAoigAwIBAgIBATANBgkqhkiG9w0BAQsFADAdMRswGQYDVQQDDBJrdy9E
3
+ Qz1saW51eC9EQz1jb20wHhcNMjEwMTAxMDAwOTU0WhcNMjYwMTAxMDAwOTU0WjAd
4
+ MRswGQYDVQQDDBJrdy9EQz1saW51eC9EQz1jb20wggGiMA0GCSqGSIb3DQEBAQUA
5
+ A4IBjwAwggGKAoIBgQCv02+pOrAMVUipLY7qjNLx8OMfMjnVCBH21SsWn8gyIHE9
6
+ BtQ+tlsJU8nWNjlr++7SKIV5vQmyYsC2UY0cdcFASdoV0A3Lc2Z0makfvPgcIlpL
7
+ 5THqQtP11tJfhjQ3MSFJYRG2D6m6GNqA+WTTsvJbPIxy5PHv5xb+79LLzcVxB9Op
8
+ C0ervTOF8W6Ye4EDvhQ5tppOr/d26UQV+8zRuxjxx5uZSPfUrE9u/gIIdeROoB3T
9
+ cnjAfzvjIzampLQppvhUnT4Vf5Rum2LohZ5GosKLy7tM65vr1eLyI2ndvS1MXWrU
10
+ 05sFKm5CSqORD+TVkR1jd3z5HdPgO1KwadccxFixoI8kEIt/iixWTzbka2T2iJoq
11
+ 5DeNMEMYYAZKuAGBzBcgvgPccCeAXFVF9AHjfaGRUMmYNHKK2HD6AdgNdhcE18V+
12
+ CJxFtggCJFYc1ygFOsHZPTbKo9+3ybSQhAswtDosWt26utUT1JOc2Bkj1sgQ0Yrk
13
+ orANsdYz2ee3JFkzx08CAwEAAaNrMGkwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAw
14
+ HQYDVR0OBBYEFME41mMBb/XAHgjhlX4kE8xnXxewMBcGA1UdEQQQMA6BDGt3QGxp
15
+ bnV4LmNvbTAXBgNVHRIEEDAOgQxrd0BsaW51eC5jb20wDQYJKoZIhvcNAQELBQAD
16
+ ggGBAF/2ryrSgTpf0+wYG46Oi6V3p9A3uMxxbGsh0ZmlLQT1Z1ozclR+xzxzyIFC
17
+ i7YWYyR085brDBnYNXgnUT9Nm4iAYx58MaPD28IFrcX0XA3DJE6Wkx4MmMV0chiJ
18
+ kf5ASikfGtFtf9qDmpLQB4louB6HpKNq1MwsQo8zwY93t77HCiV5f0g43rcZfGTn
19
+ wfBhpT7oOVWgfTLeBD811aS52VUCWclmnz+f01GdTeMwqsgc6oJaPSvg4JCMN94O
20
+ wcBlxus69rhM8PeTJNhwLxzRCNnZ69t9k2cU+4lTmQQIB8gnEFJxY9gZdgQ6pej0
21
+ HXUIut+6pp5BRYF2mTZ3fnxhWyIZaeB7lla2gI6rV2GXDbp4kVplGEuQ975YklJw
22
+ i4y7bz2Es4Hkly2Nc/J2L0+uMoBhMWMY3Xl0XDdkZYtBdnSB5g7NacChaAsukEkd
23
+ Ozt6vfXxSzW/KfodB+0ELjlHWiFbI8w7c8Kt7/ceZGVsXTxD/f4tVWy8MmxFy3pY
24
+ Cpe8Kg==
25
+ -----END CERTIFICATE-----
data/lib/magic.rb CHANGED
@@ -1,32 +1,10 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- # :stopdoc:
4
-
5
- #
6
- # magic.rb
7
- #
8
- # Copyright 2013-2015 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
1
+ # frozen_string_literal: true
22
2
 
23
3
  require_relative 'magic/magic'
24
4
  require_relative 'magic/version'
25
5
  require_relative 'magic/core/file'
26
6
  require_relative 'magic/core/string'
27
7
 
28
- # :startdoc:
29
-
30
8
  #
31
9
  # File _Magic_ in Ruby.
32
10
  #
@@ -37,12 +15,12 @@ class Magic
37
15
  # call-seq:
38
16
  # magic.inspect -> string
39
17
  #
40
- # Returns
41
- #
42
18
  # Example:
43
19
  #
44
- # magic = Magic.new #=> #<Magic:0x007fd5258a1108>
45
- # magic.inspect #=> "#<Magic:0x007fd5258a1108 @flags=0, @path=[\"/etc/magic\", \"/usr/share/misc/magic\"]>"
20
+ # magic = Magic.new
21
+ # magic.inspect #=> "#<Magic:0x007fd5258a1108 @flags=0, @paths=[\"/etc/magic\", \"/usr/share/misc/magic\"]>"
22
+ # magic.close
23
+ # magic.inspect #=> "#<Magic:0x007fd5258a1108 @flags=0, @paths=[\"/etc/magic\", \"/usr/share/misc/magic\"] (closed)>"
46
24
  #
47
25
  def inspect
48
26
  super.insert(-2, self.closed? ? ' (closed)' : '')
@@ -50,17 +28,11 @@ class Magic
50
28
 
51
29
  #
52
30
  # call-seq:
53
- # magic.flags_to_a( names ) -> array
31
+ # magic.flags_list( boolean ) -> array
54
32
  #
55
- # Returns an +array+
56
- #
57
- # Example:
33
+ # See also: Magic#flags and Magic#flags_names
58
34
  #
59
- # Will raise <i>Magic::LibraryError</i> exception if, or
60
- #
61
- # See also: Magic#flags
62
- #
63
- def flags_to_a(names = false)
35
+ def flags_list(names = false)
64
36
  raise LibraryError, 'Magic library is not open' if closed?
65
37
  return [names ? 'NONE' : 0] if @flags.zero?
66
38
 
@@ -78,17 +50,22 @@ class Magic
78
50
  flags
79
51
  end
80
52
 
53
+ alias_method :flags_to_a, :flags_list
54
+
55
+ # call-seq:
56
+ # magic.flags_names -> array
57
+ #
58
+ # See also: Magic#flags and Magic#flags_list
59
+ #
60
+ def flags_names
61
+ flags_list(true)
62
+ end
63
+
81
64
  class << self
82
65
  #
83
66
  # call-seq:
84
- # Magic.open( flags ) -> self
85
- # Magic.open( flags ) {|magic| block } -> string or array
86
- #
87
- # Returns
88
- #
89
- # Example:
90
- #
91
- # Will raise <i></i>
67
+ # Magic.open( integer ) -> self
68
+ # Magic.open( integer ) {|magic| block } -> string or array
92
69
  #
93
70
  # See also: Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check
94
71
  #
@@ -112,12 +89,6 @@ class Magic
112
89
  # Magic.mime -> self
113
90
  # Magic.mime {|magic| block } -> string or array
114
91
  #
115
- # Returns
116
- #
117
- # Example:
118
- #
119
- # Will raise <i></i>
120
- #
121
92
  # See also: Magic::open, Magic::type, Magic::encoding, Magic::compile and Magic::check
122
93
  #
123
94
  def mime(&block)
@@ -129,12 +100,6 @@ class Magic
129
100
  # Magic.type -> self
130
101
  # Magic.type {|magic| block } -> string or array
131
102
  #
132
- # Returns
133
- #
134
- # Example:
135
- #
136
- # Will raise <i></i>
137
- #
138
103
  # See also: Magic::open, Magic::mime, Magic::encoding, Magic::compile and Magic::check
139
104
  #
140
105
  def type(&block)
@@ -146,12 +111,6 @@ class Magic
146
111
  # Magic.encoding -> self
147
112
  # Magic.encoding {|magic| block } -> string or array
148
113
  #
149
- # Returns
150
- #
151
- # Example:
152
- #
153
- # Will raise <i></i>
154
- #
155
114
  # See also: Magic::open, Magic::mime, Magic::type, Magic::compile and Magic::check
156
115
  #
157
116
  def encoding(&block)
@@ -160,55 +119,85 @@ class Magic
160
119
 
161
120
  #
162
121
  # call-seq:
163
- # Magic.compile( path, ... ) -> true
164
- # Magic.compile( array ) -> true
122
+ # Magic.compile( string, ... ) -> true
123
+ # Magic.compile( array ) -> true
165
124
  #
166
- # Returns
125
+ # See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check
167
126
  #
168
- # Example:
127
+ def compile(*paths)
128
+ open {|m| m.compile(paths) }
129
+ end
130
+
169
131
  #
170
- # Will raise <i></i>
132
+ # call-seq:
133
+ # Magic.check( string, ... ) -> true or false
134
+ # Magic.check( array ) -> true or false
171
135
  #
172
- # See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check
136
+ # See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile
173
137
  #
174
- def compile(path)
175
- open {|m| m.compile(path) }
138
+ def check(*paths)
139
+ open {|m| m.check(paths) }
176
140
  end
177
141
 
178
142
  #
179
143
  # call-seq:
180
- # Magic.check( path, ... ) -> true or false
181
- # Magic.check( array ) -> true or false
144
+ # Magic.file( object ) -> string or array
145
+ # Magic.file( string ) -> string or array
146
+ # Magic.file( string, integer ) -> string or array
182
147
  #
183
- # Returns
148
+ # See also:
184
149
  #
185
- # Example:
150
+ def file(path, flags = Magic::MIME)
151
+ open(flags).file(path)
152
+ end
153
+
186
154
  #
187
- # Will raise <i></i>
155
+ # call-seq:
156
+ # Magic.buffer( string ) -> string or array
157
+ # Magic.buffer( string, integer ) -> string or array
188
158
  #
189
- # See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile
159
+ # See also:
190
160
  #
191
- def check(path)
192
- open {|m| m.check(path) }
161
+ def buffer(buffer, flags = Magic::MIME)
162
+ open(flags).buffer(buffer)
193
163
  end
194
- end
195
164
 
196
- alias_method :flags_array, :flags_to_a
165
+ #
166
+ # call-seq:
167
+ # Magic.descriptor( object ) -> string or array
168
+ # Magic.descriptor( integer ) -> string or array
169
+ # Magic.descriptor( integer, integer ) -> string or array
170
+ #
171
+ # See also:
172
+ #
173
+ def descriptor(fd, flags = Magic::MIME)
174
+ open(flags).descriptor(fd)
175
+ end
176
+
177
+ alias_method :fd, :descriptor
178
+ end
197
179
 
198
180
  private
199
181
 
182
+ def power_of_two?(number)
183
+ number > 0 && Math.log2(number) % 1 == 0
184
+ end
185
+
200
186
  def flags_as_map
201
- self.class.constants.inject({}) do |flags,constant|
202
- value = self.class.const_get(constant)
203
- flags[value] = constant.to_s if value.is_a?(Fixnum)
204
- flags
187
+ klass = self.class
188
+
189
+ klass.constants.each_with_object({}) do |constant, flags|
190
+ constant = constant.to_s
191
+
192
+ next if constant.start_with?('PARAM_')
193
+
194
+ value = klass.const_get(constant)
195
+
196
+ if value.is_a?(Integer) && power_of_two?(value)
197
+ flags[value] = constant
198
+ end
205
199
  end
206
200
  end
207
201
  end
208
202
 
209
- # :enddoc:
210
-
211
203
  FileMagic = Magic
212
-
213
- # vim: set ts=2 sw=2 sts=2 et :
214
- # encoding: utf-8
@@ -1,57 +1,21 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- # :stopdoc:
4
-
5
- #
6
- # core/file.rb
7
- #
8
- # Copyright 2013-2015 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
22
-
23
- # :startdoc:
24
-
25
- #
26
- #
27
- #
28
3
  class File
29
4
  class << self
30
5
  #
31
6
  # call-seq:
32
- # File.magic( path ) -> string or array
33
- #
34
- # Returns
35
- #
36
- # Example:
7
+ # File.magic( object ) -> string or array
8
+ # File.magic( string ) -> string or array
37
9
  #
38
10
  # See also: File::mime and File::type
39
11
  #
40
12
  def magic(path, flags = Magic::NONE)
41
- path = path.path if path.respond_to?(:path)
42
- path ||= path.to_path if path.respond_to?(:to_path)
43
- path ||= path.to_s
44
-
45
- Magic.open(flags | Magic::ERROR) {|mgc| mgc.file(path) }
13
+ Magic.open(flags) {|mgc| mgc.file(path) }
46
14
  end
47
15
 
48
16
  #
49
17
  # call-seq:
50
- # File.mime( path ) -> string or array
51
- #
52
- # Returns
53
- #
54
- # Example:
18
+ # File.mime( string ) -> string or array
55
19
  #
56
20
  # See also: File::magic and File::type
57
21
  #
@@ -61,11 +25,7 @@ class File
61
25
 
62
26
  #
63
27
  # call-seq:
64
- # File.type( path ) -> string or array
65
- #
66
- # Returns
67
- #
68
- # Example:
28
+ # File.type( string ) -> string or array
69
29
  #
70
30
  # See also: File::magic and File::mime
71
31
  #
@@ -76,11 +36,7 @@ class File
76
36
 
77
37
  #
78
38
  # call-seq:
79
- # File.magic( path ) -> string or array
80
- #
81
- # Returns
82
- #
83
- # Example:
39
+ # File.magic -> string or array
84
40
  #
85
41
  # See also: File#mime and File#type
86
42
  #
@@ -90,11 +46,7 @@ class File
90
46
 
91
47
  #
92
48
  # call-seq:
93
- # File.mime( path ) -> string or array
94
- #
95
- # Returns
96
- #
97
- # Example:
49
+ # File.mime -> string or array
98
50
  #
99
51
  # See also: File#magic and File#type
100
52
  #
@@ -104,11 +56,7 @@ class File
104
56
 
105
57
  #
106
58
  # call-seq:
107
- # File.type( path ) -> string or array
108
- #
109
- # Returns
110
- #
111
- # Example:
59
+ # File.type -> string or array
112
60
  #
113
61
  # See also: File#magic and File#mime
114
62
  #
@@ -116,8 +64,3 @@ class File
116
64
  self.class.type(self)
117
65
  end
118
66
  end
119
-
120
- # :enddoc:
121
-
122
- # vim: set ts=2 sw=2 sts=2 et :
123
- # encoding: utf-8
@@ -1,39 +1,10 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- # :stopdoc:
4
-
5
- #
6
- # core/string.rb
7
- #
8
- # Copyright 2013-2015 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
13
- #
14
- # http://www.apache.org/licenses/LICENSE-2.0
15
- #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
21
- #
22
-
23
- # :startdoc:
24
-
25
- #
26
- #
27
- #
28
3
  class String
29
4
  #
30
5
  # call-seq:
31
6
  # string.magic -> string or array
32
7
  #
33
- # Returns
34
- #
35
- # Example:
36
- #
37
8
  # See also: String#mime and String#type
38
9
  #
39
10
  def magic(flags = Magic::NONE)
@@ -44,10 +15,6 @@ class String
44
15
  # call-seq:
45
16
  # string.mime -> string or array
46
17
  #
47
- # Returns
48
- #
49
- # Example:
50
- #
51
18
  # See also: String#magic and String#type
52
19
  #
53
20
  def mime
@@ -58,18 +25,9 @@ class String
58
25
  # call-seq:
59
26
  # string.type -> string or array
60
27
  #
61
- # Returns
62
- #
63
- # Example:
64
- #
65
28
  # See also: String#magic and String#mime
66
29
  #
67
30
  def type
68
31
  magic(Magic::MIME_TYPE)
69
32
  end
70
33
  end
71
-
72
- # :enddoc:
73
-
74
- # vim: set ts=2 sw=2 sts=2 et :
75
- # encoding: utf-8