json_hash 0.0.2
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.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/lib/json_hash.rb +70 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e6eb3ab0716e36abec34ef40bc1af6752373773
|
4
|
+
data.tar.gz: 06d8d190c23d6c32034c2353e4ef2b283247e4e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec5cd1df497e0d1049f6c7035716c97c5fe3db7325e414669902e856ed4e0cb126b5ca4289ed2678d14ffd03a1101d1f539d91dfeda78f34a059507ea25dae3b
|
7
|
+
data.tar.gz: 9fe316a8c9918fb26c7c5b4239ba5138870b2577e525804c4e5bc8df19d5098e2fc0e4a8432e71d48e68b21890f279aa15d2bfd4294e25a8f37aa4074e1ff941
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Nathan Brazil
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, 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,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# json_hash
|
2
|
+
|
3
|
+
This is a simple gem that provides method-like syntatic sugar on top of a JSON object via the JSONHash class.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
If you are using Bundler, simply add the following line to your Gemfile:
|
8
|
+
|
9
|
+
gem "json_hash"
|
10
|
+
|
11
|
+
Otherwise, just type:
|
12
|
+
|
13
|
+
gem install json_hash
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
The canonical usage is to obtain some JSON text from a remote endpoint, and parse it. For example:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require "json_hash"
|
21
|
+
=> true
|
22
|
+
user = JSONHash.parse("http://example.org/users/1.json")
|
23
|
+
=> #<JSONHash:0x007fe0d320fd30 @json={"id"=>1, ...}
|
24
|
+
user.id
|
25
|
+
=> 1
|
26
|
+
users = JSONHash.parse("http://cumulus.local/identity-manager/users.json")
|
27
|
+
=> [#<JSONHash:0x007fe0d322c368 @json={"id"=>1, ...}, ...]
|
28
|
+
users.count
|
29
|
+
=> 4
|
30
|
+
users[1].id
|
31
|
+
=> 2
|
32
|
+
```
|
33
|
+
|
34
|
+
## Requirements
|
35
|
+
|
36
|
+
The json_hash gem relies on the following:
|
37
|
+
|
38
|
+
* The json gem
|
39
|
+
* The open-uri module
|
40
|
+
* The uri module
|
41
|
+
|
42
|
+
## Credits
|
43
|
+
|
44
|
+
This was inspired by a similar class I wrote in Python to parse Jenkin's RESTful API output, which made use of the
|
45
|
+
setattr() method. I wanted to see if I can do something similar in Ruby.
|
46
|
+
|
47
|
+
Alas, since Ruby doesn't have something like setattr(), I've had to rely on method_missing() instead.
|
data/lib/json_hash.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require "json"
|
2
|
+
require "open-uri"
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
##
|
6
|
+
# Provides syntactic sugar to a JSON structure.
|
7
|
+
# Given a Ruby hash h, instead of using h[key] to obtain its value, you can use the
|
8
|
+
# syntax of h.key, as if you are invoking a method.
|
9
|
+
|
10
|
+
class JSONHash
|
11
|
+
|
12
|
+
##
|
13
|
+
# Initializer. The json parameter is a Ruby hash. Usually you would call JSON.parse to get such a hash.
|
14
|
+
# @param json [Hash] A Ruby hash, usually obtained by calling JSON.parse or HTTP GET on a .json endpoint.
|
15
|
+
|
16
|
+
def initialize(json)
|
17
|
+
@json = json
|
18
|
+
@json.each do |key,value|
|
19
|
+
if value.class == Hash
|
20
|
+
@json[key] = JSONHash.new(value)
|
21
|
+
elsif value.class == Array
|
22
|
+
values = value.collect { |v| v.class == Hash ? JSONHash.new(v) : v }
|
23
|
+
@json[key] = values
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Whatever .method is invoked, it will convert that to a string and use it as the key to look it up in the hash.
|
30
|
+
# @param method [String, Symbol] The name of method to invoke.
|
31
|
+
# @param args The arguments supplied to the method.
|
32
|
+
|
33
|
+
def method_missing(method, *args)
|
34
|
+
@json[method.to_s] || super
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Returns the underling JSON parsed hash structure.
|
39
|
+
# @return [JSON] Underlying JSON object.
|
40
|
+
|
41
|
+
def to_json
|
42
|
+
@json
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# Return a string representation of the JSON parsed hash structure.
|
47
|
+
# @return [String] String representation of the underlying JSON object.
|
48
|
+
|
49
|
+
def to_s
|
50
|
+
@json.to_s
|
51
|
+
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# Build an array of JSONHash objects if the argument is an array of Hash instances. If the argument is an URI,
|
55
|
+
# then parse the URI contents with JSON.parse, and then build it. If the argument is a string, then parse it
|
56
|
+
# using JSON.parse first, then try to parse it again. Otherwise, assume it's a single Hash object and build
|
57
|
+
# just a single JSONHash object.
|
58
|
+
# @param from [Array, Hash, String, URI] Object to build from.
|
59
|
+
# @return [Array, JSONHash] Either an array of JSONHash objects or a single JSONHash object.
|
60
|
+
|
61
|
+
def self.parse(from)
|
62
|
+
return JSONHash.new(from) if from.class == Hash
|
63
|
+
return from.collect { |item| parse(item) } if from.class == Array
|
64
|
+
return parse(JSON.parse(open(from) { |fp| fp.read })) if from.is_a? URI::Generic
|
65
|
+
# Assume from is String from here on
|
66
|
+
return parse(URI.parse(from)) if /^http[s]?:\/\/.+/ =~ from
|
67
|
+
return parse(JSON.parse(from))
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Brazil
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-31 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.8'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.8.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.8'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.8.2
|
33
|
+
description: A simple gem that adds method-like syntactic sugar to a JSON hash.
|
34
|
+
email: nb@bitaxis.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- lib/json_hash.rb
|
42
|
+
homepage: https://github.com/bitaxis/json_hash.git
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.4.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Hola!
|
66
|
+
test_files: []
|
67
|
+
has_rdoc:
|