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 +4 -4
- data/README.md +13 -0
- data/lib/attributes_json.rb +28 -0
- data/lib/audio.rb +2 -0
- data/lib/document.rb +2 -0
- data/lib/format_parser.rb +3 -1
- data/lib/format_parser/version.rb +1 -1
- data/lib/image.rb +2 -0
- data/lib/video.rb +2 -0
- data/spec/attributes_json_spec.rb +29 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 641f97911cf2f5a69318c06525d0360c554d4db0
|
4
|
+
data.tar.gz: 79eaf519017dc996431b7e185a07bf352e52de6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/lib/document.rb
CHANGED
data/lib/format_parser.rb
CHANGED
data/lib/image.rb
CHANGED
data/lib/video.rb
CHANGED
@@ -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.
|
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-
|
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
|