README.md in snake-eyes-0.0.8 vs README.md in snake-eyes-0.1.0

- old
+ new

@@ -1,17 +1,13 @@ # SnakeEyes +[![Gem](https://img.shields.io/gem/dt/snake-eyes.svg)]() +[![Build Status](https://travis-ci.org/greena13/snake-eyes.svg)](https://travis-ci.org/greena13/snake-eyes) +[![GitHub license](https://img.shields.io/github/license/greena13/snake-eyes.svg)](https://github.com/greena13/snake-eyes/blob/master/LICENSE) + Automatically convert between camel case APIs to snake case for your Rails code -## Important - -🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 - -If you are using a version below `0.0.4`, please upgrade to avoid [potentially logging sensitive user information](https://github.com/greena13/snake-eyes/issues/1) - -🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 - ## Installation Add this line to your application's Gemfile: gem 'snake-eyes' @@ -69,10 +65,12 @@ #} 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: ```ruby class WithSnakeEyesController < ApplicationController @@ -90,25 +88,111 @@ #} end end ``` -## Limitations +#### Reference any element of an array: the '*' wildcard -SnakeEyes does *not* currently handle the following aspects of converting a JSON API to a Rails-compatible one: +To apply the `_attributes` suffix to all elements of an array, use the `'*'` wildcard in place of the array index: -**Converting arrays of objects to hashes, keyed by item indexes** +```ruby +class WithSnakeEyesController < ApplicationController + snake_eyes_params -Fox example: + def show + # Given + params(nested_attributes: [ _array: { '*' => :string } ]) -```JSON -{ - "a": [ - { "b": 1 }, - { "b": 2 }, - { "b": 3 } - ] -} + # 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: + +```ruby +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. + +```ruby +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: