Project

json_hash

0.0
No commit activity in last 3 years
No release in over 3 years
A simple gem that adds method-like syntactic sugar to a JSON hash.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 3.5.1, ~> 3.5
>= 0.8.7.6, ~> 0.8.7

Runtime

>= 1.8.2, ~> 1.8
 Project Readme

json_hash

This is a simple gem that provides method-like syntatic sugar on top of a JSON object via the JSONHash class.

Installation

If you are using Bundler, simply add the following line to your Gemfile:

gem "json_hash"

Otherwise, just type:

gem install json_hash

Usage

The canonical usage is to obtain some JSON text from a remote endpoint, and parse it. For example:

require "json_hash"
 => true
user = JSONHash.parse("http://example.org/users/1.json")
 => #<JSONHash:0x007fe0d320fd30 @json={"id"=>1, ...}
user.id
 => 1 
user.login
 => "john_smith" 
users = JSONHash.parse("http://example.org/users.json")
 => [#<JSONHash:0x007fe0d322c368 @json={"id"=>1, ...}, ...]
users.count
 => 4 
users[1].login
 => "jane_smith" 

Requirements

The json_hash gem relies on the following:

  • The json gem
  • The open-uri module
  • The uri module

Credits

This was inspired by a similar class I wrote in Python to parse Jenkin's RESTful API output, which made use of the setattr() method. I wanted to see if I can do something similar in Ruby.

Alas, since Ruby doesn't have something like setattr(), I've had to rely on method_missing() instead.