Project

japin

0.0
No commit activity in last 3 years
No release in over 3 years
Ruby implementation of NodeJS json-api-normalizer
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.7.2
~> 0.14.0
~> 3.0
~> 1.14.0
~> 0.21.2

Runtime

~> 1.1.0
 Project Readme

Japin

Gem Version

Japin gem is Ruby implementation of json-api-normalizer provides data normalization and formating for json api response like data.

Installation

Add this line to your application's Gemfile:

gem 'japin'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install japin

Usage

Pass hash to initializer and call normalize to normalize data.

data = { data: { id: '1', type: 'Manga', attributes: { name: "JoJo's Bizarre Adventure Part 6: Stone Ocean" } } }
Japin::Normalizer.new(data).normalize
# output
# {"Manga"=>{"1"=>{"id"=>"1", "attributes"=>{"name"=>"JoJo's Bizarre Adventure Part 6: Stone Ocean"}, "type"=>"Manga"}}}

You also can call to_h which is an alias of normalize.

data = { data: [
    { id: '1', type: 'Manga', attributes: { name: "JoJo's Bizarre Adventure Part 5: Golden Wind" } },
    { id: '2', type: 'Manga', attributes: { name: "JoJo's Bizarre Adventure Part 4: Diamond Is Unbreakable" } },
  ] }
Japin::Normalizer.new(data).to_h
# output
#{"Manga"=> {
#  1"=>{"id"=>"1", "attributes"=>{"name"=>"JoJo's Bizarre Adventure Part 5 : Golden Wind"}, "type"=>"Manga"},
# "2"=>{"id"=>"2", "attributes"=>{"name"=>"JoJo's Bizarre Adventure Part 4: Diamond Is Unbreakable"}, "type"=>"Manga"}
# } }

Also normalizer supports includes normalization

data = {
  data: [
    { id: 1, type: 'Manga', attributes: { name: "JoJo's Bizarre Adventure Part 3: Stardust Crusaders" },
      relationships: { author: { data: { id: 1, type: 'Author' } } } },
    { id: 2, type: 'Manga', attributes: { name: "JoJo's Bizarre Adventure Part 2: Battle Tendency" } ,
      relationships: { author: { data: { id: 1, type: 'Author' } } } }
  ],
  included: [{ id: 1, type: 'Author', attributes: { name: 'Hirohiko Araki' } }]
}
Japin::Normalizer.new(data).to_h
# output =>
# {"Manga"=>
#   {1=>
#     {"id"=>1,
#      "attributes"=>{"name"=>"JoJo's Bizarre Adventure Part 3: Stardust Crusaders"},
#      "type"=>"Manga",
#      "relationships"=>{"author"=>{"data"=>{"id"=>1, "type"=>"Author"}}}},
#    2=>
#     {"id"=>2,
#      "attributes"=>{"name"=>"JoJo's Bizarre Adventure Part 2: Battle Tendency"},
#      "type"=>"Manga",
#      "relationships"=>{"author"=>{"data"=>{"id"=>1, "type"=>"Author"}}}}},
#  "Author"=>{1=>{"id"=>1, "attributes"=>{"name"=>"Hirohiko Araki"}, "type"=>"Author"}}}

And another feature of normalizer is keys and type case transformation. To see all allowed cases see lucky_case gem

data = { data: { id: '1', type: 'Manga', attributes: { name: "JoJo's Bizarre Adventure Part 1: Phantom Blood" } } }
Japin::Normalizer.new(data, keys_case: :pascal_case, types_case: :upper_snake_case).normalize
# output
# {"MANGA"=>{"1"=>{"Id"=>"1", "Attributes"=>{"Name"=>"JoJo's Bizarre Adventure Part 1: Phantom Blood"}, "Type"=>"MANGA"}}}

Endpoint option is not supported now :(

Development

After checking out the repo, run bin/setup to install dependencies. Then, run spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Ar2emis/japin.

License

The gem is available as open source under the terms of the MIT License.