Skip to content

Instantly share code, notes, and snippets.

@martinciu
Created August 12, 2015 18:19
Show Gist options
  • Save martinciu/2091571bdd6d13dcf82d to your computer and use it in GitHub Desktop.
Save martinciu/2091571bdd6d13dcf82d to your computer and use it in GitHub Desktop.
require 'rom-mapper'
require 'json'
class ProfileMapper < ROM::Mapper
reject_keys true
attribute :id, from: "userId"
unwrap "profile" do
attribute :created_at, from: "firstDate"
# attribute :metric
# attribute :locale
unwrap "localization" do
attribute :metric, from: "metric"
attribute :locale, from: "locale"
end
end
end
json = JSON.parse([
{
userId: 1,
profile: {
firstDate: '2012-12-12',
localization: {
metric: true,
locale: 'en'
}
}
}
].to_json)
puts ProfileMapper.build.call(json)
# {:id=>1, "profile"=>{:metric=>true, :locale=>"en"}, :created_at=>"2012-12-12"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment