rushi 0.0.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/.gitignore +18 -0
- data/LICENSE +20 -0
- data/README.md +44 -0
- data/lib/rushi/rushi_object.rb +70 -0
- data/lib/rushi.rb +4 -0
- data/rushi.gemspec +18 -0
- data/spec/rushi_object_spec.rb +82 -0
- data/spec/spec_helper.rb +1 -0
- metadata +104 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Haiyang (harry) Gao
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
rushi
|
2
|
+
=====
|
3
|
+
|
4
|
+
Convert javascript JSON data into a Ruby OpenStruct with ruby naming convention
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'rushi'
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install rushi
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Convert simple json:
|
20
|
+
```
|
21
|
+
os = Rushi::RushiObject.objectify('{"foo1":"bar1", "foo2":true }
|
22
|
+
os.foo1 == "bar1"
|
23
|
+
os.foo2 = true
|
24
|
+
```
|
25
|
+
Convert array:
|
26
|
+
```
|
27
|
+
array = Rushi::RushiObject.objectify('[{"foo1":"bar1"}, {"foo2":true}]')
|
28
|
+
array[0].foo1 == "bar1"
|
29
|
+
array[1].foo2 == true
|
30
|
+
```
|
31
|
+
Create OpenStruct using ruby convention
|
32
|
+
```
|
33
|
+
os = Rushi::RushiObject.objectify('{"isActive":true, "UID":"1234", "hasHTML":false }')
|
34
|
+
os.is_active == true
|
35
|
+
os.uid == "1234"
|
36
|
+
os.has_html == false
|
37
|
+
```
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'strscan'
|
4
|
+
|
5
|
+
module Rushi
|
6
|
+
class RushiObject
|
7
|
+
def self.objectify(json)
|
8
|
+
hash = JSON.parse(json)
|
9
|
+
rubify_hash(hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def self.rubify_hash(hash)
|
14
|
+
hash.kind_of?(Array) ? generate_array(hash) : generate_openstruct(hash)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.generate_array(hash)
|
18
|
+
array = Array.new
|
19
|
+
hash.each { |item| array.push(rubify_hash(item)) }
|
20
|
+
array
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.generate_openstruct(hash)
|
24
|
+
open_struct = OpenStruct.new
|
25
|
+
hash.each do |key, value|
|
26
|
+
rubified_key = rubify_key(key)
|
27
|
+
open_struct.new_ostruct_member rubified_key
|
28
|
+
if(value.is_a?(Hash))
|
29
|
+
obj_val = self.rubify_hash(value)
|
30
|
+
elsif (value.kind_of?(Array))
|
31
|
+
obj_val = generate_array(value)
|
32
|
+
else
|
33
|
+
obj_val = value
|
34
|
+
end
|
35
|
+
open_struct.send "#{rubified_key}=" , obj_val
|
36
|
+
end
|
37
|
+
open_struct
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.rubify_key(key)
|
41
|
+
s = StringScanner.new(key)
|
42
|
+
ret = ""
|
43
|
+
remaining = key
|
44
|
+
while (val = s.scan_until(/[A-Z]+/)) do
|
45
|
+
if (s.pos == 1 || s.pos - s.matched_size == 0)
|
46
|
+
ret << val.downcase
|
47
|
+
remaining = s.post_match
|
48
|
+
elsif(s.pos == key.size)
|
49
|
+
ret << val[0, val.size - s.matched.size]
|
50
|
+
ret << '_'
|
51
|
+
ret << s.matched.downcase
|
52
|
+
remaining = s.post_match
|
53
|
+
elsif(s.matched.size == 1)
|
54
|
+
ret << val[0, val.size - 1]
|
55
|
+
ret << '_'
|
56
|
+
ret << s.matched.downcase
|
57
|
+
remaining = s.post_match
|
58
|
+
else
|
59
|
+
ret << val[0, val.size - s.matched.size]
|
60
|
+
ret << '_'
|
61
|
+
ret << val[val.size - s.matched.size, s.matched.size - 1].downcase
|
62
|
+
ret << '_'
|
63
|
+
ret << val[val.size - 1 , 1].downcase
|
64
|
+
remaining = s.post_match
|
65
|
+
end
|
66
|
+
end
|
67
|
+
ret << remaining
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/rushi.rb
ADDED
data/rushi.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'rushi'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2013-12-05'
|
5
|
+
s.summary = "Convert javascript JSON data into a Ruby OpenStruct"
|
6
|
+
s.description = "Convert javascript JSON data into a Ruby OpenStruct with ruby naming convention"
|
7
|
+
s.authors = ["Harry Gao"]
|
8
|
+
s.email = 'foxgaocn@gmail.com'
|
9
|
+
s.files = [".gitignore", "LICENSE", "rushi.gemspec", "README.md","lib/rushi.rb", "lib/rushi/rushi_object.rb"]
|
10
|
+
s.homepage =
|
11
|
+
'http://rubygems.org/gems/rushi'
|
12
|
+
s.license = 'MIT'
|
13
|
+
s.test_files = ["spec/rushi_object_spec.rb", "spec/spec_helper.rb"]
|
14
|
+
|
15
|
+
s.add_development_dependency "bundler", "~> 1.3"
|
16
|
+
s.add_development_dependency "rake"
|
17
|
+
s.add_development_dependency "rspec"
|
18
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Rushi::RushiObject do
|
4
|
+
describe '#objectify' do
|
5
|
+
context 'simple hash' do
|
6
|
+
subject {Rushi::RushiObject.objectify('{"foo1":"bar2", "foo2":true }')}
|
7
|
+
it 'should return a hash' do
|
8
|
+
subject.foo1 == "bar1"
|
9
|
+
subject.foo2 == true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'nested hash' do
|
14
|
+
subject {Rushi::RushiObject.objectify('{"foo1":"bar1", "foo2":{"zoo2":2} }')}
|
15
|
+
it 'should return a nested object' do
|
16
|
+
subject.foo1 == "bar1"
|
17
|
+
subject.foo2 .zoo2 == 2
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'nested array' do
|
22
|
+
subject {Rushi::RushiObject.objectify('{"foo1":[{"bar1":"zoo1"}, {"bar2":null}]}')}
|
23
|
+
it 'should return a nested object' do
|
24
|
+
subject.foo1[0].bar1 == "zoo1"
|
25
|
+
subject.foo1[1].bar2 == nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'nested hash with nested array' do
|
30
|
+
subject {Rushi::RushiObject.objectify('{"foo1":{"foo2" :[{"bar1":"zoo1"}, {"bar2":"zoo2"}]}}')}
|
31
|
+
it 'should return a nested object' do
|
32
|
+
subject.foo1.foo2[0].bar1 == "zoo1"
|
33
|
+
subject.foo1.foo2[0].bar2 == "zoo2"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'first layer array' do
|
38
|
+
subject {Rushi::RushiObject.objectify('[{"foo1":"bar1"}, {"foo2":"bar2"}]')}
|
39
|
+
it 'should retune an array of openstruct' do
|
40
|
+
subject[0].foo1 = "bar1"
|
41
|
+
subject[1].foo2 = "bar2"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#rubify_key" do
|
47
|
+
it 'should return origin string if no uppercase characters' do
|
48
|
+
Rushi::RushiObject.send(:rubify_key, 'abcd').should == 'abcd'
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should return rubified string if first character is uppercase' do
|
52
|
+
Rushi::RushiObject.send(:rubify_key, 'Abcd').should == 'abcd'
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should return rubified string if first 3 characters are uppercase' do
|
56
|
+
Rushi::RushiObject.send(:rubify_key, 'UID').should == 'uid'
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should return rubified string if there is 1 character in middle' do
|
60
|
+
Rushi::RushiObject.send(:rubify_key, 'isUp').should == 'is_up'
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should return rubified string if there are 2 character in middle' do
|
64
|
+
Rushi::RushiObject.send(:rubify_key, 'isUPcase').should == 'is_u_pcase'
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should return rubified string if there are 3 upper case characters in middle' do
|
68
|
+
Rushi::RushiObject.send(:rubify_key, 'isXMLString').should == 'is_xml_string'
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should return rubified string if there are 3 upper case characters in end' do
|
72
|
+
Rushi::RushiObject.send(:rubify_key, 'isXML').should == 'is_xml'
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
it 'should return rubified string if there are 1 upper case characters in end' do
|
77
|
+
Rushi::RushiObject.send(:rubify_key, 'isX').should == 'is_x'
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rushi.rb'
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rushi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Harry Gao
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Convert javascript JSON data into a Ruby OpenStruct with ruby naming
|
63
|
+
convention
|
64
|
+
email: foxgaocn@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- LICENSE
|
71
|
+
- rushi.gemspec
|
72
|
+
- README.md
|
73
|
+
- lib/rushi.rb
|
74
|
+
- lib/rushi/rushi_object.rb
|
75
|
+
- spec/rushi_object_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
homepage: http://rubygems.org/gems/rushi
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.8.23
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Convert javascript JSON data into a Ruby OpenStruct
|
102
|
+
test_files:
|
103
|
+
- spec/rushi_object_spec.rb
|
104
|
+
- spec/spec_helper.rb
|