Question

I want to use the API of a website in a Ruby script, and the only return from the API is a number through the HTTPS protocol. Nothing more, not even tags or something, so I was wondering if there is a way to get that number in a string or integer in my script without using any XML parsing livrary or gem like REXML or hpricot or libXML, because the webpages that I want to parse are, as I said, extremely basic...

Was it helpful?

Solution

If I understand. A request to https://www.website.com/api/getid return 2.

Then, I guess this would do:

require 'net/https'
require 'uri'

def open(url)
  Net::HTTP.get(URI.parse(url))
end

response = open("https://www.website.com/api/getid")

EDIT

You'll find much usefull examples here.

As it is mentioned in the link above, HTTParty is quite popular. An example:

require 'httparty'

response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
puts response.body, response.code, response.message, response.headers.inspect
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top