Skip to content

Tame that wild hash to get the values you're looking for — without making your eyes bleed staring at a jumbled console!

License

Notifications You must be signed in to change notification settings

ianrandmckenzie/the_hash_whisperer

Repository files navigation

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

About

Tame that wild hash to get the values you're looking for — without making your eyes bleed staring at a jumbled console!

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages