Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ask wikipedia from command line using ruby
require 'open-uri'
require 'json'
language = 'en'
print 'What do you need to know? : '
article = URI::encode gets.chomp
request_url = "http://#{language}{article}&format=json&prop=text&section=0"
open(request_url) do |file|
content = JSON.parse(['parse']['text'].first[1]
content_text = content.gsub(/<\/?[^>]+>/, '')
content_text = content_text.gsub(/[[:space:]]+/, ' ')
content_text = content_text.gsub(/&#[0-9]+;/,'')
content_text = content_text.gsub(/\[[0-9]+\]/,'')
puts content_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment