Skip to content

GeorgeKaraszi/jsonapi-object-mapper

Repository files navigation

Gem Version Build Status Maintainability

JsonapiObjectMapper

Deserialize's raw or pre-hashed JsonAPI objects into plan ruby objects as well as embeds any included relational resources.

Installation

Add this line to your application's Gemfile:

gem 'jsonapi-object-mapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install jsonapi-object-mapper

Usage

require "jsonapi-object-mapper"

class Photo < JsonAPIObjectMapper::Deserialize::Resource
  attribute :image
end

class User < JsonAPIObjectMapper::Deserialize::Resource
  # Embedding with another Resource class, will deserialize the `included` resource with the given class
  has_one :photo, embed_with: Photo
  
  # By default the value will be assigned whatever is located in the `included` selection. 
 # Otherwise basic relationship resource information will be added.
 #    - IE: { "type" = "friend", "id" = "10" }
  has_one :friend
  
  has_many :enemies, embed_with: User
  
  # This will accept the default value
  attribute :last_name
  
  # You can transform the setting value
  attribute :first_name do |attr_value|
    attr_value.upcase
  end
  
  # You can mass-assign attributes using the `attributes` method instead if blocks don't matter
  attributes :ssn, :passport, :more_person_info
end
  

user = User.call(json_payload) #=> <#User:123>

user.first_name #=> "FOOER"
user.last_name  #=> "Bar"
user.enemies    #=> <# JsonAPIObjectMapper::Deserialize::Collection #>

# If json API Payload is a collection of data points
users = User.call(json_payload) #=> <# JsonAPIObjectMapper::Deserialize::Collection #>

users.each do |user|
  user.first_name
  user.last_name
end

Errors

user = User.call(json_payload)

# Aliases: document_valid?
# Inverses: errors?, invalid?, document_invalid? 
user.valid?  #=> false
user.errors  #=> [<# OpenStruct title:..., detail: ..., source: {...}, ...>, ...]

Links

When links contain pagination results:

user = User.call(json_payload)
user.links             #=> <# OpenStruct total_pages: 10, next: "https://random-api.com/search?name=me&page=2", ... #>
user.links.total_pages #=> 10
user.links.next        #=> "https://random-api.com/search?name=me&page=2"

If no links come back from a response

User.call(json_payload).links #=> nil

Potential Gotchas:

Since the ruby language doesn't necessary allow for you to call methods with hyphens in them. Link method names are always converted to underscore's.

example:

payload = { "links" => { "total-pages" => 10, ... }, ... }
user = User.call(payload)

user.links.total_pages #=> 10

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.

License

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

Code of Conduct

Everyone interacting in the JsonapiObjectMapper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Deserializes JSON-API objects into plain-old ruby objects

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published