Giter Club home page Giter Club logo

reptar's Introduction

Reptar

Microlibrary for write representations of objects to your JSON APIs.

Reptar

Installation

Installing Reptar is as simple as running:

$ gem install reptar

Include Reptar in your Gemfile with gem 'reptar' or require it with require 'reptar'.

Usage

Attributes

Inherit Reptar to build your representation, initialize your Reptar class with your object and get your json output with to_json. Declare the fields you want to return with attribute

class UserRep < Reptar
  attribute :name
end

user = User.new(name: "Julio")
UserRep.new(user).to_json
# => "{\"name\":\"Julio\"}"

You can define multiple attributes with attributes

class UserRep < Reptar
  attributes :first_name, :last_name, :email
end

user = User.new(first_name: "Julio", last_name: "Piero", email: "[email protected]")
UserRep.new(user).to_json

# => The json output is:
{
  "first_name": "Julio",
  "last_name": "Piero",
  "email": "[email protected]"
}

If you want to rename an attribute to send a different name in the json output you can use the key option.

class UserRep < Reptar
  attribute :name
  attribute :city, key: :location
end

user = User.new(name: "Julio", city: "Lima")
UserRep.new(user).to_json

# => The json output is:
{
  "name": "Julio",
  "location": "Lima"
}

Methods

Send your custom methods as attributes to return their results to the json output.

class PostRep < Reptar
  attributes :title, :slug

  def slug
    title.downcase.gsub(' ', '-')
  end
end

post = Post.new(title: "My awesome post")
PostRep.new(post).to_json

# => The json output is:
{
  "title": "My awesome post",
  "slug": "my-awesome-post"
}

Set your root

You can specify a root for prepend a keyname to your ouput, use the root option for that.

UserRep.new(user).to_json(root: :user)
# => The json output is:
{ 
  "user": {
    "first_name": "Julio",
    "last_name": "Piero",
    "email": "[email protected]"
  }
}

Start with an array

You can initialize your representation class with an array of objects to return the output of each one.

class UserRep < Reptar
  attributes :name
end

user1 = User.new(name: "Julio")
user2 = User.new(name: "Abel")
user3 = User.new(name: "Piero")
users = [user1, user2, user3]
UserRep.new(users).to_json

# => The json output is:
[
  { name: "Julio" },
  { name: "Abel" },
  { name: "Piero" }
]

Collections

collection method is available to be more explicit in your representation.

class UserRep < Reptar
  attribute :name
  collection :languages
end

user = User.new(name: "Julio", languages: ["Ruby", "Js", "Go"])
UserRep.new(user).to_json

# => The json output is:
{
  name: "Julio", 
  languages: ["Ruby", "Js", "Go"]
}

Associations

You can associate your representation class with another using the with option, this is useful for return a nested output.

class UserRep < Reptar
  attributes :name, :email
  attribute :company, with: "CompanyRep"
end

class CompanyRep < Reptar  
  attributes :name, :city
end

user = User.new(name: "Julio", email: "[email protected]")
user.company = Company.new(name: "Codalot", city: "Lima")
UserRep.new(user).to_json

# => The json output is:
{
  name: "Julio",
  email: "[email protected]",
  company: {
    name: "Codalot",
    city: "Lima"
  }
}

You are free to also use collection for define your asocciations.

class UserRep < Reptar
  attributes :name, :email
  collection :posts, with: "PostRep"
end

class PostRep < Reptar
  attributes :title, :content
end

user = User.new(name: "Julio", email: "[email protected]")
user.posts << Post.new(title: "Awesome title 1", content: "lorem lipsum")
user.posts << Post.new(title: "Awesome title 2", content: "lorem lipsum")
user.posts << Post.new(title: "Awesome title 3", content: "lorem lipsum")
UserRep.new(user).to_json

# => The json output is:
{
  name: "Julio",
  email: "[email protected]",
  posts: [
    {
      title: "Awesome title 1",
      content: "lorem lipsum"
    },
    {
      title: "Awesome title 2",
      content: "lorem lipsum"
    },
    {
      title: "Awesome title 3",
      content: "lorem lipsum"
    }
  ]
}

reptar's People

Contributors

miroslavcsonka avatar piscolomo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.