Skip to content

Instantly share code, notes, and snippets.

@valachi
Created April 1, 2013 15:16
Show Gist options
  • Save valachi/5285498 to your computer and use it in GitHub Desktop.
Save valachi/5285498 to your computer and use it in GitHub Desktop.
#coding: utf-8
require 'xmlsimple'
require 'restclient'
class Wiki
attr_reader :url
def initialize(search)
@url = url_for(search)
end
def description
base['Description'].first['content'] rescue 'Нет описания'
end
def url
base['Url'].first['content'] rescue 'http://ru.wikipedia.org'
end
def title
base['Text'].first['content'] rescue 'Нет заголовка'
end
def image
base['Image'].first['source'] rescue 'wiki.jpg'
end
private
def xml
XmlSimple.xml_in raw
end
def raw
RestClient.get @url
end
def url_for(search)
'http://ru.wikipedia.org/w/api.php?action=opensearch&search='\
"#{URI.escape(search)}"\
'&format=xml'
end
def base
xml['Section'].first['Item'].first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment