1

I have a url which has been supplied which data is updated every 30 mins and wondering if i can save the data to my database as it updates? I'm using Rails 4.2.0.

There a 10 url's all up, each with a different unit number, which needs to be reference to be able to call each data for each unit.

URL structure

http://sitename/cgi-bin/site=1

JSON structure

{"status"=>"ok", "data"=>[{"2014-08-11 11:00:00"=>14.9},{"2014-08-11 11:30:00"=>15.1}]}
2
  • for each element in data you want a new record? Commented May 27, 2015 at 2:07
  • Yes. So i'm guessing creation of table: date|number... Commented May 27, 2015 at 2:08

2 Answers 2

2

With your json response, it can be done with something like this:

json = JSON.parse('{"status"=>"ok", "data"=>[{"2014-08-11 11:00:00"=>14.9},{"2014-08-11 11:30:00"=>15.1}]}') #string representing your json

json['data'].each do |element|
  element.each do |key, value|
    Model.create(date: key, number: value) # This Model is the name of your model
  end
end

If you let me suggest you something, You can send json as:

{"status"=>"ok", "data"=>[{"date" => "2014-08-11 11:00:00", "number" =>14.9},{...}]}

So you can access data like: element['date'] and element['number']

Sign up to request clarification or add additional context in comments.

2 Comments

Great. One more question though. As this is supplied in the format I stated, there are 10 urls each with a different 'unit number' in the url. I have updated what it looks like above, how could this be integrated in say a unit_id column?
@DollarChills You have to access to site in your params hash, I think you can with params[:site], can you post your logger information for a single request?
0

I think you should go for crone jobs. Refer this Whenever gem that provides a clear syntax for writing and deploying cron jobs and you would save your data on every 30 mins into the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.