2

In my Grape endpoint I have a required param called user_type. On the other hand, my public API declares this param as userType, i.e. camel case. Is there a way to changes this?

 module MyAPI
      module V1
        class SignUp < Grape::API
          desc 'Create new user account'
          params do
           requires :email, type: String
           requires :password, type: String
           requires :user_type, type: String
          end
          post :sign_up do
            {}
          end
        end
      end
    end
  • FYI, user_type is referred to as snake case; userType is referred to as lower camel case. – bigtunacan Feb 16 '16 at 16:58
  • @bigtunacan thanks, I mixed it up. –  Feb 16 '16 at 17:01
  • Checkout this [Blog Post](https://github.com/jrhe/grape-active_model_serializers) for a method that very nicely converts lowerCamelCase to snake_case. This way you can leave your public API untouched and handle the key conversion in the application. – engineersmnky Feb 16 '16 at 17:17

1 Answers1

0

This is a bit ugly, but it will do the trick so you could just pop it into a method.

def sym_to_lower_camel(sym)
  sym.to_s.split('_').map{|x| x.capitalize}.join.sub(/^./, sym.to_s[0].downcase)
end
bigtunacan
  • 4,873
  • 8
  • 40
  • 73