JSON Ruby and Smallr API

February 3, 2009 by pedro mota

Nuno’s smallr.net API talks JSON. So what about accessing it with Ruby ?\nWell, quite easy thanks to Florian’s JSON implementation for Ruby. So:\n\n$ sudo gem install json_pure\n\nNow, just need some attention on the use of the address / (slash) just after json and before the query, or you’ll get a HTTP 301 Status Code.\nCode follows:\n\nrequire 'rubygems'\nrequire 'net/http'\nrequire 'json/pure'\n\nurl = 'http://www.google.pt/search?q=json+ruby'\nescaped_url = URI.escape(url, Regexp.new(\"[^#{URI::PATTERN::UNRESERVED}]\"))\ncall = Net::HTTP.get_response(URI.parse(\"http://smallr.net/json/?url=#{escaped_url}\"))\nres = JSON.parse(call.body)\n\nputs res['status'] # ok\nputs res['url'] # http://smallr.net/925

© 2018 | Follow on Twitter | pedro mg & Hugo