Manage API Response Using OpenStruct

Managing Api response using OpenStruct is very easy. First, you must require it and instantiate with default values, then assign the values of each key
       
  require 'ostruct'
  @user_hash = OpenStruct.new

  @user_hash.id = 1
  @user_hash.first_name = "OJ"
  @user_hash.last_name = "De Quintos"
       
 

To use it in your views (this example we used rabl), you can simply to it like this:
       
  object false # false if you don't want a parent key
  child @user_hash => :user do                  
    attributes :id, :first_name, :last_name        
       
 

No comments:

Post a Comment