First-time poster here...
Scenario
I'm looking into some existing logs being spat out by our Ruby on Rails applications and I am struggling to motivate for rocket notation, so I've come asking the community for some suggestions.
Here's an example of a log that one might come across in our ElasticSearch database (let's not get into that topic):
# reduced for the sake of this example
{
message: "Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms",
schema: "http",
library: "Foo::Bar",
request: {
:path=>"/api/v1/pictures",
:query=>nil
}
}
At first, I figured it was harmless, but then I started pairing with a wider group of developers (I work in a Platform Team) and I found that it wasn't immediately obvious how one could write a query on multiple key/value pairs when the request portion of the log was in rocket notation (Not looking for suggestions on this, it's just the scenario).
Question
So, my questions are:
Is there a way to efficiently convert an entire Hash object into JSON-like symbol notation and purge all rockets (
=>
) from the planet?[OPTIONAL] Should I be loving rocket notation more or are we in agreement that it should die? (RIP Ruby Hash Rocket Syntax)
What I've tried
It seems to_json
will give me what I want, but I am concerned that some vendors/ingestors might not enjoy the backslashes (\
) if I go for a more generic solution in my Rails application:
pry(main)> {
message: "Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms",
schema: "http",
library: "Foo::Bar",
request: {
:path=>"/api/v1/pictures",
:query=>nil
}
}.to_json
=> "{\"message\":\"Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms\",\"schema\":\"http\",\"library\":\"Foo::Bar\",\"request\":{\"path\":\"/api/v1/pictures\",\"query\":null}}"
Expected Output
pry(main)> example_log = {
message: "Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms",
schema: "http",
library: "Foo::Bar",
request: {
:path=>"/api/v1/pictures",
:query=>nil
}
};
pry(main)> pp example_log.deep_purge_rockets;
=> {
message: "Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms",
schema: "http",
library:"Foo::Bar",
request: {
path: "/api/v1/pictures",
query: ni
}
}