Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails/Ruby equivalent of PHP file_get_contents

Would like to know the Rails/Ruby equivalent of the following PHP:

$data = json_decode(file_get_contents(URL_GOES_HERE))

The URL is an external resource that returns json data (Facebook's API).

I've tried:

data = JSON.parse(URL_GOES_HERE)

but I assume I still need the `file_get_contents' part? How do I do this in Rails 4?

like image 747
tommyd456 Avatar asked Oct 28 '25 08:10

tommyd456


1 Answers

Try this

require 'open-uri'

file = open(URL_GOES_HERE)

data = JSON.parse file.read
like image 174
Siva Avatar answered Oct 29 '25 22:10

Siva