here my file: http://example.com/test.txt
i have to read content of http://example.com/test.txt (a JSON string) and parse it in Ruby
Ruby allows the following open modes: "r" Read-only, starts at beginning of file (default mode). "r+" Read-write, starts at beginning of file. "w" Write-only, truncates existing file to zero length or creates a new file for writing.
Plain old Ruby The most popular way to download a file without any dependencies is to use the standard library open-uri . open-uri extends Kernel#open so that it can open URIs as if they were files. We can use this to download an image and then save it as a file.
I would suggest using open-uri:
require 'json'
require 'open-uri'
result = JSON.parse open('http://example.com/data.json').read
require 'net/http'
uri = URI('http://my.json.emitter/some/action')
json = Net::HTTP.get(uri)
json
will contain the JSON string you fetched from uri
.
Then read this StackOverflow post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With