DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Emad has posted 58 posts at DZone. View Full User Profile

Ask wikipedia from command line using Ruby

03.02.2014
| 1401 views |
  • submit to reddit
require 'open-uri'
require 'json'

language = 'en'

print 'What do you need to know? : '
article = URI::encode gets.chomp

request_url = "http://#{language}.wikipedia.org/w/api.php?action=parse&page=#{article}&format=json&prop=text§ion=0"

open(request_url) do |file|
  content = JSON.parse(file.read())['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
end