I am building an API with Rails. The gems I use to build api are Grape and Rabl. I have done a lot of work but now I have to add status
flag before all the json response from the api. How could I do this?
I have this .rabl
file.
object @current_parent
attributes :first_name => :name
attributes :cell_phone => :mobile
attributes :email
attributes :address
node :day_care do |m|
{
:name => current_day_care.name,
:address => current_day_care.address,
:phone => current_day_care.office_phone,
:website => current_day_care.website,
:logo => current_day_care.logo.url,
:no_of_child => current_parent.relatives.count
}
end
child :relatives, :object_root => false do |m|
child :child, :object_root => false do |n|
attributes :id
attributes :first_name => :name
attributes :gender
attributes :student_stage => :classroom
end
end
This makes the the following output
{
"parent": {
"name": "Devan",
"mobile": "1234567891",
"email": "[email protected]",
"address": "762 Beahan Expressway",
"day_care": {
"name": "Brisa Erdman",
"address": "859 Hermann Summit",
"phone": "915.758.4580",
"website": "fisher.info",
"logo": "/uploads/day_care/logo/1/http%3A/example.com/ally",
"no_of_child": 2
},
"relatives": [
{
"child": {
"id": 1,
"name": "Lucious",
"gender": "t",
"classroom": "PreKindergarten"
}
},
{
"child": {
"id": 2,
"name": "Lilly",
"gender": "t",
"classroom": "Toddlers"
}
}
]
}
}
But I want to have status
flag in the beginning before the parent
opens up like below
{
"status": "Success"
"parent": {
"name": "Devan",
"mobile": "1234567891",
"email": "[email protected]",
But I cant figure out how to make this happen using rabl. Please guide me through this. If not possible provide an alternative solution.
take your current parent.rabl
file, and use it as a partial in a new rabl template that expresses the status.
object false
node :parent do
partial("parent", :object => @parent)
end
node :status do
@status
end
then call this rabl file from your controller