Lotix
Lotix

Reputation: 309

Caching API response from simple ruby script

I'm working on a simple ruby script with cli that will allow me to browse certain statistics inside the terminal.

I'm using API from the following website: https://worldcup.sfg.io/matches

require 'httparty'

url = "https://worldcup.sfg.io/matches"

response = HTTParty.get(url)

I have to goals in mind. First is to somehow save the JSON response (I'm not using a database) so I can avoid unnecessary requests. Second is to check if the new data is available, and if it is, to override the previously saved response.

What's the best way to go about this?

Upvotes: 0

Views: 96

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230346

... with cli ...

So caching in memory is likely not available to you. In this case you can save the response to a file on disk.

Second is to check if the new data is available, and if it is, to override the previously saved response.

The thing is, how can you check if new data is available without doing a request for the data? Not possible (given the information you provided). So you can simply keep fetching data every 5 minutes or so and updating your local file.

Upvotes: 1

Related Questions