Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving external JSON to DB with Rails

I'm making a GET call to an external source using the gem 'httparty'; here's my Controller.rb:

def show
  response = HTTParty.get('URI')
  user = JSON.parse(response)
  user.each {|line| puts line['user']['id']}
  #the "['user']['id']" is because of the nested JSON object that is returned after the 
  parse.
end

This returns the correct output in my rails console, but now the question is how do I save the ['id'] to my db?

Currently, my User model has :id and :name; the JSON object from the external API sends :id and :name along with a bunch of other information I don't need.

class User < ActiveRecord::Base
   attr_accessor :id, :name
end

Any help is appreciated, thanks!

like image 446
asing Avatar asked Jan 17 '13 08:01

asing


1 Answers

First, I would suggest that you create another column for the id (say external_id or something), rather than saving it in the actual id column of the User model (that column is very important in ActiveRecord and you really don't want to be setting it directly). You can validate uniqueness on that column to ensure that you don't import the same user data into multiple separate records in the db.

Once you have that column, create a class method in your User model (I've called mine save_data_from_api) to load the JSON data into the db:

class User < ActiveRecord::Base

  # optional, but probably a good idea
  validates :external_id, :uniqueness => true

  def self.save_data_from_api
    response = HTTParty.get('URI')
    user_data = JSON.parse(response)
    users = user_data.map do |line|
      u = User.new
      u.external_id = line['user']['id']
      # set name value however you want to do that
      u.save
      u
    end
    users.select(&:persisted?)
  end

end

What this does:

  • Map the JSON data to a block (user_data.map) which takes each JSON user and
    1. initializes a new user (User.new)
    2. assigns it the id JSON data (line['user']['id'])
    3. saves the new user (u.save), and
    4. return its it (the last u in the block).
  • Then, take the result (assigned to users) and select only those that actually exist (were persisted) in the db (users.select(&:persisted?)). For example, if you have a uniqueness constraint on external_id and you try to load the db data again, u.save will return false, the record will not be (re-)created, and those results will be filtered out of the results returned from the method.

This may or may not be the return value you want. Also, you probably want to add more attribute assignments (name, etc. from the JSON data) in the block. I leave it up to you to fill in those other details.

like image 62
Chris Salzberg Avatar answered Nov 07 '22 23:11

Chris Salzberg