format_parser 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d0319cf9897c4d253b9b2202ef4d35477cc2d31
4
- data.tar.gz: 4ed1a85defea0ae296abe5e553e739e0d555d6e6
3
+ metadata.gz: 641f97911cf2f5a69318c06525d0360c554d4db0
4
+ data.tar.gz: 79eaf519017dc996431b7e185a07bf352e52de6d
5
5
  SHA512:
6
- metadata.gz: cf6c8801dad23ebab67e116d3d3e1da1a718198049d4c039b8abc4a6001b060c32300f69f73a7f35bf708566fbd9856c0d67b1e03300d7f84ad61b57c2a98d08
7
- data.tar.gz: d3aae7f141dbd7431f93555b483845579437cc936c7fe4b09a729c782d3ac7b7001e5df7ef27cea7b4fe4eec2b534148b74eec5f69f9dc5dfe563ea38c82c61b
6
+ metadata.gz: 80398fda150d24e68475e6b6f952d8f6a6f710109727993c73c77e63f42cd61224e5aea5b424981947e238433d9373cbc120afa9f63d4a6040852c5d61a285cf
7
+ data.tar.gz: 73edb441670ae6032746dde148891b2dabb870b75323614eef6664abcb88ebcc3c3b47c6695f724002b8da5934d6cc50f4fe99ea3f6fb035b9d07486f9640c3f
data/README.md CHANGED
@@ -41,6 +41,19 @@ You can also optimize the metadata extraction by providing hints to the gem:
41
41
  FormatParser.parse(File.open("myimage", "rb"), natures: [:video, :image], formats: [:jpg, :png, :mp4], results: :all)
42
42
  ```
43
43
 
44
+ If you need format_parser to return JSON, you can use `.as_json` to achieve this:
45
+
46
+ ```ruby
47
+ class Foo
48
+ include AttributesJSON
49
+ attr_accessor :number_of_bars
50
+ end
51
+
52
+ the_foo = Foo.new
53
+ the_foo.number_of_bars = 42
54
+ the_foo.as_json #=> {:number_of_bars => 42}
55
+ ```
56
+
44
57
  ## Creating your own parsers
45
58
 
46
59
  In order to create new parsers, you have to write a method or a Proc that accepts an IO and performs the
@@ -0,0 +1,28 @@
1
+ # Implements as_json as returning a Hash
2
+ # containing the return values of all the
3
+ # reader methods of an object that have
4
+ # associated pair writer methods.
5
+ #
6
+ # class Foo
7
+ # include AttributesJSON
8
+ # attr_accessor :number_of_bars
9
+ # end
10
+ # the_foo = Foo.new
11
+ # the_foo.number_of_bars = 42
12
+ # the_foo.as_json #=> {:number_of_bars => 42}
13
+ module FormatParser::AttributesJSON
14
+
15
+ # Implements a sane default `as_json` for an object
16
+ # that accessors defined
17
+ def as_json(*_maybe_root_option)
18
+ h = {}
19
+ h['nature'] = nature if respond_to?(:nature) # Needed for file info structs
20
+ methods.grep(/\w\=$/).each_with_object(h) do |attr_writer_method_name, h|
21
+ reader_method_name = attr_writer_method_name.to_s.gsub(/\=$/, '')
22
+ value = public_send(reader_method_name)
23
+ # When calling as_json on our members there is no need to pass the root: option given to us
24
+ # by the caller
25
+ h[reader_method_name] = value.respond_to?(:as_json) ? value.as_json : value
26
+ end
27
+ end
28
+ end
data/lib/audio.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module FormatParser
2
2
  class Audio
3
+ include FormatParser::AttributesJSON
4
+
3
5
  NATURE = :audio
4
6
 
5
7
  # Type of the file (e.g :mp3)
data/lib/document.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module FormatParser
2
2
  class Document
3
+ include FormatParser::AttributesJSON
4
+
3
5
  NATURE = :document
4
6
 
5
7
  attr_accessor :format
data/lib/format_parser.rb CHANGED
@@ -1,5 +1,7 @@
1
+ require 'set'
2
+
1
3
  module FormatParser
2
- require 'set'
4
+ require_relative 'attributes_json'
3
5
  require_relative 'image'
4
6
  require_relative 'audio'
5
7
  require_relative 'document'
@@ -1,3 +1,3 @@
1
1
  module FormatParser
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
data/lib/image.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module FormatParser
2
2
  class Image
3
+ include FormatParser::AttributesJSON
4
+
3
5
  NATURE = :image
4
6
 
5
7
  # What filetype was recognized? Will contain a non-ambiguous symbol
data/lib/video.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module FormatParser
2
2
  class Video
3
+ include FormatParser::AttributesJSON
4
+
3
5
  NATURE = :video
4
6
 
5
7
  attr_accessor :width_px
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe FormatParser::AttributesJSON do
4
+ it 'returns a hash of all the accessorized properties' do
5
+ anon_class = Class.new do
6
+ include FormatParser::AttributesJSON
7
+ attr_accessor :foo, :bar, :baz
8
+ def nature
9
+ 'good'
10
+ end
11
+ end
12
+ instance = anon_class.new
13
+ instance.foo = 42
14
+ instance.bar = 'abcdef'
15
+ expect(instance.as_json).to eq('nature' => 'good', 'foo' => 42, 'bar' => 'abcdef', 'baz' => nil)
16
+ expect(instance.as_json(root: true)).to eq('nature' => 'good', 'foo' => 42, 'bar' => 'abcdef', 'baz' => nil)
17
+ end
18
+
19
+ it 'is included into file information types' do
20
+ [
21
+ FormatParser::Image,
22
+ FormatParser::Video,
23
+ FormatParser::Audio,
24
+ FormatParser::Document
25
+ ].each do |file_related_class|
26
+ expect(file_related_class.ancestors).to include(FormatParser::AttributesJSON)
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: format_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Berman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-02-20 00:00:00.000000000 Z
12
+ date: 2018-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ks
@@ -159,6 +159,7 @@ files:
159
159
  - README.md
160
160
  - Rakefile
161
161
  - format_parser.gemspec
162
+ - lib/attributes_json.rb
162
163
  - lib/audio.rb
163
164
  - lib/care.rb
164
165
  - lib/document.rb
@@ -186,6 +187,7 @@ files:
186
187
  - lib/read_limiter.rb
187
188
  - lib/remote_io.rb
188
189
  - lib/video.rb
190
+ - spec/attributes_json_spec.rb
189
191
  - spec/care_spec.rb
190
192
  - spec/file_information_spec.rb
191
193
  - spec/format_parser_spec.rb