Skip to content

Instantly share code, notes, and snippets.

@kevindayton
Created June 22, 2022 13:22
Show Gist options
  • Save kevindayton/0387dfedf04a92d7010d0b8f04677d6b to your computer and use it in GitHub Desktop.
Save kevindayton/0387dfedf04a92d7010d0b8f04677d6b to your computer and use it in GitHub Desktop.
JsonWithRootFormat (ARes customer encoder/decoder)
module ActiveResource
module Formats
module JsonWithRootFormat
extend self
def extension
"json"
end
def mime_type
"application/json"
end
def encode(hash, options={})
hash.to_json(options)
end
def decode(json)
decoded = ActiveSupport::JSON.decode(json)
if json['data']
decoded = decoded['data'].map do | d |
d.transform_keys do |k|
k.to_s.underscore
end
end
else
decoded = decoded.transform_keys do |k|
k.to_s.underscore
end
end
decoded
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment