Created
April 1, 2013 15:16
-
-
Save valachi/5285498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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