file_type_detector_sfedu 0.2.0 → 0.2.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
  SHA256:
3
- metadata.gz: 5a5b6d66dd409a03792b309a5a50070c32ec1cb8a3578d0d9f791511425013d5
4
- data.tar.gz: 3a714ef0dd6ef17e4e05c562c4a3f4b10bceb11d8c8f297469c6c7063d7d3ebb
3
+ metadata.gz: 58a8d37d3931c29954f7870c6125905261def1d31d71bcd4ebcba1e7499b4a61
4
+ data.tar.gz: f9201f1e4bfec70f6a63dce6fa652cbac8e215f82078e47f93daff2b4bc661cd
5
5
  SHA512:
6
- metadata.gz: 99e503196037d1ca0fb09ae51011f4f510ad81ff03045b356ee1e2df0eb7f4fc81d3aec90d20b9f7ee9b7a460ebf2aa65d71f59ffe8393d51d7bb884a0b1a49c
7
- data.tar.gz: b4b0bd07b6ed8d92204dff6ee187d5375bdcbe44294b94b0a9d84526ed905ca1484c003d93c09ee6cc6404a104e94d02c0b575fb22ecd4d2b5604ffca127ebea
6
+ metadata.gz: 1251c06470e171d5fda3eff3a1514253e80305a5cbe8d3e24c190f522e4f445dcd9de2ddbd8f75668737e5c108173bfafaa39b3f4a9daffcaaf2b438acf7e4bf
7
+ data.tar.gz: 7aed639f2d0f518aa09dbdb711b79695551f5f47e9336df265d8344294d8379ee66ad7b2e3141d342211550803f05dfe4f49f1aa0e8da5982a22e611937a6b1b
data/CHANGELOG.md CHANGED
@@ -6,7 +6,7 @@
6
6
  - Initial release
7
7
 
8
8
 
9
- ## [0.2.0] - 2024-03-2
9
+ ## [0.2.0] - 2024-04-5
10
10
 
11
11
  - Tests have been updated to test functions more correctly
12
12
  - The existing code has been significantly improved:
@@ -16,4 +16,14 @@
16
16
  - Improved code style
17
17
  - Added functions for checking new types of extensions: png, jpeg/jpg, gif, json, xml
18
18
  - Updated the readme file
19
+ - Other minor improvements
20
+
21
+ ## [0.2.1] - 2024-04-6
22
+
23
+ - Added documentation to all functions
24
+ - Other minor improvements
25
+
26
+ ## [0.2.2] - 2024-04-08
27
+
28
+ - Bug fixed
19
29
  - Other minor improvements
data/README.md CHANGED
@@ -28,13 +28,21 @@ puts FileTypeDetector.check("path/to/your/file.pdf") # if it's truly pdf, value
28
28
 
29
29
  You can also use a specific detector:
30
30
  ```ruby
31
- puts FileTypeDetector.pdf_check("path/to/your/real_pdf.pdf") # true
32
- puts FileTypeDetector.pdf_check("path/to/your/fake_pdf.pdf") # false
31
+ puts FileTypeDetector.pdf_check("path/to/your/real_pdf.pdf") # true
32
+ puts FileTypeDetector.pdf_check("path/to/your/fake_pdf.pdf") # false
33
33
 
34
34
  puts FileTypeDetector.docx_check("path/to/your/real_docx.docx") # true
35
35
  puts FileTypeDetector.docx_check("path/to/your/fake_docx.docx") # false
36
36
  ```
37
37
 
38
+ You can also run all the detectors on the file to determine its type using the `identify` method:
39
+ ```ruby
40
+ puts FileTypeDetector.identify("path/to/your/real_pdf.pdf") # "pdf"
41
+ puts FileTypeDetector.identify("path/to/your/real_xml.pdf") # "xml"
42
+ puts FileTypeDetector.identify("path/to/your/real_docx.docx") # docx
43
+ puts FileTypeDetector.identify("path/to/your/real_jpeg.png") # jpeg
44
+ puts FileTypeDetector.identify("path/to/your/unknown.png") # nil
45
+ ```
38
46
  ## Supported File Types
39
47
  The gem provides support for detecting the following file types:
40
48
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileTypeDetector
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  end
@@ -2,9 +2,12 @@
2
2
 
3
3
  require_relative "file_type_detector/version"
4
4
 
5
+ # Provides methods for detecting file types based on their content.
5
6
  module FileTypeDetector
6
- # @param [String] file_path
7
- # @return [Boolean]
7
+ # Determines the file type based on its content.
8
+ #
9
+ # @param [String] file_path The path to the file to be checked.
10
+ # @return [Boolean] Returns true if the file type is recognized, otherwise false.
8
11
  def self.check(file_path)
9
12
  if error_handling(file_path)
10
13
  file_extension = File.extname(file_path).downcase
@@ -17,8 +20,21 @@ module FileTypeDetector
17
20
  false
18
21
  end
19
22
 
23
+ # Identifies the file type based on its content.
24
+ #
25
+ # @param [String] file_path The path to the file to be identified.
26
+ # @return [String] Returns the identified file type.
27
+ def self.identify(file_path)
28
+ return unless error_handling(file_path)
29
+
30
+ FILE_CHECKS.find { |fn| fn.call(file_path)}.name.to_s.split("_")[0]
31
+ end
32
+
20
33
  # =============================================
21
- # Error handling
34
+ # Error handling for file operations.
35
+ #
36
+ # @param [String] file_path The path to the file to be checked.
37
+ # @return [Boolean] Returns true if no errors are encountered, otherwise raises IOError.
22
38
  def self.error_handling(file_path)
23
39
  raise IOError, "File '#{file_path}' not found" unless File.exist?(file_path)
24
40
  raise IOError, "File '#{file_path}' is not a file" unless File.file?(file_path)
@@ -28,37 +44,52 @@ module FileTypeDetector
28
44
  end
29
45
 
30
46
  # =============================================
31
- # Write your methods here
47
+ # Checks if the file has a PDF format.
48
+ #
49
+ # @param [String] file_path The path to the file to be checked.
50
+ # @return [Boolean] Returns true if the file has a PDF format, otherwise false.
32
51
  def self.pdf_check(file_path)
33
52
  return unless error_handling(file_path)
34
53
 
35
54
  File.read(file_path, 5) == "%PDF-"
36
-
37
55
  end
38
56
 
57
+ # Checks if the file has a DOCX format.
58
+ #
59
+ # @param [String] file_path The path to the file to be checked.
60
+ # @return [Boolean] Returns true if the file has a DOCX format, otherwise false.
39
61
  def self.docx_check(file_path)
40
62
  return unless error_handling(file_path)
41
63
 
42
64
  File.open(file_path, "rb") { |file| file.read(4) } == "PK\x03\x04"
43
-
44
65
  end
45
66
 
67
+ # Checks if the file has a PNG format.
68
+ #
69
+ # @param [String] file_path The path to the file to be checked.
70
+ # @return [Boolean] Returns true if the file has a PNG format, otherwise false.
46
71
  def self.png_check(file_path)
47
72
  return unless error_handling(file_path)
48
73
 
49
74
  first_bytes = File.open(file_path, "rb") { |file| file.read(16) }
50
- first_bytes.start_with?("\x89PNG\r\n\x1A\n".b)
51
-
75
+ first_bytes&.start_with?("\x89PNG\r\n\x1A\n".b)
52
76
  end
53
77
 
78
+ # Checks if the file has a GIF format.
79
+ #
80
+ # @param [String] file_path The path to the file to be checked.
81
+ # @return [Boolean] Returns true if the file has a GIF format, otherwise false.
54
82
  def self.gif_check(file_path)
55
83
  return unless error_handling(file_path)
56
84
 
57
85
  first_bytes = File.open(file_path, "rb") { |file| file.read(6) }
58
86
  %w[GIF87a GIF89a].include?(first_bytes)
59
-
60
87
  end
61
88
 
89
+ # Checks if the file has a JPEG format.
90
+ #
91
+ # @param [String] file_path The path to the file to be checked.
92
+ # @return [Boolean] Returns true if the file has a JPEG format, otherwise false.
62
93
  def self.jpeg_check(file_path)
63
94
  return unless error_handling(file_path)
64
95
 
@@ -67,10 +98,18 @@ module FileTypeDetector
67
98
 
68
99
  end
69
100
 
101
+ # Checks if the file has a JPG format.
102
+ #
103
+ # @param [String] file_path The path to the file to be checked.
104
+ # @return [Boolean] Returns true if the file has a JPG format, otherwise false.
70
105
  def self.jpg_check(file_path)
71
106
  jpeg_check(file_path)
72
107
  end
73
108
 
109
+ # Checks if the file has a JSON format.
110
+ #
111
+ # @param [String] file_path The path to the file to be checked.
112
+ # @return [Boolean] Returns true if the file has a JSON format, otherwise false.
74
113
  def self.json_check(file_path)
75
114
  return unless error_handling(file_path)
76
115
 
@@ -78,14 +117,18 @@ module FileTypeDetector
78
117
  first_chars.start_with?("{") || first_chars.start_with?("[")
79
118
  end
80
119
 
120
+ # Checks if the file has a XML format.
121
+ #
122
+ # @param [String] file_path The path to the file to be checked.
123
+ # @return [Boolean] Returns true if the file has a XML format, otherwise false.
81
124
  def self.xml_check(file_path)
82
125
  return unless error_handling(file_path)
83
126
 
84
- (File.open(file_path, "rb") { |file| file.read(2) }) == "<?"
85
-
127
+ File.open(file_path, "rb") { |file| file.read(2) } == "<?"
86
128
  end
87
129
 
88
130
  # =============================================
131
+ # Array containing check functions for different file types.
89
132
  FILE_CHECKS = [
90
133
  method(:pdf_check),
91
134
  method(:docx_check),
@@ -94,7 +137,7 @@ module FileTypeDetector
94
137
  method(:jpeg_check),
95
138
  method(:jpg_check),
96
139
  method(:json_check),
97
- method(:xml_check),
140
+ method(:xml_check)
98
141
  # Add your methods here
99
142
  ].freeze
100
143
 
data/ruby_gem.gemspec CHANGED
@@ -5,7 +5,7 @@ require_relative "lib/file_type_detector/version"
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "file_type_detector_sfedu"
7
7
  spec.version = FileTypeDetector::VERSION
8
- spec.authors = ["Garik", "watermelon0guy", "synthematikj", "kamchatnaya"]
8
+ spec.authors = ["Garik Mnacakanyan", "Mikhail Babichev", "Maria Putrova", "Elena Kamchatnaya"]
9
9
  spec.email = ["garfild_2003@mail.ru"]
10
10
 
11
11
  spec.summary = "A gem for detecting file types based on their extensions or content."
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_type_detector_sfedu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
- - Garik
8
- - watermelon0guy
9
- - synthematikj
10
- - kamchatnaya
7
+ - Garik Mnacakanyan
8
+ - Mikhail Babichev
9
+ - Maria Putrova
10
+ - Elena Kamchatnaya
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2024-04-05 00:00:00.000000000 Z
14
+ date: 2024-04-08 00:00:00.000000000 Z
15
15
  dependencies: []
16
16
  description: FileTypeDetector is a Ruby gem that provides functionality for determining
17
17
  the type of a file based on its extension or content.