Project

mishmash

0.0
No commit activity in last 3 years
No release in over 3 years
Given an input hash, output a translated hash with a given schema. Mishmash.new({a:1}).translate({a:b}) = {b:1}
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0
~> 3.2
 Project Readme

Mishmash

A ruby class for translating complex hashes to an alternate schema.

Features

  • rename keys in a nested hash
  • condense an array of hash values to an array
  • return a hash from an array of hashes based on conditional attributes
  • return an array of hashes filtered by keys

What? Why?

How do you translate an API response - or multiple responses from different APIs - to a single database schema?

Mishmash.

Methods

hash = Mishmash.new({ "a" => 1 })

Mishmash new accepts and returns an input hash.

hash.translate({ "a" = "b" })

An instance of Mishmash accepts a schema and returns an translated output.

{ "b" => 1 }

Examples

  • rename keys in a nested hash
> Mishmash.new({ "hash"=> {"a" => 1} }).translate({ "hash" =>{ "a" => "b" }})
=> {"hash" => {"b" => 1}}
  • condense an array of hash values to an array
> Mishmash.new({
       "array" => [
         {"key" => "value 1"}, 
         {"key" => "value 2"}
         ]
 }).translate({
   "array" => {"key" => "array_of_values"}
 })

=> { "array_of_values" => ["value 1", "value 2"] }
  • return a hash from an array of hashes based on a condition
> Mishmash.new({
      "array" => [
        {"option"=> "1", "value" => "X"},
        {"option"=> "2", "value" => "Y"}
      ]
    }
  }).translate({"array" => {"option" => "1", "value" => "key"}})

=> {"key" => "X"}
  • return an array of hashes filtered by keys
> Mishmash.new({
      "array" => [
        {"option"=> "1", "value" => "X"},
        {"option"=> "2", "value" => "Y"}
      ]
    }
  }).translate({"array" => ["new_array", {"option" => "1", "value" => "key"}]})

=> {"new_array" => [{"key" => "X"}, {"key" => "y"}]}

Inspection

Mishmash .translate() returns an Mishmash instance, allowing for additional translation. Useful for inspecting and navigating a response to determine and appropriate response.