So I needed to parse the log files for SlimTimer this weekend to correct a data loss issue due to a small issue with implementing https for subscribers. The issue required looking for requests that had returned something other than “200 OK” collecting the parameters and entering any data that had got lost. After a bit of messing around it occured to me the the format of the parameters string in the Rail’s logs was not a million miles from the JSON format, so I came up with this to turn the string into a useable hash:

params =~ /.*: Parameters: (\{.*\})$/
str_hash = JSON.parse(params.gsub('=>',':'))

This provides a hash of the parameters used in the request. Of course the keys here are strings so to convert to symbols we can then use:

def create_symbol_hash(input)
  ret = input
  if input.is_a? Hash
    ret = {}
    input.each do |k, v|
      ret[k.to_sym] = create_symbol_hash(v)
    end
    ret
  else
    ret
  end
end

and simply pass in the output from the JSON library. Seemed quite neat to me anyway.

Sorry, the comment form is closed at this time.