2

I'm trying to create an API using grape, which saves the JSON data to PostgreSQL when posted. Here is the sample JSON:

{
    "about": "this is about me",
    "company_name": "David Co",
    "company_url": "http://google.com",
    "created_at": "2013-02-03T13:09:37+05:30",
    "email": "jones@david.com",
    "first_name": "David",
    "google_plus": "http://google.com",
    "id": 1
}

This is the Ruby code:

class Posts < Grape::API

  version 'v1', :using => :path
  format :json

  resource 'posts' do
    get "/" do
      Post.all
    end

    get "/:id" do 
      Post.find(params['id'])
    end

    post "/create" do
      Post.create(params['post'])
    end
  end

end

I'm following this sample, which works fine with the above JSON.

How do I modify the code to make it work with a more complex JSON like:

{
 "about": "this is about me",
 "company_name": "David Co",
 "company_url": "http://google.com",
 "created_at": "2013-02-03T13:09:37+05:30",
 "email": "jones@david.com",
 "first_name": "David",
 "google_plus": "http://google.com",
 "id": 1,
 "chall": [{
     "attributes": {
         "type": "tegory__c",
         "url": "/services0/sobjects/__c/a08Z0000000RmoTIAS"
     }
}

Here is the present database schema:

create_table :posts do |t|
  t.string :first_name
  t.string :email
  t.string :company_name
  t.string :google_plus
  t.string :about
  t.string :company_url
  t.timestamps
end
dB.
  • 4,700
  • 2
  • 46
  • 51
iJade
  • 23,144
  • 56
  • 154
  • 243
  • What happens now if you attempt to use the more complex JSON? Any error messages in Ruby or PostgreSQL? –  Feb 03 '13 at 16:19
  • well actually that wont work...i havent included those fields in postgres.I dont know how to draw the database schema.I just updated the present database schema – iJade Feb 03 '13 at 16:27

1 Answers1

1

Your attributes is going to be a separate table, including type and url. Then your chall is going to be a link table that maps from a single posts row to multiple attributes rows. That allows you to put the data in this format in to the database.

Of course, with only one entry for attributes it's hard to provide a complete schema but this should give you some idea about how to put them together.

Note that if you wanted you could simplify things if your JSON looked like this:

"chall": [{
     "type": "tegory__c",
     "url": "/services0/sobjects/__c/a08Z0000000RmoTIAS"
}]

as then chall would be a direct one-to-many linkage to posts, although again it's hard to tell if this is valid or not given the lack of data in the example JSON. Hopefully this points you in the right direction, though.

If I assume the former, then your schema looks something like this:

create_table :posts do |t|
  t.string :first_name
  t.string :email
  t.string :company_name
  t.string :google_plus
  t.string :about
  t.string :company_url
  t.timestamps
end

create_table : attributes do |t|
  t.string :type
  t.string :url
end

create_table post_attributes do |t|
  t.references : posts
  t.references : attributes
end

In this case you have a many-to-many relationship between posts and attributes.