jeudi 31 mars 2016

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 'http://ift.tt/1SoPMRI'

  # 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!

Aucun commentaire:

Enregistrer un commentaire