Save each JSON object to a model

I'm looking to save each JSON object within the call to a model so I don't have to call the API every single time I load up the application, instead I call all the models entries.

require 'httparty'
require 'json'

class Feed < ActiveRecord::Base
  include HTTParty
  base_uri 'https://www.parsehub.com/api/v2/projects'

  # GET /feeds
  # GET /feeds.json
  def boom
    response = self.class.get("/tVv8nTahbhgkyIUW8ByCe0-7/last_ready_run/data?&format=json")
    @elements = response.parsed_response["image"]
    @parsed = @elements.collect { |e| e['url'] }
  end

This is what I currently have within my model, which perfectly calls the API and displays each one of them within my view. Obviously this isn't the greatest way of doing this, I would like to save each of the JSON's URL property to my model - not entirely sure how to go about doing this!

Answers


From your comments, it seems you want create a new instance for each value in the response JSON object.

require 'httparty'
require 'json'

class Feed < ActiveRecord::Base
  include HTTParty
  base_uri 'https://www.parsehub.com/api/v2/projects'

  has_many :image_urls

  def fetch_image_urls
    response = self.class.get("/tVv8nTahbhgkyIUW8ByCe0-7/last_ready_run/data?&format=json")
    @elements = response.parsed_response["image"]

    @elements.map do |image_info|
      self.image_urls.create(url: image_info['url'])
    end
  end
end


class ImageUrl < ActiveRecord::Base
  belongs_to :feed

  # add_colum(:image_urls, :url, :string)
end

Then, to use this new method...

feed = Feed.find(id)
feed.image_urls # => []
feed.fetch_image_urls # => [<ImageUrl url:http://example.com>, ...]
feed.image_urls # => [<ImageUrl url:http://example.com>, ...]

If you are using Postgres (which I wouldn't recommend), you can store the hash (instead of JSON) in an HStore column. This will allow you to save all the JSON in one field. You don't have a name for this in you code, so let's call it info.

To access the info, you can to define methods.

class Feed < ActiveRecord::Base
  def image
    @info[:image]
  end

  def url
    @info[:url]
  end

To save the info,

def boom
  ...
  @parsed = @elements.collect { |e| e['url'] }
  @info = @parsed
  save
end

HStore reference

If you have an Image model with a field name that you want to create,

def boom
  ...
  @elements = response.parsed_response["image"]
  @image_urls = @elements.collect { |e| e['url'] }

  @image_urls.each do |url|
    Image.create(url:url)
  end
end

You can save whole JSON in a field of model/table and then can get same JSON by JSON.parse method.


Need Your Help

What is the maximum number of bytes WSARecv can receive at one time?

c++ sockets winsock2 iocp

I'm using std::vector to represent a buffer in my per IO data structure:

text search too slow on sqlite db on tableview on iphone

iphone search sqlite performance tableview

I have a large table of around 2500 entries. I am displaying it on tableview. however the search bar is too slow while doing dynamic search. ie I am filtering the table everytime the user puts in a