file_data 4.0.0 → 5.0.0

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: 008003613bb4284356911dcc023da0a9190f9787
4
- data.tar.gz: 95cfd92bf198612c9e20796577809f507cd866ab
3
+ metadata.gz: d38cf3da31fc0fc355f64228574f06888f52563e
4
+ data.tar.gz: 6babcaa52b7fe146f4a310eb2eb34868522edd2b
5
5
  SHA512:
6
- metadata.gz: fd9ae2b5d47292ee6a0318dce866f97fa4d42ad812de9e05e5e1c18e140fa0186ab93045af8a7c0c8fafc01b9ad5d9e2e0fd7b77fee2d86d9de873d2fbe29274
7
- data.tar.gz: 759531661d30f168362e75494e2dff6c003cefc3dd6819d6875882df32280aa617e00f98d16648f003633a80ba173c9610d40ddb20ab6648bcbbc65bc2bc17ab
6
+ metadata.gz: f537057f31aa2f9021cdf9f9d75a94fb03024f2e9b796f99c56a26c3826a7b801931771e73ffecdee4492050eb57fc320403b8007dbda3403317af577b95428b
7
+ data.tar.gz: 4ffd9045d319937d8b2561ee046eca8e6a08ca6cadcf938f4a3a132ec872aca310645c8b7a517ef6ed7e8f4f1793e9736fc3483010fd9a18ddaf87ab4635e596
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- file_data (4.0.0)
4
+ file_data (5.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -53,4 +53,4 @@ DEPENDENCIES
53
53
  rspec (~> 3.0)
54
54
 
55
55
  BUNDLED WITH
56
- 1.14.3
56
+ 1.14.6
data/README.md CHANGED
@@ -20,40 +20,42 @@ To read exif data a stream of the jpeg data should be used as input. For perform
20
20
  Examples:
21
21
 
22
22
  ```ruby
23
- # Get Exif data from a file path or stream
23
+ ## Get Exif data from a file path or stream
24
24
 
25
- # Complete example with file path...
26
- tag_value = FileData::Exif.new.image_data_only('/path/to/file.jpg')
25
+ # The file path or stream is passed as a parameter to class methods on FileData::Exif
26
+ # rather than having the file path or stream set in the constructor and the methods
27
+ # be instance methods to ensure that the user falls into a "pit of success" by only
28
+ # opening the file once to get tag values rather than potentially multiple times
27
29
 
28
- # Complete example with a file stream...
30
+ # Get image exif data from a file path...
31
+ hash = FileData::Exif.image_data_only('/path/to/file.jpg')
32
+
33
+ # Get thumbnail exif data from a file stream...
29
34
  File.open('/path/to/file.jpg', 'rb') do |f|
30
- hash = FileData::Exif.new.image_data_only(f)
35
+ hash = FileData::Exif.thumbnail_data_only(f)
31
36
  end
32
37
 
33
- # Examples for all commands
34
-
35
- ## Commands that get multiple tag values
36
-
37
- # Get only the image Exif data
38
- hash = FileData::Exif.new.image_data_only(file_path_or_stream)
39
-
40
- # Get only the thumbnail Exif data
41
- hash = FileData::Exif.new.thumbnail_data_only(file_path_or_stream)
38
+ ## Extract values depending on how much data is returned
42
39
 
43
- # Get all data (image and thumbnail)
44
- # Use result.image or result.thumbnail to get value hashes
45
- result = FileData::Exif.new.all_data(file_path_or_stream)
40
+ # Image and thumbnail data are returned as a hash with keys
41
+ # being the full tag name if it exists in FileData::ExifTags (see below listing)
42
+ # or "#{ifd_id}-#{tag_id}" if there is no existing name for the tag in FileData::ExifTags
43
+ image_hash = FileData::Exif.image_data_only(file_path_or_stream)
44
+ image_description_value = image_hash[:Other_ImageDescription]
45
+ unknown_gps_tag_value = image_hash["34853-99999"]
46
46
 
47
- ## Commands that get a single tag value
48
- # tag_key is section/tag_id from the description in the first paragraph
49
- # tag_key values can be taken from the hash keys in FileData::ExifTags.tag_groups
50
- # all FileData::ExifTags.tag_groups keys are given after the examples
47
+ thumbnail_hash = FileData::Exif.thumbnail_data_only(file_path_or_stream)
48
+ image_width_tag_value = thumbnail_hash[:Image_Structure_Width]
49
+ unknown_gps_tag_value = thumbnail_hash["Tiff-99999"]
51
50
 
52
- # Image example
53
- tag_value = FileData::Exif.new.only_image_tag(file_path_or_stream, tag_id)
51
+ all_data = FileData::Exif.all_data(file_path_or_stream)
52
+ image_hash = all_data.image
53
+ thumbnail_hash = all_data.thumbnail
54
54
 
55
- # Thumbnail example
56
- tag_value = FileData::Exif.new.only_thumbnail_tag(file_path_or_stream, tag_id)
55
+ # For extracting only a specific tag the value is returned and the search key must be
56
+ # an array matching the keys needed to get traverse FileData:ExifTags (see below listing)
57
+ image_description_value = FileData::Exif.only_image_tag(file_path_or_stream, [:Tiff, 270])
58
+ unknown_exif_tag_value = FileData::Exif.only_thumbnail_tag(file_path_or_stream, [34665, 2])
57
59
  ```
58
60
 
59
61
  ## Known Tag Keys
@@ -5,22 +5,23 @@ module FileData
5
5
  # Convenience class for extracting exif data from a file or stream
6
6
  class Exif
7
7
  # Create methods that forward to ExifReader
8
- ExifReader.public_instance_methods.each do |method_name|
9
- define_method(method_name) do |input, *other_args|
8
+ # Each method requires the stream as a parameter to help the user
9
+ # fall into a "pit of success" by only opening and closing
10
+ # the stream once to get data
11
+ ExifReader.public_instance_methods(false).each do |method_name|
12
+ define_singleton_method(method_name) do |input, *other_args|
10
13
  delegate_to_exif_reader(input, method_name, other_args)
11
14
  end
12
15
  end
13
16
 
14
- private
15
-
16
- def delegate_to_exif_reader(input, name, other_args)
17
+ def self.delegate_to_exif_reader(input, name, other_args)
17
18
  streamify(input) do |stream|
18
19
  exif = ExifJpeg.new(stream).exif
19
- ExifReader.new.send(name, exif, *other_args)
20
+ ExifReader.new(exif).send(name, *other_args)
20
21
  end
21
22
  end
22
23
 
23
- def streamify(input)
24
+ def self.streamify(input)
24
25
  if input.is_a?(String)
25
26
  File.open(input, 'rb') { |f| yield f }
26
27
  else
@@ -6,47 +6,51 @@ require_relative 'exif_data'
6
6
  module FileData
7
7
  # Returns the exif data from a jpeg file
8
8
  class ExifReader
9
- def image_data_only(exif_stream)
10
- exif_tags_internal(exif_stream, 0).image
9
+ def initialize(exif_stream)
10
+ @exif_stream = exif_stream
11
11
  end
12
12
 
13
- def thumbnail_data_only(exif_stream)
14
- exif_tags_internal(exif_stream, 1).thumbnail
13
+ def image_data_only
14
+ exif_tags_internal(0).image
15
15
  end
16
16
 
17
- def all_data(exif_stream)
18
- exif_tags_internal(exif_stream, 0, 1)
17
+ def thumbnail_data_only
18
+ exif_tags_internal(1).thumbnail
19
19
  end
20
20
 
21
- def only_image_tag(exif_stream, tag_id)
22
- exif_tag_internal(exif_stream, 0, tag_id)
21
+ def all_data
22
+ exif_tags_internal(0, 1)
23
23
  end
24
24
 
25
- def only_thumbnail_tag(exif_stream, tag_id)
26
- exif_tag_internal(exif_stream, 1, tag_id)
25
+ def only_image_tag(tag_id)
26
+ exif_tag_internal(0, tag_id)
27
27
  end
28
28
 
29
- def tags(exif_stream, *ifds_to_include)
30
- return [] if exif_stream.nil?
29
+ def only_thumbnail_tag(tag_id)
30
+ exif_tag_internal(1, tag_id)
31
+ end
32
+
33
+ def tags(*ifds_to_include)
34
+ return [] if @exif_stream.nil?
31
35
 
32
- exif_stream.read_header
33
- ExifTagReader.new(exif_stream, *ifds_to_include).tags
36
+ @exif_stream.read_header
37
+ ExifTagReader.new(@exif_stream, *ifds_to_include).tags
34
38
  end
35
39
 
36
40
  private
37
41
 
38
- def exif_tags_internal(exif_stream, *ifds_to_include)
39
- tags(exif_stream, *ifds_to_include).each_with_object(ExifData.new) do |tag_info, data|
40
- data.add_tag(*tag_info, exif_stream.read_tag_value)
42
+ def exif_tags_internal(*ifds_to_include)
43
+ tags(@exif_stream, *ifds_to_include).each_with_object(ExifData.new) do |tag_info, data|
44
+ data.add_tag(*tag_info, @exif_stream.read_tag_value)
41
45
  end
42
46
  end
43
47
 
44
- def exif_tag_internal(exif_stream, ifd_index, tag_to_find)
45
- exif_stream.read_tag_value if find_tag(exif_stream, ifd_index, tag_to_find)
48
+ def exif_tag_internal(ifd_index, tag_to_find)
49
+ @exif_stream.read_tag_value if find_tag(ifd_index, tag_to_find)
46
50
  end
47
51
 
48
- def find_tag(exif_stream, ifd_index, tag_to_find)
49
- tags(exif_stream, ifd_index).find do |_, ifd_id, tag_num|
52
+ def find_tag(ifd_index, tag_to_find)
53
+ tags(@exif_stream, ifd_index).find do |_, ifd_id, tag_num|
50
54
  tag_to_find == [ifd_id, tag_num]
51
55
  end
52
56
  end
@@ -1,3 +1,3 @@
1
1
  module FileData
2
- VERSION = '4.0.0'.freeze
2
+ VERSION = '5.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-19 00:00:00.000000000 Z
11
+ date: 2017-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler