Skip to content

Instantly share code, notes, and snippets.

@epitron
Last active August 29, 2015 14:23
Show Gist options
  • Save epitron/9e63864d6fbb6c63652e to your computer and use it in GitHub Desktop.
Save epitron/9e63864d6fbb6c63652e to your computer and use it in GitHub Desktop.
require 'epitools'
class Hash
def to_ostructs
deeply_ostructed_hash = map_values do |value|
case value
when Hash
value.to_ostructs
when Array
value.map do |e|
case e
when Hash
e.to_ostructs
else
e
end
end
else
value
end
end
OpenStruct.new(deeply_ostructed_hash)
end
end
hash = {"startedDateTime"=>"2015-06-23T04:10:34.718Z",
"time"=>0.8050000033108518,
"request"=>
{"method"=>"GET",
"url"=>"https://maps.gstatic.com/favicon4.ico",
"httpVersion"=>"unknown",
"headers"=>
[{"name"=>"Referer", "value"=>"https://www.google.ca/"},
{"name"=>"User-Agent",
"value"=>
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.125 Safari/537.36"}],
"queryString"=>[],
"cookies"=>[],
"headersSize"=>-1,
"bodySize"=>0},
"response"=>
{"status"=>200,
"statusText"=>"OK",
"httpVersion"=>"unknown",
"headers"=>
[{"name"=>"date", "value"=>"Tue, 23 Jun 2015 04:06:25 GMT"},
{"name"=>"content-encoding", "value"=>"gzip"},
{"name"=>"x-content-type-options", "value"=>"nosniff"},
{"name"=>"last-modified", "value"=>"Wed, 14 Jan 2015 23:17:52 GMT"},
{"name"=>"alternate-protocol", "value"=>"443:quic,p=1"},
{"name"=>"vary", "value"=>"Accept-Encoding"},
{"name"=>"content-type", "value"=>"image/x-icon"},
{"name"=>"status", "value"=>"200"},
{"name"=>"cache-control", "value"=>"private, max-age=31536000"},
{"name"=>"content-length", "value"=>"1697"},
{"name"=>"x-xss-protection", "value"=>"1; mode=block"},
{"name"=>"server", "value"=>"sffe"},
{"name"=>"expires", "value"=>"Tue, 23 Jun 2015 04:06:25 GMT"}],
"cookies"=>[],
"content"=>{"size"=>1890, "mimeType"=>"image/x-icon"},
"redirectURL"=>"",
"headersSize"=>-1,
"bodySize"=>0,
"_transferSize"=>0},
"cache"=>{},
"timings"=>
{"blocked"=>0.240000008489005,
"dns"=>-1,
"connect"=>-1,
"send"=>0,
"wait"=>0.20499998936429603,
"receive"=>0.36000000545755073,
"ssl"=>-1}}
if __FILE__ == $0
os = hash.to_ostructs
pp os
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment