jsonpatcher 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: a48cdf6310404c3930e690d86ee1c45759d438f2
4
+ data.tar.gz: dd34539e34b19d8f9f16259a899741fa84169af6
5
+ SHA512:
6
+ metadata.gz: 6ff921ec737ab26b3517a441de66b9fd3e20d8984d1caa2d6bef7e7519468ba8e9da2733f87fcb91d66463b383a436014c1952ca46f75547772a9b27965ec61b
7
+ data.tar.gz: 80b8f3a0eff387b6a52b4ba73f181f7fdeca603586abc7496a5307e983d2a2f426add04dea4ce369d6a4384ca6dbd2c2927756131a885fd78191aa0f45fb9d86
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsonpatcher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Shinpei Maruyama
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # JSONPatcher
2
+
3
+ jsonpatcher provides a DSL to patch semantically wrong JSON
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jsonpatcher'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jsonpatcher
18
+
19
+ ## Usage
20
+ require 'jsonpatcher'
21
+
22
+ # patch array
23
+ bad_json = '["1","2","3"]' # (elements should be Number)
24
+ patcher = JSONPatcher.new {
25
+ array ->(element){ element.to_i }
26
+ }
27
+ p patcher.patch(bad_json) #=> [1, 2, 3]
28
+
29
+ # patch object
30
+ bad_json = '{"name":"shinpei","age":"28"}' # (age should be Number)
31
+ patcher = JSONPatcher.new {
32
+ object {
33
+ property :age, ->(v){ v.to_i }
34
+ }
35
+ }
36
+ p patcher.patch(bad_json) # => '{"name":"shinpei","age":28}'
37
+
38
+ # patch array of objects
39
+ bad_json = '[{"name":"shinpei","age":"28"},{"name":"kosuge","age":"22"}]'
40
+ patcher = JSONPatcher.new {
41
+ array {
42
+ object {
43
+ property :age, ->(v){ v.to_i }
44
+ }
45
+ }
46
+ }
47
+ p patcher.patch(bad_json) # => '[{"name":"shinpei","age":28},{"name":"kosuge","age":22}]'
48
+
49
+ # patch nested object
50
+ bad_json = '{"following":[{"name":"shinpei","age":"28"},{"name":"kosuge","age":"22"}]}'
51
+ patcher = JSONPatcher.new {
52
+ object {
53
+ property (:following) {
54
+ array {
55
+ object {
56
+ property :age, ->(v){ v.to_i }
57
+ }
58
+ }
59
+ }
60
+ }
61
+ }
62
+ p patcher.patch(bad_json) # => '{"following":[{"name":"shinpei","age":28},{"name":"kosuge","age":22}]}'
63
+
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jsonpatcher/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "jsonpatcher"
8
+ gem.version = JSONPatcher::VERSION
9
+ gem.authors = ["Shinpei Maruyama"]
10
+ gem.email = ["shinpeim@gmail.com"]
11
+ gem.description = "patches semantically wrong json"
12
+ gem.summary = "jsopatcher provides DSL to patch semantically wrong json"
13
+ gem.homepage = "https://github.com/Shinpeim/jsonpatcher"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "json", "~> 1.7.7"
21
+ gem.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,17 @@
1
+ require "jsonpatcher/version"
2
+ require "jsonpatcher/dsl"
3
+ require "jsonpatcher/object_patcher"
4
+ require "jsonpatcher/array_patcher"
5
+ require "jsonpatcher/element_patcher"
6
+ require "json"
7
+ class JSONPatcher
8
+ def initialize(&block)
9
+ @patcher = DSL.parse &block
10
+ end
11
+
12
+ def patch(json)
13
+ item = JSON.parse(json)
14
+ patched = @patcher.call(item)
15
+ patched.to_json
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ class JSONPatcher
2
+ class ArrayPatcher
3
+ def initialize(callable)
4
+ @patcher = callable
5
+ end
6
+
7
+ def call(array)
8
+ array.map{ |e| @patcher.call(e) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ class JSONPatcher
2
+ class DSL
3
+ def self.parse(&block)
4
+ parser = new
5
+ parser.instance_eval &block
6
+ end
7
+
8
+ def object(&block)
9
+ ObjectPatcher.new(ObjectDSL.parse &block)
10
+ end
11
+
12
+ def array(callable = nil, &block)
13
+ if block_given?
14
+ callable = ArrayDSL.parse &block
15
+ else
16
+ callable
17
+ end
18
+ ArrayPatcher.new(callable)
19
+ end
20
+
21
+ class ArrayDSL < self
22
+ end
23
+
24
+ class ObjectDSL < self
25
+ def self.parse(&block)
26
+ parser = new
27
+ parser.instance_eval &block
28
+ parser.instance_variable_get(:"@patcher")
29
+ end
30
+
31
+ def property(key, callable = nil, &block)
32
+ if block_given?
33
+ callable = DSL.parse(&block)
34
+ end
35
+ @patcher ||= {}
36
+ @patcher[key] = callable
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ class JSONPatcher
2
+ class ElementPatcher
3
+ def initialize(&block)
4
+ @patcher = block
5
+ end
6
+
7
+ def patch(element)
8
+ @patcher.call(element)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ class JSONPatcher
2
+ class ObjectPatcher
3
+ def initialize(callables)
4
+ @patcher = callables
5
+ end
6
+
7
+ def call(hash)
8
+ patched = {}
9
+ hash.each do |k, v|
10
+ patcher = @patcher[k.to_sym]
11
+ if patcher
12
+ patched[k] = patcher.call(v)
13
+ else
14
+ patched[k] = v
15
+ end
16
+ end
17
+ patched
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ class JSONPatcher
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe JSONPatcher do
4
+ context "when simple array JSON is given" do
5
+ let (:patcher) do
6
+ JSONPatcher.new {
7
+ array ->(element){ element.to_i }
8
+ }
9
+ end
10
+ describe "#patch" do
11
+ subject { patcher.patch('["1","2","3"]') }
12
+ it "should be array of integers" do should == '[1,2,3]' end
13
+ end
14
+ end
15
+
16
+ context "when simple object JSON is given" do
17
+ let (:patcher) do
18
+ JSONPatcher.new {
19
+ object {
20
+ property :age, ->(v){ v.to_i }
21
+ }
22
+ }
23
+ end
24
+ describe "#patch" do
25
+ subject { patcher.patch('{"name":"shinpei","age":"28"}') }
26
+ it {should == '{"name":"shinpei","age":28}'}
27
+ end
28
+ end
29
+
30
+ context "when nested array JOSN is given" do
31
+ let (:patcher) do
32
+ JSONPatcher.new {
33
+ array {
34
+ object {
35
+ property :age, ->(v){ v.to_i }
36
+ }
37
+ }
38
+ }
39
+ end
40
+ describe "#patch" do
41
+ subject {patcher.patch('[{"name":"shinpei","age":"28"},{"name":"kosuge","age":"22"}]')}
42
+ it {should == '[{"name":"shinpei","age":28},{"name":"kosuge","age":22}]'}
43
+ end
44
+ end
45
+
46
+ context "when nested object JSON is given" do
47
+ let (:patcher) do
48
+ JSONPatcher.new {
49
+ object {
50
+ property (:following) {
51
+ array {
52
+ object {
53
+ property :age, ->(v){ v.to_i }
54
+ }
55
+ }
56
+ }
57
+ }
58
+ }
59
+ end
60
+ describe "#patch" do
61
+ subject {patcher.patch('{"following":[{"name":"shinpei","age":"28"},{"name":"kosuge","age":"22"}]}')}
62
+ it {should == '{"following":[{"name":"shinpei","age":28},{"name":"kosuge","age":22}]}'}
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require 'jsonpatcher'
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonpatcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shinpei Maruyama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.7.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.7.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: patches semantically wrong json
42
+ email:
43
+ - shinpeim@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - jsonpatcher.gemspec
54
+ - lib/jsonpatcher.rb
55
+ - lib/jsonpatcher/array_patcher.rb
56
+ - lib/jsonpatcher/dsl.rb
57
+ - lib/jsonpatcher/element_patcher.rb
58
+ - lib/jsonpatcher/object_patcher.rb
59
+ - lib/jsonpatcher/version.rb
60
+ - spec/jsonpatcher-spec.rb
61
+ - spec/spec_helper.rb
62
+ homepage: https://github.com/Shinpeim/jsonpatcher
63
+ licenses: []
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.0
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: jsopatcher provides DSL to patch semantically wrong json
85
+ test_files:
86
+ - spec/jsonpatcher-spec.rb
87
+ - spec/spec_helper.rb
88
+ has_rdoc: