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

  • 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}{article}&format=json&prop=text§ion=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