Skip to content

ar2em1s/japin

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published