pnjson 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +36 -0
- data/LICENSE +22 -0
- data/README.md +16 -0
- data/Rakefile +8 -0
- data/lib/pnjson.rb +62 -0
- data/lib/pnjson/chunk.rb +34 -0
- data/lib/pnjson/error.rb +5 -0
- data/lib/pnjson/version.rb +5 -0
- data/pnjson.gemspec +27 -0
- data/spec/fixtures/test.png +0 -0
- data/spec/fixtures/test.txt +1 -0
- data/spec/pnjson_spec.rb +46 -0
- data/spec/spec_helper.rb +12 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f5f86054f4ebae5cc2c960f0fc7357bec616e660
|
4
|
+
data.tar.gz: 66e7b9caa3092d3bb04acc248d06c839992db344
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 19575c33c5338adc77fdba82f6861aae0ce0ae2384e6fbe8485140b071836595e0450117b7d5a4947121ce7f3e8c229e70b232e9b38bc969423f5901e768b2d5
|
7
|
+
data.tar.gz: 19cfdc0b14535fbf95210135aff579ebba472aab8a90d050bdd6fe444953043744c58cb212896d25ecfbf2f70f79d72e34fbe49e6c2a5e19eea455cc4d5fc63c
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pnjson (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
json (1.8.3)
|
11
|
+
rake (10.4.2)
|
12
|
+
rspec (3.3.0)
|
13
|
+
rspec-core (~> 3.3.0)
|
14
|
+
rspec-expectations (~> 3.3.0)
|
15
|
+
rspec-mocks (~> 3.3.0)
|
16
|
+
rspec-core (3.3.2)
|
17
|
+
rspec-support (~> 3.3.0)
|
18
|
+
rspec-expectations (3.3.1)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.3.0)
|
21
|
+
rspec-mocks (3.3.2)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.3.0)
|
24
|
+
rspec-support (3.3.0)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
json
|
31
|
+
pnjson!
|
32
|
+
rake
|
33
|
+
rspec
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
1.10.6
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Mahesh Baheti
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
data/Rakefile
ADDED
data/lib/pnjson.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'pnjson/chunk'
|
3
|
+
require 'pnjson/error'
|
4
|
+
|
5
|
+
module Pnjson
|
6
|
+
class Pnjson
|
7
|
+
attr_accessor :header
|
8
|
+
attr_accessor :chunks
|
9
|
+
|
10
|
+
HEADER_PNG = "89504e470d0a1a0a"
|
11
|
+
|
12
|
+
def initialize(filename)
|
13
|
+
raw = File.open(filename).read
|
14
|
+
hex_png = ascii_to_hex(raw)
|
15
|
+
|
16
|
+
raw.close
|
17
|
+
|
18
|
+
raise Error::InvalidPng unless valid_png?(hex_png)
|
19
|
+
|
20
|
+
@header = hex_png.shift(8).join
|
21
|
+
@chunks = chunkify(hex_png)
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_json
|
25
|
+
{ header: @header, chunks: @chunks.map { |c| c.to_hash } }.to_json
|
26
|
+
end
|
27
|
+
|
28
|
+
def valid_png?(hex_png)
|
29
|
+
hex_png[0..7].join == HEADER_PNG
|
30
|
+
end
|
31
|
+
|
32
|
+
def chunkify(array)
|
33
|
+
return [] if array.empty?
|
34
|
+
length = convert_base(array.shift(4).join, 16, 10).to_i
|
35
|
+
|
36
|
+
[Chunk.new({
|
37
|
+
length: length,
|
38
|
+
type: array.shift(4).join,
|
39
|
+
data: array.shift(length).join,
|
40
|
+
crc: array.shift(4).join
|
41
|
+
}), chunkify(array)].flatten
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def ascii_to_hex(raw_str)
|
47
|
+
raw_str.each_byte.map do |byte|
|
48
|
+
b = byte.to_s(16)
|
49
|
+
b.length == 1 ? "0#{b}" : b
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def convert_base(str, from, to)
|
54
|
+
str.to_i(from).to_s(to)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module_function
|
59
|
+
def open(filename)
|
60
|
+
Pnjson.new(filename)
|
61
|
+
end
|
62
|
+
end
|
data/lib/pnjson/chunk.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Pnjson
|
2
|
+
class Chunk
|
3
|
+
attr_accessor :length
|
4
|
+
attr_accessor :type
|
5
|
+
attr_accessor :data
|
6
|
+
attr_accessor :crc
|
7
|
+
|
8
|
+
CRITICAL_CHUNKS_TYPES = %w( IHDR PLTE IDAT IEND )
|
9
|
+
ANCILLARY_CHUNKS_TYPES = %w(bKGD cHRM gAMA hIST iCCP iTXt pHYs sBIT sPLT
|
10
|
+
sRGB sTER tEXt tIME tRNS zTXt)
|
11
|
+
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
@length = options[:length]
|
15
|
+
@type = hex_to_ascii(options[:type])
|
16
|
+
@data = options[:data]
|
17
|
+
@crc = options[:crc]
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_hash
|
21
|
+
{
|
22
|
+
length: @length,
|
23
|
+
type: @type,
|
24
|
+
data: @data,
|
25
|
+
crc: @crc
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def hex_to_ascii(str)
|
31
|
+
str.scan(/../).map { |x| x.hex.chr }.join
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/pnjson/error.rb
ADDED
data/pnjson.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'pnjson/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = 'pnjson'
|
9
|
+
|
10
|
+
# Do not change the version and date fields by hand. This will be done
|
11
|
+
# automatically by the gem release script.
|
12
|
+
s.version = Pnjson::VERSION
|
13
|
+
|
14
|
+
s.summary = "Pure ruby library for converting png images to json."
|
15
|
+
s.description = "Pure ruby library for converting png images to json."
|
16
|
+
|
17
|
+
s.authors = ['Mahesh Baheti']
|
18
|
+
s.email = ['mahsh.baheti@gmail.com']
|
19
|
+
s.homepage = 'http://www.github.com/avellable/pnjson'
|
20
|
+
s.license = 'MIT'
|
21
|
+
|
22
|
+
s.add_development_dependency('rake')
|
23
|
+
s.add_development_dependency('rspec', '~> 3')
|
24
|
+
|
25
|
+
s.files = `git ls-files`.split($/)
|
26
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
27
|
+
end
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
test
|
data/spec/pnjson_spec.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Pnjson do
|
4
|
+
it "should have a VERSION constant" do
|
5
|
+
expect(Pnjson.const_defined?('VERSION')).to eql true
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Pnjson::Pnjson do
|
10
|
+
let(:png_file) { Pnjson::Pnjson.new(fixture_file("test.png")) }
|
11
|
+
|
12
|
+
describe '#initialize' do
|
13
|
+
context "invalid png" do
|
14
|
+
let(:filename) { fixture_file("test.txt") }
|
15
|
+
|
16
|
+
it "raises InvalidPng exception" do
|
17
|
+
expect { Pnjson::Pnjson.new(filename) }.to raise_error(Pnjson::Error::InvalidPng)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "valid png" do
|
22
|
+
let(:filename) { fixture_file("test.png") }
|
23
|
+
|
24
|
+
it "returns valid instance of Pnjson::Pnjson" do
|
25
|
+
expect { Pnjson::Pnjson.new(filename) }.not_to raise_error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#valid_png' do
|
31
|
+
subject { png_file.valid_png?(hash) }
|
32
|
+
context "invalid png" do
|
33
|
+
let(:hash) { [ "af", "ae"] }
|
34
|
+
it "returns false if file invalid" do
|
35
|
+
is_expected.to eq false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "valid png" do
|
40
|
+
let(:hash) { [ "89", "50", "4e", "47", "0d", "0a", "1a", "0a"] }
|
41
|
+
it "returns true if file valid" do
|
42
|
+
is_expected.to eq true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pnjson
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mahesh Baheti
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3'
|
41
|
+
description: Pure ruby library for converting png images to json.
|
42
|
+
email:
|
43
|
+
- mahsh.baheti@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/pnjson.rb
|
54
|
+
- lib/pnjson/chunk.rb
|
55
|
+
- lib/pnjson/error.rb
|
56
|
+
- lib/pnjson/version.rb
|
57
|
+
- pnjson.gemspec
|
58
|
+
- spec/fixtures/test.png
|
59
|
+
- spec/fixtures/test.txt
|
60
|
+
- spec/pnjson_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
homepage: http://www.github.com/avellable/pnjson
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.0.14
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Pure ruby library for converting png images to json.
|
86
|
+
test_files:
|
87
|
+
- spec/fixtures/test.png
|
88
|
+
- spec/fixtures/test.txt
|
89
|
+
- spec/pnjson_spec.rb
|
90
|
+
- spec/spec_helper.rb
|