Skip to content

An adapter for Active Model Serializers giving a sideload JSON style similar to AMS 0.8.* embed approach with some alterations

License

Notifications You must be signed in to change notification settings

rocketmade/sideload_serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SideloadSerializer

This is an adapter for Active Model Serializers >= 0.10 intended to give a sideload JSON style similar to AMS 0.8.* embed approach with some alterations to conform to the Rocketmade API Standard.

It includes two forms of the adapter, the first serializes to the sideloaded format and the second alters all hash keys to camelCase.

Sideloaded json is extremely useful for offline sync situations where data is being loaded into coredata or other local storage. It helps reduce reparsing and transfer size by ensuring that each object is represented exactly once.

An authentication response from Rockauth is used for the following examples.

Underscore Example:

{
  "authentications": [
    {
      "id": 4,
      "token": "<jwt>",
      "token_id": "<jwt_id>",
      "expiration": 1483647326,
      "client_version": null,
      "device_identifier": null,
      "device_os": null,
      "device_os_version": null,
      "device_description": null,
      "user_id": 3,
      "provider_authentication_id": 1
    }
  ],
  "users": [
    {
      "id": 3,
      "email": null,
      "first_name": null,
      "last_name": null
    }
  ],
  "provider_authentications": [
    {
      "id": 1,
      "provider": "facebook",
      "provider_user_id": "facebook_user_id"
    }
  ],
  "meta": {
    "primary_resource_collection": "authentications",
    "primary_resource_id": 4
  }
}

Camelized Example:

{
  "authentications": [
    {
      "id": 8,
      "token": "<jwt>",
      "expiration": 1483647222,
      "tokenId": "<jwt_id>",
      "clientVersion": null,
      "deviceIdentifier": null,
      "deviceOs": null,
      "deviceOsVersion": null,
      "deviceDescription": null,
      "userId": 5,
      "providerAuthenticationId": 1
    }
  ],
  "users": [
    {
      "id": 5,
      "email": null,
      "firstName": null,
      "lastName": null
    }
  ],
  "providerAuthentications": [
    {
      "id": 1,
      "provider": "facebook",
      "providerUserId": "facebook_user_id"
    }
  ],
  "meta": {
    "primaryResourceCollection": "authentications",
    "primaryResourceId": 8
  }
}

The same response with the json adapter:

{
  "authentication": {
    "id": 6,
    "token": "<jwt>",
    "token_id": "<jwt>",
    "expiration": 1483647755,
    "client_version": null,
    "device_identifier": null,
    "device_os": null,
    "device_os_version": null,
    "device_description": null,
    "user": {
      "id": 4,
      "email": null,
      "first_name": null,
      "last_name": null
    },
    "provider_authentication": {
      "id": 1,
      "provider": "facebook",
      "provider_user_id": "facebook_user_id"
    }
  }
}

Installation

Add this line to your application's Gemfile:

gem 'sideload_serializer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sideload_serializer

Usage

Add the following line to $RAILS_ROOT/config/initializers/active_model_serializers

ActiveModelSerializers.config.adapter = "sideload_serializer/adapter"

Or, to use the camelized keys form of the adapter:

ActiveModelSerializers.config.adapter = "sideload_serializer/camelized_adapter"

Development

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

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

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

License

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

About

An adapter for Active Model Serializers giving a sideload JSON style similar to AMS 0.8.* embed approach with some alterations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published