views:

67

answers:

2

Hi

I need to do periodical background task fetching data from a weather site.

How in rails can I fetch json from a remote url and store it on the server? Also, there doesn't seem to be any point in storing this in a db so how in rails can I store a variable available to all users?

+1  A: 

I will only partialy answer your question.

If you want to store it as some variable availble to all users, then probably you can create a my_new_variable.rb in config/initializers. And you can generate some code that defines and initializes your variable (maybe constant is better). Then is a bad part of this approach - you have to restart your server. If you are using passanger, then just touch tmp/restart.txt and it's done.

You can also store it in yml file and load it on server start.

Even if you would store it in a different way, probably the easiest way is to restart server to load this new variable. Otherwise on every request you should check if there is new variable availble (on example check last update time on a file), or reload this file.

So, for me it looks like the easiest way is to store it in db.

klew
Thanks klew for your reply. Putting this in a db table would be entirely feasible but it would be a one row one column table which just seemed wrong. Restarting the server hourly isn't really something I want to do either.
mark
+2  A: 

Why don't you store the data in the cache (local OR memcached).

Write/update the cache when you retrieve the feed data:

def load_feed
  json_data = get_data_from_feed
  Rails.cache.write("feed_data", json_data)
  json_data
end

Read from cache, when you need to access the data:

Rails.cache.read("feed_data") || load_feed
KandadaBoggu
Thanks very much for your reply. Seems like a great solution.
mark