jsondoc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d667ff1f9ee92a1c9423f8c3b53df0693b30c19
4
+ data.tar.gz: e43d87d1e0bcc58910994324eec90b83e6837318
5
+ SHA512:
6
+ metadata.gz: d37da726b1e38bc70ed25faedb63a30de58ab92cffffdd38200f69fb8907d3d6e146a6cf1c51935854f1e427aa9bd9da2dfc92ccfe7da6f54ef6a9838f68413c
7
+ data.tar.gz: c07f374ba8c987565f4d707070b829173370c08ed18aafb50c62d892bb7211de51f817f2658082ef67cd23add042c65e53ae25065157dfecb0d1cd2a3ebe24ff
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ = 0.0.1
2
+ - Initial release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 John Wang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,73 @@
1
+ = JsonDoc
2
+
3
+ Generic JSON document base class to set/get document attributes based on JSON Schema, dump as JSON and support building of CSV and Excel workbooks. Subclasses can be built with additional functionality using the setAttr method. Primary use cases include being used with parsers to create JSON documents and to create CSV/Excel reports.
4
+
5
+ == Installation
6
+
7
+ === Gem Installation
8
+
9
+ Download and install jsondoc with the following:
10
+
11
+ gem install jsondoc
12
+
13
+ == Usage
14
+
15
+ require 'jsondoc'
16
+
17
+ mySchema = {
18
+ :type => 'My Document Type',
19
+ :properties => {
20
+ :first_name => { :type => 'string', :description => 'First Name', :default => '' },
21
+ :last_name => { :type => 'string', :description => 'Last Name', :default => '' },
22
+ :email_addresses => { :type => 'array' , :description => 'Email Addresses', :default => [] }
23
+ }
24
+ }
25
+
26
+ thisUser = JsonDoc.new( mySchema )
27
+ thisUser.setAttr( :first_name, 'John' )
28
+ thisUser.setAttr( :last_name, 'Doe' )
29
+
30
+ first_name = thisUser.getAttr( :first_name )
31
+
32
+ thisUserHash = thisUser.asHash
33
+ thisUserJson = thisUser.asJson
34
+
35
+ descs = thisUser.getDescArrayForProperties( [:first_name,:last_name] )
36
+ values = thisUser.getValArrayForProperties( [:first_name,:last_name] )
37
+
38
+ thisUser.pushAttr( :email_addresses, 'john@example.com' )
39
+ thisUser.pushAttr( :email_addresses, 'john.doe@example.com' )
40
+
41
+ == Notes
42
+
43
+ === Schema Validation
44
+
45
+ Schema validation is not provided in this version.
46
+
47
+ == Links
48
+
49
+ JSON
50
+
51
+ http://www.json.org/
52
+
53
+ JSON Schema
54
+
55
+ http://json-schema.org/
56
+
57
+ == Problems, Comments, Suggestions?
58
+
59
+ All of the above are most welcome. mailto:johncwang@gmail.com
60
+
61
+ == Credits
62
+
63
+ John Wang - http://johnwang.com
64
+
65
+ == License
66
+
67
+ JsonDoc is available under an MIT-style license.
68
+
69
+ :include: MIT-LICENSE
70
+
71
+ == Warranty
72
+
73
+ This software is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the JsonDoc library.'
9
+ Rake::TestTask.new do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/test_*.rb'
12
+ t.verbose = false
13
+ end
14
+
15
+ desc 'Generate RDoc documentation.'
16
+ RDoc::Task.new do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'jsondoc'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/jsondoc.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'jsondoc'
3
+ s.version = '0.0.1'
4
+ s.date = '2014-01-28'
5
+ s.summary = 'JsonDoc'
6
+ s.description = 'A base document object'
7
+ s.authors = ['John Wang']
8
+ s.email = 'john@johnwang.com'
9
+ s.files = [
10
+ 'CHANGELOG',
11
+ 'MIT-LICENSE',
12
+ 'README.rdoc',
13
+ 'Rakefile',
14
+ 'VERSION',
15
+ 'jsondoc.gemspec',
16
+ 'lib/jsondoc.rb',
17
+ 'lib/jsondoc/document.rb',
18
+ 'test/test_setup.rb'
19
+ ]
20
+ s.homepage = 'http://johnwang.com/'
21
+ s.license = 'MIT'
22
+ end
data/lib/jsondoc.rb ADDED
@@ -0,0 +1,3 @@
1
+ module JsonDoc
2
+ autoload :Document, 'jsondoc/document'
3
+ end
@@ -0,0 +1,140 @@
1
+ require 'json'
2
+
3
+ module JsonDoc
4
+ class Document
5
+
6
+ def initialize(dSchema=nil,bDefaultifyDoc=false,bIsStrict=true)
7
+ @dSchema = dSchema || self.getDefaultSchema()
8
+ @bDefaultifyDoc = bDefaultifyDoc ? true : false
9
+ @bIsStrict = bIsStrict ? true : false
10
+ @dDocument = self.getDefaultDocument()
11
+ end
12
+
13
+ def getDefaultSchema()
14
+ dSchema = {
15
+ :type => '',
16
+ :properties => {
17
+ :id => { :default => '', :description => 'Doc Id', :type => 'string' }
18
+ }
19
+ }
20
+ return dSchema
21
+ end
22
+
23
+ def getDefaultDocument()
24
+ dDocument = {}
25
+ if @bDefaultifyDoc && @dSchema.has_key?(:properties)
26
+ @dSchema[:properties].keys.each do |yKey|
27
+ dProperty = @dSchema[:properties][yKey]
28
+ xxVal = dProperty.has_key?(:default) ? dProperty[:default] : ''
29
+ dDocument[yKey] = xxVal
30
+ end
31
+ end
32
+ return dDocument
33
+ end
34
+
35
+ def getAttr(yKey=nil)
36
+ yKey = yKey.to_sym if yKey.kind_of?(String)
37
+ xxVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
38
+ return xxVal
39
+ end
40
+
41
+ def validateKey(yKey=nil)
42
+ if yKey.nil?
43
+ raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]"
44
+ end
45
+
46
+ bKeyExists = @dSchema.has_key?(:properties) && @dSchema[:properties].has_key?(yKey) ? true : false
47
+
48
+ if @bIsStrict && ! bKeyExists
49
+ raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}"
50
+ end
51
+
52
+ return true
53
+ end
54
+
55
+ def setAttr(yKey=nil,xxVal=nil)
56
+ yKey = yKey.to_sym if yKey.kind_of?(String)
57
+
58
+ self.validateKey(yKey)
59
+
60
+ @dDocument[yKey] = xxVal
61
+ end
62
+
63
+ def pushAttr(yKey=nil,xxVal=nil)
64
+ yKey = yKey.to_sym if yKey.kind_of?(String)
65
+
66
+ self.validateKey(yKey)
67
+
68
+ sType = @dSchema[:properties][yKey].has_key?(:type) ? @dSchema[:properties][yKey][:type] : nil
69
+
70
+ if sType == 'array'
71
+ if @dDocument.has_key?(yKey)
72
+ @dDocument[yKey].push(xxVal)
73
+ else
74
+ @dDocument[yKey] = [xxVal]
75
+ end
76
+ else
77
+ @dDocument[yKey] = xxVal
78
+ end
79
+ end
80
+
81
+ def fromJson(jDocument=nil)
82
+ if jDocument.kind_of?(String)
83
+ @dDocument = JSON.load(jDocument)
84
+ end
85
+ return self
86
+ end
87
+
88
+ def fromDict(dDocument=nil)
89
+ @dDocument = dDocument if dDocument.is_a?(Hash)
90
+ end
91
+
92
+ def asHash()
93
+ return @dDocument
94
+ end
95
+
96
+ def asJson()
97
+ return JSON.dump( self.asHash() )
98
+ end
99
+
100
+ def getValStringForProperties(aCols=nil)
101
+ aVals = self.getValArrayForProperties(aCols)
102
+ sVals = aVals.join("\t")
103
+ return sVals
104
+ end
105
+
106
+ def getValArrayForProperties(aCols=nil)
107
+ aVals = []
108
+ return aVals if aCols.nil?
109
+ aCols.each do |yKey|
110
+
111
+ yKey = yKey.to_sym if yKey.kind_of?(String)
112
+ xVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
113
+
114
+ aVals.push( xVal )
115
+ end
116
+ return aVals
117
+ end
118
+
119
+ def getDescStringForProperties(aCols=nil)
120
+ aVals = self.getDescArrayForProperties(aCols)
121
+ sVals = aVals.join("\t")
122
+ return sVals
123
+ end
124
+
125
+ def getDescArrayForProperties(aCols=nil)
126
+ aVals = []
127
+ return aVals if aCols.nil?
128
+ aCols.each do |yKey|
129
+
130
+ yKey = yKey.to_sym if yKey.kind_of?(String)
131
+ xxVal = ( @dSchema[:properties].has_key?(yKey) && @dSchema[:properties][yKey].has_key?(:description) && @dSchema[:properties][yKey][:description].length > 0 ) \
132
+ ? @dSchema[:properties][yKey][:description] : yKey.to_s
133
+
134
+ aVals.push( xxVal )
135
+ end
136
+ return aVals
137
+ end
138
+
139
+ end
140
+ end
@@ -0,0 +1,39 @@
1
+ require 'test/unit'
2
+ require 'jsondoc'
3
+
4
+ class JsonDocTest < Test::Unit::TestCase
5
+ def testSetup
6
+
7
+ schemaTest = {
8
+ :type => 'TestDocument',
9
+ :properties => {
10
+ :id => { :default => 'abc', :type => 'string', :description => 'Id' },
11
+ :foo => { :default => 'bar', :type => 'string', :description => 'Foo' },
12
+ :hello => { :default => 'world', :type => 'string', :description => 'Hello' },
13
+ :array => { :default => ['foo'], :type => 'array', :description => 'Array' }
14
+ }
15
+ }
16
+
17
+ jDoc = JsonDoc::Document.new(schemaTest,true,true)
18
+
19
+ assert_equal 'abc' , jDoc.getAttr(:id)
20
+ assert_equal "abc\tbar" , jDoc.getValStringForProperties([:id,:foo])
21
+ assert_equal ['bar','world'], jDoc.getValArrayForProperties([:foo,:hello])
22
+ assert_equal "Id\tFoo" , jDoc.getDescStringForProperties([:id,:foo])
23
+ assert_equal ['Foo','Hello'], jDoc.getDescArrayForProperties([:foo,:hello])
24
+
25
+ jDoc.setAttr(:foo,'baz')
26
+ assert_equal 'baz' , jDoc.getAttr(:foo)
27
+ assert_equal "abc\tbaz" , jDoc.getValStringForProperties([:id,:foo])
28
+ assert_equal ['abc','baz'] , jDoc.getValArrayForProperties([:id,:foo])
29
+
30
+ assert_equal ['foo'], jDoc.getAttr(:array)
31
+ jDoc.pushAttr(:array,'bar')
32
+ assert_equal ['foo','bar'], jDoc.getAttr(:array)
33
+ jDoc.pushAttr(:array,'baz')
34
+ assert_equal ['foo','bar','baz'], jDoc.getAttr(:array)
35
+ jDoc.setAttr(:array,['qux'])
36
+ assert_equal ['qux'], jDoc.getAttr(:array)
37
+
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsondoc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Wang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A base document object
14
+ email: john@johnwang.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG
20
+ - MIT-LICENSE
21
+ - README.rdoc
22
+ - Rakefile
23
+ - VERSION
24
+ - jsondoc.gemspec
25
+ - lib/jsondoc.rb
26
+ - lib/jsondoc/document.rb
27
+ - test/test_setup.rb
28
+ homepage: http://johnwang.com/
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.2.1
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: JsonDoc
52
+ test_files: []