SnakeEyes
Automatically convert between camel case APIs to snake case for your Rails code
Installation
Add this line to your application's Gemfile:
gem 'snake-eyes'
And then execute:
$ bundle
Usage
To use SnakeEyes, simply add the following to the top of any controller in which you wish to have snake case parameters. All controllers that inherit from it shall also have the behaviour
class JsonController < ApplicationController
snake_eyes_params
def show
#reference the params method as normal
end
end
Dealing with nested params
Once snake_eyes_params
has been enabled for a controller, params
accepts an options hash, which can be used to specify which attributes should have the _attributes
suffix appended.
class WithoutSnakeEyesController < ApplicationController
def show
params # results in:
# {
# 'user' => {
# 'name' => 'John Smith',
# 'favouriteColor' => 'blue',
# 'address' => { line1: '123 street' },
# 'billingAddress' => { line1: '456 road' }
# }
#}
end
end
class WithSnakeEyesController < ApplicationController
snake_eyes_params
def show
params(nested_attributes: { user: [ :address, :billing_address ] }) # results in:
# {
# 'user_attributes' => {
# 'name' => 'John Smith',
# 'favourite_color' => 'blue',
# 'address_attributes' => { line1: '123 street' },
# 'billing_address_attributes' => { line1: '456 road' }
# }
#}
end
end
Avoid _attributes suffix on parents: the _ prefix
To specify nested objects that should not have the _attributes
suffix (but contain attributes that should), you can prefix them with an underscore:
class WithSnakeEyesController < ApplicationController
snake_eyes_params
def show
params(nested_attributes: { _user: [ :address, :billing_address ] }) # results in:
# {
# 'user' => {
# 'name' => 'John Smith',
# 'favourite_color' => 'blue',
# 'address_attributes' => { 'line1: 123 street' },
# 'billing_address_attributes' => { line1: '456 road' }
# }
#}
end
end
Reference any element of an array: the '*' wildcard
To apply the _attributes
suffix to all elements of an array, use the '*'
wildcard in place of the array index:
class WithSnakeEyesController < ApplicationController
snake_eyes_params
def show
# Given
params(nested_attributes: [ _array: { '*' => :string } ])
# If the params are:
#
# 'array' => [
# { 'string' => 'string' },
# { 'string' => 'string2' },
# ]
#
# What will be returned:
#
# 'array' => [
# { 'string_attributes' => 'string' },
# { 'string_attributes' => 'string2' },
# ]
end
end
Substitutions
If you want to substitute alternative values for the ones that the controller actually receives, you can do that using the substitutions
option:
class WithSnakeEyesController < ApplicationController
snake_eyes_params
def show
# Given
params(substitutions: {
shallow_object: {
price: { replace: 'FREE', with: 0.00 }
}
})
# If params is:
#
# 'shallowObject' => {
# 'price' => 'FREE'
# }
#
# What will be returned:
#
# 'shallow_object' => {
# 'price' => 0.00
# }
end
end
You can also provide multiple substitutions as an array. They are matched left-to-right, and the first matching substitution is the one that is used.
class WithSnakeEyesController < ApplicationController
snake_eyes_params
def show
# Given
params(substitutions: {
shallow_object: {
price: [
{ replace: 'FREE', with: 0.00 } ,
{ replace: 'EXPENSIVE', with: 999.00 }
]
}
})
# If params is:
#
# 'shallowObject' => {
# 'price' => 'FREE'
# }
#
# What will be returned:
#
# 'shallow_object' => {
# 'price' => 0.00
# }
# If params is:
#
# 'shallowObject' => {
# 'price' => 'EXPENSIVE'
# }
#
# What will be returned:
#
# 'shallow_object' => {
# 'price' => 999.00
# }
end
end
Configuration
By default SnakeEyes logs the snake case parameters to the Rails console. You can prevent this behaviour by configuring the gem:
SnakeEyes.configuration do |config|
config.log_snake_eyes_parameters = false
end
Contributing
- Fork it ( https://github.com/greena13/snake-eyes/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request