Module: IntrospectiveGrape::Formatter::CamelJson

Defined in:
lib/introspective_grape/formatter/camel_json.rb

Class Method Summary collapse

Class Method Details

.call(object, env) ⇒ Object



18
19
20
# File 'lib/introspective_grape/formatter/camel_json.rb', line 18

def call(object, env)
  Grape::Formatter::Json.call(transform_to_camel_keys(object), env)
end

.transform_to_camel_keys(object) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/introspective_grape/formatter/camel_json.rb', line 8

def transform_to_camel_keys(object)
  # We only need to parse(object.to_json) like this if it isn't already
  # a native hash (or array of them), i.e. we have to parse Grape::Entities
  # and other formatter facades:
  unless (object.is_a?(Array) && object.first.is_a?(Hash)) || object.is_a?(Hash)
    object = JSON.parse(object.to_json) if object.respond_to?(:to_json)
  end
  CamelSnakeKeys.camel_keys(object)
end