nick34992
6/3/2016 - 11:48 PM

SourceGraph | Written in Ruby

SourceGraph | Written in Ruby

#!/usr/bin/ruby
# based on code published by Satish Talim in his booklet titled 'How do I use Sourcegraph with Ruby?'
# under a Creative Commons Attribution 3.0 Unported License
# made available at http://leanpub.com/howdoiusesourcegraphwithruby
# a couple of lines of code have been added by nick3499 to include source's title and permalink
# ----------
# may need to run script multiple times
# expect to see error messages like this:
# sourcegraph.rb:12:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

require 'json'
require 'net/http'

# removed conditional statement

url = "http://www.reddit.com/r/ruby.json"
uri = URI.parse(url)

res = Net::HTTP.get_response(uri)
result = JSON.parse(res.body)

for x in 0..(result['data']['children'].length-1)
  puts "Author: "      + result['data']['children'][x]["data"]["author"]
  # following line added by nick3499
  puts "Title: "       + result['data']['children'][x]["data"]["title"]
  puts "Article URL: " + result['data']['children'][x]["data"]["url"]
  # following line added by nick3499
  puts "PermaLink: "   + "https://www.reddit.com" + result['data']['children'][x]["data"]["permalink"]
  puts " --------------------- "
end