ruby - 如何在 Grape 参数中将 camelcase 更改为 snakecase

标签 ruby ruby-grape

在我的 Grape 端点中,我有一个名为 user_type 的必需参数。另一方面,我的公共(public) API 将此参数声明为 userType,即驼峰式大小写。有办法改变这个吗?

 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

最佳答案

这有点难看,但它可以解决问题,因此您可以将它放入方法中。

def sym_to_lower_camel(sym)
  sym.to_s.split('_').map{|x| x.capitalize}.join.sub(/^./, sym.to_s[0].downcase)
end

关于ruby - 如何在 Grape 参数中将 camelcase 更改为 snakecase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35436822/

相关文章:

ruby - 将用户从 Active Directory 加载到 Rails 3.1 Active Record 数据库

ruby-on-rails - 为什么到处都是卡住的常量?

ruby - GulpJS : I don't want gulp-ruby-sass to loop through all sass files

arrays - Rspec 将空数组解释为缺少 Grape 参数

ruby - 如何使用 Ruby 和 Rack 为 API 设置 redis?

ruby-on-rails - 为 Grape API 添加路径/自动加载路径不起作用

c++ - 如果用户使用 Rice 在 Ruby 中重新定义 initialize(),则避免 C++ 代码中的段错误

ruby-on-rails - 如何使用 Grape 和 MongoDB 在 Ruby on Rails 中正确测试 'post'(创建)方法

ruby-on-rails - 如何在没有 grape-api 的情况下使用 grape-entity?

ruby - 获取构造函数的参数名称