Sha256: df588bc4644bed93b9ff92f64b202ff529a6e20c855c336896e261ed420b0237

Contents?: true

Size: 620 Bytes

Versions: 1

Compression:

Stored size: 620 Bytes

Contents

# Add a formatter to grape that converts all snake case hash keys from ruby to camel case.
require 'camel_snake_keys'
require 'grape/formatter/json'
module IntrospectiveGrape
  module Formatter
    module CamelJson
      def self.call(object, _env)
        if object.respond_to?(:to_json) && !object.respond_to?(:with_camel_keys) &&
          (parsed_object = JSON.parse(object.to_json)).respond_to?(:with_camel_keys)
          object = parsed_object
        end
        object = object.with_camel_keys if object.respond_to?(:with_camel_keys)
        Grape::Formatter::Json.call(object, _env)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
introspective_grape-0.3.1 lib/introspective_grape/formatter/camel_json.rb