jsontrim 0.1.1

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Levin Alexander
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,18 @@
1
+ = jsontrim
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Levin Alexander. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "jsontrim"
8
+ gem.summary = %Q{remove unimportant elements from a json object}
9
+ gem.description = %Q{remove unimportant elements from a json object}
10
+ gem.email = "mail@levinalex.net"
11
+ gem.homepage = "http://github.com/levinalex/jsontrim"
12
+ gem.authors = ["Levin Alexander"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.add_dependency "json"
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "jsontrim #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/jsontrim.gemspec ADDED
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{jsontrim}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Levin Alexander"]
12
+ s.date = %q{2009-10-09}
13
+ s.description = %q{remove unimportant elements from a json object}
14
+ s.email = %q{mail@levinalex.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "jsontrim.gemspec",
27
+ "lib/jsontrim.rb",
28
+ "test/jsontrim_test.rb",
29
+ "test/test_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/levinalex/jsontrim}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{remove unimportant elements from a json object}
36
+ s.test_files = [
37
+ "test/jsontrim_test.rb",
38
+ "test/test_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ s.add_runtime_dependency(%q<json>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ s.add_dependency(%q<json>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ s.add_dependency(%q<json>, [">= 0"])
55
+ end
56
+ end
data/lib/jsontrim.rb ADDED
@@ -0,0 +1,97 @@
1
+ require 'json'
2
+
3
+ # takes a JSON object, removes specified subtrees and trims arrays
4
+ # returns a pretty printed string
5
+ #
6
+ # before = <<-EOF
7
+ # { "foo": {
8
+ # "one": { "a": 1, "b": {"c": 2} },
9
+ # "two": "value",
10
+ # "three": { "b": [1,2,3] }},
11
+ # "bar": [1,2,3,4,5] }
12
+ # EOF
13
+ #
14
+ # blacklist = [
15
+ # "foo:!two", # delete the value before['foo']['two']
16
+ # "foo:*:!b", # delete all the keys called "b" nested two levels under "foo"
17
+ # "bar:+" # only keep the first element of the foo-list
18
+ # ]
19
+ #
20
+ # after = JSONTrim.cut(before, :ignore => blacklist)
21
+ #
22
+ # {
23
+ # "foo": {
24
+ # "two": ...,
25
+ # "three": {
26
+ # "b": [ ... ]
27
+ # },
28
+ # "one": {
29
+ # "a": 1,
30
+ # "b": { ... }
31
+ # }
32
+ # },
33
+ # "bar": [
34
+ # 1,
35
+ # ...
36
+ # ]
37
+ # }
38
+ #
39
+ class JSONTrim
40
+
41
+ # prune the given JSON-string or ruby Hash according to
42
+ # the rules given in opts[:ignore]
43
+ #
44
+ def self.cut(obj, opts = {})
45
+ obj = JSON.parse(obj) if String === obj
46
+
47
+ (opts[:ignore] || []).each do |ignore_str|
48
+ ignoring = ignore_str.split(":")
49
+
50
+ obj = cutr(obj, ignoring)
51
+ end
52
+
53
+ js = JSON.pretty_generate(obj)
54
+ js = js.gsub('"IGN_HSH"', "{ ... }")
55
+ js = js.gsub('"IGN_ARY"', "[ ... ]")
56
+ js = js.gsub('"IGN"', "...")
57
+ end
58
+
59
+ # recursively prune the current ruby object with the given rules
60
+ # replace pruned elements with marker strings
61
+ #
62
+ # cutr( [1,2,3], ["+"] ) #=> [ 1, "IGN" ]
63
+ # cutr( {"a"=>{"b"=>3}}, ["!a"] ) #=> { "a" => "IGN_HSH" }
64
+ # cutr( {"a"=>{"b"=>3}}, ["a", "!b"]) #=> { "a" => { "b" => "IGN" }}
65
+ #
66
+ def self.cutr(data, rules)
67
+ key, *rest = rules
68
+
69
+ if key =~ /^!(.*)/
70
+ name = $1
71
+
72
+ data[name] = case data[name]
73
+ when Hash then "IGN_HSH"
74
+ when Array then "IGN_ARY"
75
+ else "IGN"
76
+ end if data[name]
77
+ elsif key =~ /^\+$/
78
+ if Array === data
79
+ data = [data.first, "IGN"]
80
+ end
81
+ elsif key =~ /^\*$/
82
+ if Array === data
83
+ data = data.map { |elem| cutr(elem, rest) }
84
+ elsif Hash === data
85
+ data = data.inject({}) do |h,(k,v)| h[k] = cutr(v, rest); h end
86
+ end
87
+ else
88
+ name = key
89
+
90
+ if data[name] and not rest.empty?
91
+ data[name] = cutr(data[name], rest)
92
+ end
93
+ end
94
+
95
+ data
96
+ end
97
+ end
@@ -0,0 +1,95 @@
1
+ require 'test_helper'
2
+
3
+ class JsontrimTest < Test::Unit::TestCase
4
+ def assert_equal_ws(expected, actual)
5
+ expected = expected.gsub(/\s/, "")
6
+ actual = actual.gsub(/\s/,"")
7
+ assert_equal expected, actual
8
+ end
9
+
10
+ should "pass json through unchanged" do
11
+ assert_equal %Q({\n "foo": "bar"\n}),
12
+ JSONTrim.cut(%q({"foo": "bar"}))
13
+ end
14
+
15
+ should "ignore" do
16
+ assert_equal({"a" => 3, "b" => "IGN" },
17
+ JSONTrim.cutr({ "a" => 3, "b" => 4 } , ["!b"]))
18
+ assert_equal({"a" => 3, "b" => "IGN_HSH" },
19
+ JSONTrim.cutr({ "a" => 3, "b" => { "c" => 4 }} , ["!b"]))
20
+ assert_equal({"a" => 3, "b" => "IGN_ARY" },
21
+ JSONTrim.cutr({ "a" => 3, "b" => [ 4 ] } , ["!b"]))
22
+ end
23
+
24
+ should "distinguish between arrays and hashes" do
25
+ assert_equal %Q({\n "foo": { ... }\n}),
26
+ JSONTrim.cut(%q({"foo" : { "bar": "fred"}}), :ignore => ["!foo"])
27
+ assert_equal %Q({\n "foo": [ ... ]\n}),
28
+ JSONTrim.cut(%q({"foo" : [ "bar", "fred"]}), :ignore => ["!foo"])
29
+ assert_equal %Q({\n "foo": ...\n}),
30
+ JSONTrim.cut(%q({"foo" : 3}), :ignore => ["!foo"])
31
+ end
32
+
33
+ should "work for nested attributes" do
34
+ assert_equal %Q({\n "foo": {\n "bar": { ... }\n }\n}),
35
+ JSONTrim.cut(%q({"foo" : { "bar": {"f":3}}}), :ignore => ["foo:!bar"])
36
+
37
+ assert_equal %Q({\n "foo": {\n "bar": ...\n }\n}),
38
+ JSONTrim.cut(%q({"foo" : { "bar": 3}}), :ignore => ["foo:!bar"])
39
+ end
40
+
41
+ should "not alter json if ignored thing does not exist" do
42
+ assert_equal %Q({\n "foo": { ... }\n}),
43
+ JSONTrim.cut(%q({"foo" : { "bar": "fred"}}),
44
+ :ignore => ["!foo", "!doesnotexist", "foo:!doesnotexist"])
45
+ end
46
+
47
+ should "allow ignoring of subelements of arrays" do
48
+ assert_equal_ws %Q({"foo":[{"bar": { ... }},{"bar": ...}]}),
49
+ JSONTrim.cut(%q({"foo" : [{ "bar": {"baz":3}}, {"bar":3}]}), :ignore => ["foo:*:!bar"])
50
+ end
51
+
52
+ should "allow ignoring of arrays" do
53
+ assert_equal %Q({\n "foo": [\n "bar",\n ...\n ]\n}),
54
+ JSONTrim.cut(%q({"foo": ["bar", "baz", "fred"]}),
55
+ :ignore => ["foo:+"])
56
+ end
57
+
58
+ should "pass a complex testcase" do
59
+ before = <<-EOF
60
+ { "foo": {
61
+ "one": { "a": 1, "b": {"c": 2} },
62
+ "two": "value",
63
+ "three": { "b": [1,2,3] }},
64
+ "bar": [1,2,3,4,5] }
65
+ EOF
66
+
67
+ blacklist = [
68
+ "foo:!two",
69
+ "foo:*:!b",
70
+ "bar:+"
71
+ ]
72
+
73
+ expected = <<-EOF
74
+ {
75
+ "foo": {
76
+ "two": ...,
77
+ "three": {
78
+ "b": [ ... ]
79
+ },
80
+ "one": {
81
+ "a": 1,
82
+ "b": { ... }
83
+ }
84
+ },
85
+ "bar": [
86
+ 1,
87
+ ...
88
+ ]
89
+ }
90
+ EOF
91
+
92
+ actual = JSONTrim.cut(before, :ignore => blacklist)
93
+ assert_equal expected.chomp, actual
94
+ end
95
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'jsontrim'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsontrim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Levin Alexander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-09 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: remove unimportant elements from a json object
36
+ email: mail@levinalex.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - jsontrim.gemspec
52
+ - lib/jsontrim.rb
53
+ - test/jsontrim_test.rb
54
+ - test/test_helper.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/levinalex/jsontrim
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: remove unimportant elements from a json object
83
+ test_files:
84
+ - test/jsontrim_test.rb
85
+ - test/test_helper.rb