json_wrapper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/json_wrapper/version.rb +5 -0
- data/lib/json_wrapper.rb +130 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d43f0be083a577ad94ad7ccb8ac96134815bea10
|
4
|
+
data.tar.gz: 921f96757d7bd313ec828de307cffc6bdf686a3f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 991ad5d73f2822c632b9dacaf99a7438535b5a298cfa07a8244097a3a791ead8152446dbbf00cff97b2c7cf540debd4b0abb426b086b6852ac5c7b2633aa1653
|
7
|
+
data.tar.gz: a033bdd139bc625476cd7198b8d43a1cabc0f990b4330150a87d6ad625322d9898d96d49a004e4fca9aa4a5b25b91a59a859d2d98629313d3512bb39585271f7
|
data/lib/json_wrapper.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
class JsonWrapper
|
4
|
+
autoload :VERSION, 'json_wrapper/version'
|
5
|
+
# Internal value
|
6
|
+
# @return [Hash, Array, String, Number, Nil] internal value
|
7
|
+
attr_reader :value
|
8
|
+
|
9
|
+
# Create a JsonWrapper
|
10
|
+
# @param value [Hash, Array, String, Numeric, Nil] parse result from JSON.parse
|
11
|
+
# @return [JsonWrapper] instance
|
12
|
+
def initialize(value = nil)
|
13
|
+
@value = value
|
14
|
+
end
|
15
|
+
|
16
|
+
# If value is a Hash
|
17
|
+
# @return [True, False] if value is a Hash
|
18
|
+
def hash?
|
19
|
+
@value.kind_of? Hash
|
20
|
+
end
|
21
|
+
|
22
|
+
# If value is a Array
|
23
|
+
# @return [True, False] if value is an Array
|
24
|
+
def array?
|
25
|
+
@value.kind_of? Array
|
26
|
+
end
|
27
|
+
|
28
|
+
# If value is String
|
29
|
+
# @return [True,False] if value is a String
|
30
|
+
def string?
|
31
|
+
@value.kind_of? String
|
32
|
+
end
|
33
|
+
|
34
|
+
# If value is a Number
|
35
|
+
# @return [True,False] if value is an Number
|
36
|
+
def number?
|
37
|
+
@value.kind_of? Numeric
|
38
|
+
end
|
39
|
+
|
40
|
+
# If value is a Nil
|
41
|
+
# @return [True, False] if value is a Nil
|
42
|
+
def null?
|
43
|
+
@value.nil?
|
44
|
+
end
|
45
|
+
|
46
|
+
# Get the value if is a Hash
|
47
|
+
# @return [Hash, nil] value if it's a Hash
|
48
|
+
def hash
|
49
|
+
@value if hash?
|
50
|
+
end
|
51
|
+
|
52
|
+
# Get the value if is a Array
|
53
|
+
# @return [Array, Nil] value if it's a Array
|
54
|
+
def array
|
55
|
+
@value if array?
|
56
|
+
end
|
57
|
+
|
58
|
+
# Get the value if value is string
|
59
|
+
# @return [String, Nil] value if it's a String
|
60
|
+
def string
|
61
|
+
@value if string?
|
62
|
+
end
|
63
|
+
|
64
|
+
# Get the value if value is a number
|
65
|
+
# @return [Fixnum, Float, Nil] value if it's a Numeric, typically Fixnum or Float
|
66
|
+
def number
|
67
|
+
@value if number?
|
68
|
+
end
|
69
|
+
|
70
|
+
# Force convert to Hash
|
71
|
+
# @return [Hash] Hash format of value, {} if not capable
|
72
|
+
def hash!
|
73
|
+
hash || {}
|
74
|
+
end
|
75
|
+
|
76
|
+
# Force convert to Array
|
77
|
+
# @return [Array] Array format of value, {} if not capable
|
78
|
+
def array!
|
79
|
+
array || []
|
80
|
+
end
|
81
|
+
|
82
|
+
# Force convert to String
|
83
|
+
# @return [String] String format of value, "" if not capable
|
84
|
+
def string!
|
85
|
+
return @value if string?
|
86
|
+
return @value.to_s if number?
|
87
|
+
""
|
88
|
+
end
|
89
|
+
|
90
|
+
# Force convert to number
|
91
|
+
# @return [Numeric] Number format of value, 0 if not capable
|
92
|
+
def number!
|
93
|
+
return @value if number?
|
94
|
+
return @value.to_f if string?
|
95
|
+
0
|
96
|
+
end
|
97
|
+
|
98
|
+
# Force convert to Float
|
99
|
+
# @return [Float] Float format of value
|
100
|
+
def float!
|
101
|
+
number!.to_f
|
102
|
+
end
|
103
|
+
|
104
|
+
# Force convert to Fixnum
|
105
|
+
# @return [Fixnum] Fixnum format of value
|
106
|
+
def fixnum!
|
107
|
+
number!.to_i
|
108
|
+
end
|
109
|
+
|
110
|
+
# Try to get value from Hash or Array
|
111
|
+
# @param key [String, Symbol, Fixnum] key
|
112
|
+
# @return [JsonWrapper] JsonWrapper of the value
|
113
|
+
def get(key)
|
114
|
+
return JsonWrapper.new(@value[key]) if array? and key.kind_of?(Fixnum)
|
115
|
+
return JsonWrapper.new(@value[key]) if hash? and key.kind_of?(String)
|
116
|
+
return JsonWrapper.new(@value[key.to_s]) if hash? and key.kind_of?(Symbol)
|
117
|
+
JsonWrapper.new
|
118
|
+
end
|
119
|
+
|
120
|
+
# @see #get
|
121
|
+
def [](key)
|
122
|
+
get(key)
|
123
|
+
end
|
124
|
+
|
125
|
+
# @see #get
|
126
|
+
def method_missing(*args)
|
127
|
+
get args.first
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Guo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
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-core
|
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: SwiftyJSON inspired JSON lib for Ruby, very handy for JSON access while
|
42
|
+
incoming JSON structure is not definite.
|
43
|
+
email: ryan@islandzero.net
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/json_wrapper/version.rb
|
49
|
+
- lib/json_wrapper.rb
|
50
|
+
homepage: http://rubygems.org/gems/json_wrapper
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.0.14
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: SwiftyJSON inspired JSON lib for Ruby.
|
74
|
+
test_files: []
|
75
|
+
has_rdoc:
|