Code Monkey home page Code Monkey logo

the_hash_whisperer's Introduction

The Hash Whisperer

The Hash Whisperer figures out where it needs to go by itself. Traversing through arrays, nested hashes, and a bunch of other variables without you needing to travel with it. If you want to give it a map (aka explicit instructions where to drill into), great! Otherwise, just tell it what you're looking for by providing a single key.

Navigate this README

Installation

Add this line to your application’s Gemfile:

gem "the_hash_whisperer"

Or, for non-framework projects, simply type this into your terminal:

gem install the_hash_whisperer

Usage

See the example data near the bottom of this README for reference to the objects the code examples are using.

Get all values identified by a specified a key

# Ruby Hash
hash_whisperer = TheHashWhisperer.new(your_hash)
values = hash_whisperer.find_all_values_for('title')

# JSON
json_whisperer = TheHashWhisperer.new(your_valid_json, true)
values ||= json_whisperer.find_all_values_for('title')

values
# => ["Example Article Title 1", "Example Article Title 2", "Example Comment Title 1"]

Get all values specified by a direct path to that key

# Ruby Hash
hash_whisperer = TheHashWhisperer.new(your_hash)
values = hash_whisperer.drill_into_and_find('data.articles.comments.title')

# JSON
json_whisperer = TheHashWhisperer.new(your_valid_json, true)
values ||= json_whisperer.drill_into_and_find('data.articles.comments.title')

values
# => ["Example Comment Title 1"]

Yes, it works for the shallower paths, too

# Ruby Hash
hash_whisperer = TheHashWhisperer.new(your_hash)
values = hash_whisperer.drill_into_and_find('data.articles.title')

# JSON
json_whisperer = TheHashWhisperer.new(your_valid_json, true)
values ||= json_whisperer.drill_into_and_find('data.articles.title')

values
# => ["Example Article Title 1", "Example Article Title 2"]

Object Examples

Ruby Hash

{
  data: {
    articles: [
      {
        title: 'Example Article Title 1',
        body: 'This is a stub.',
        comments: [
          {
            title: 'Example Article Comment Title 1',
            body: 'Why are all your articles just stubs?'
          }
        ]
      },
      {
        title: 'Example Article Title 2',
        body: 'This is a stub.',
        comments: []
      }
    ]
  }
}

JSON

{
  "data": {
    "articles": [
      {
        "title": "Example Article Title 1",
        "body": "This is a stub.",
        "comments": [
          {
            "title": "Example Article Comment Title 1",
            "body": "Why are all your articles just stubs?"
          }
        ]
      },
      {
        "title": "Example Article Title 2",
        "body": "This is a stub.",
        "comments": []
      }
    ]
  }
}

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development and testing:

git clone https://github.com/ianrandmckenzie/the_hash_whisperer.git
cd the_hash_whisperer
bundle install
bundle exec rake test

For security issues, send an email to the address on this page.

Alternative Gems

Hashie is a robust library for managing hashes, including features offered by the_hash_whisperer called Deep Fetch: https://github.com/hashie/hashie#deepfetch

the_hash_whisperer's People

Contributors

ianrandmckenzie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

the_hash_whisperer's Issues

TypeError from valid Ruby hash

I am getting an error from JSON String:

"{\n  \"code\": 200,\n  \"result\": [\n    {\n      \"id\": 307633301,\n      \"external_id\": \"645b0ca743a257\",\n      \"name\": \"Zephyr\",\n      \"variants\": 8,\n      \"synced\": 8,\n      \"thumbnail_url\": \"https://files.cdn.printful.com/files/d38/d38ef3c02cbb716dbc0f1ba453bfc03e_preview.png\",\n      \"is_ignored\": false\n    }\n  ],\n  \"extra\": [],\n  \"paging\": {\n    \"total\": 1,\n    \"offset\": 0,\n    \"limit\": 20\n  }\n}"

Note that JSON.parse(json_string) returns a valid Ruby hash:

valid_ruby_hash = JSON.parse(json_string)
{"code"=>200,                                          
 "result"=>                                            
  [{"id"=>307633301,                                   
    "external_id"=>"645b0ca743a257",                   
    "name"=>"Zephyr",
    "variants"=>8,
    "synced"=>8,
    "thumbnail_url"=>"https://files.cdn.printful.com/files/d38/d38ef3c02cbb716dbc0f1ba453bfc03e_preview.png",
    "is_ignored"=>false}],
 "extra"=>[],
 "paging"=>{"total"=>1, "offset"=>0, "limit"=>20}}

Using Hash Whisperer:

values = hash_whisperer.drill_into_and_find(valid_ruby_hash)

=> /the_hash_whisperer-0.0.0/lib/the_hash_whisperer.rb:42:in '[]': no implicit conversion of String into Integer (TypeError)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.